首页 > 其他分享 >一个简单的ssm案例之Mybatis框架搭建

一个简单的ssm案例之Mybatis框架搭建

时间:2022-11-30 19:02:38浏览次数:45  
标签:maven ibatis java jar ssm apache Mybatis org 搭建


1、修改AccountDao

package com.cnstrong.dao;

import com.cnstrong.domain.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
* 账户Dao接口
*/
public interface AccountDao {

//查询所有账户信息
@Select("select * from account")
public List<Account> findAll();

//保存账户信息
@Insert("insert into account (name,money) value (#{name},#{money})")
public void saveAccount(Account account);

}

2、resources目录下新建SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<!--配置环境-->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql//localhost:3306/ssm"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</dataSource>
</environment>
</environments>

<!--引入映射配置文件-->
<mappers>
<!--<mapper class="com.cnstrong.dao.AccountDao"></mapper>
<mapper class="com.cnstrong.dao.UserDao"></mapper>
-->
<package name="com.cnstrong.dao"></package>
</mappers>
</configuration>

3、test包下新建TestMybatis类

package com.cnstrong.test;

import com.cnstrong.dao.AccountDao;
import com.cnstrong.domain.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TestMybatis {

@Test
public void run1() throws Exception {
//加载配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
//创建SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//创建SqlSession对象
SqlSession session = factory.openSession();
//获取到代理对象
AccountDao dao = session.getMapper(AccountDao.class);
//查询所有数据
List<Account> list = dao.findAll();
for (Account account:list){
System.out.println(account);
}
//关闭资源
session.close();
in.close();
}
}

4、Account类中添加toString方法

5、数据库中添加数据

一个简单的ssm案例之Mybatis框架搭建_ssm整合

6、运行测试,报如下错误

"C:\Program Files\Java\jdk1.8.0_131\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:E:\idea\IntelliJ IDEA 2018.1.4\lib\idea_rt.jar=49193:E:\idea\IntelliJ IDEA 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "E:\idea\IntelliJ IDEA 2018.1.4\lib\idea_rt.jar;E:\idea\IntelliJ IDEA 2018.1.4\plugins\junit\lib\junit-rt.jar;E:\idea\IntelliJ IDEA 2018.1.4\plugins\junit\lib\junit5-rt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar;F:\ssm\target\classes;E:\maven-3.6.1\maven-repository\org\aspectj\aspectjweaver\1.6.8\aspectjweaver-1.6.8.jar;E:\maven-3.6.1\maven-repository\org\springframework\spring-aop\5.0.2.RELEASE\spring-aop-5.0.2.RELEASE.jar;E:\maven-3.6.1\maven-repository\org\springframework\spring-beans\5.0.2.RELEASE\spring-beans-5.0.2.RELEASE.jar;E:\maven-3.6.1\maven-repository\org\springframework\spring-core\5.0.2.RELEASE\spring-core-5.0.2.RELEASE.jar;E:\maven-3.6.1\maven-repository\org\springframework\spring-jcl\5.0.2.RELEASE\spring-jcl-5.0.2.RELEASE.jar;E:\maven-3.6.1\maven-repository\org\springframework\spring-context\5.0.2.RELEASE\spring-context-5.0.2.RELEASE.jar;E:\maven-3.6.1\maven-repository\org\springframework\spring-expression\5.0.2.RELEASE\spring-expression-5.0.2.RELEASE.jar;E:\maven-3.6.1\maven-repository\org\springframework\spring-web\5.0.2.RELEASE\spring-web-5.0.2.RELEASE.jar;E:\maven-3.6.1\maven-repository\org\springframework\spring-webmvc\5.0.2.RELEASE\spring-webmvc-5.0.2.RELEASE.jar;E:\maven-3.6.1\maven-repository\org\springframework\spring-test\5.0.2.RELEASE\spring-test-5.0.2.RELEASE.jar;E:\maven-3.6.1\maven-repository\org\springframework\spring-tx\5.0.2.RELEASE\spring-tx-5.0.2.RELEASE.jar;E:\maven-3.6.1\maven-repository\org\springframework\spring-jdbc\5.0.2.RELEASE\spring-jdbc-5.0.2.RELEASE.jar;E:\maven-3.6.1\maven-repository\junit\junit\4.11\junit-4.11.jar;E:\maven-3.6.1\maven-repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;E:\maven-3.6.1\maven-repository\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar;E:\maven-3.6.1\maven-repository\javax\servlet\servlet-api\2.5\servlet-api-2.5.jar;E:\maven-3.6.1\maven-repository\javax\servlet\jsp\jsp-api\2.0\jsp-api-2.0.jar;E:\maven-3.6.1\maven-repository\jstl\jstl\1.2\jstl-1.2.jar;E:\maven-3.6.1\maven-repository\log4j\log4j\1.2.12\log4j-1.2.12.jar;E:\maven-3.6.1\maven-repository\org\slf4j\slf4j-api\1.6.6\slf4j-api-1.6.6.jar;E:\maven-3.6.1\maven-repository\org\slf4j\slf4j-log4j12\1.6.6\slf4j-log4j12-1.6.6.jar;E:\maven-3.6.1\maven-repository\org\mybatis\mybatis\3.4.5\mybatis-3.4.5.jar;E:\maven-3.6.1\maven-repository\org\mybatis\mybatis-spring\1.3.0\mybatis-spring-1.3.0.jar;E:\maven-3.6.1\maven-repository\c3p0\c3p0\0.9.1.2\c3p0-0.9.1.2.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.cnstrong.test.TestMybatis,run1
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/ssm
### The error may exist in com/cnstrong/dao/AccountDao.java (best guess)
### The error may involve com.cnstrong.dao.AccountDao.findAll
### The error occurred while executing a query
### Cause: java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/ssm

    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
    at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
    at com.sun.proxy.$Proxy5.findAll(Unknown Source)
    at com.cnstrong.test.TestMybatis.run1(TestMybatis.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/ssm
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:208)
    at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:201)
    at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:196)
    at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.getConnection(UnpooledDataSource.java:93)
    at org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:404)
    at org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection(PooledDataSource.java:90)
    at org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:139)
    at org.apache.ibatis.transaction.jdbc.JdbcTransaction.getConnection(JdbcTransaction.java:61)
    at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:336)
    at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:84)
    at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62)
    at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
    at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
    ... 28 more

Process finished with exit code -1

经检查发现此处出错

一个简单的ssm案例之Mybatis框架搭建_mybatis配置_02

问题解决

一个简单的ssm案例之Mybatis框架搭建_ssm整合_03

7、在TestMybatis类中添加保存测试方法

        /**
* 测试保存
* @throws Exception
*/
@Test
public void run2() throws Exception {

Account account = new Account();
account.setName("熊大");
account.setMoney(400d);

//加载配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
//创建SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//创建SqlSession对象
SqlSession session = factory.openSession();
//获取到代理对象
AccountDao dao = session.getMapper(AccountDao.class);
//保存
dao.saveAccount(account);

//提交事务
session.commit();

//关闭资源
session.close();
in.close();
}


一个简单的ssm案例之Mybatis框架搭建_ssm整合_04

标签:maven,ibatis,java,jar,ssm,apache,Mybatis,org,搭建
From: https://blog.51cto.com/u_12528551/5900190

相关文章

  • 一个简单的ssm案例之spring整合mybatis
    把生成的代理对象存到ioc容器中,service就可以拿到代理对象并注入,就可以调用dao中的方法。1、修改applicationContext.xml<?xmlversion="1.0"encoding="UTF-8"?><beansxm......
  • 一个简单的ssm整合案例之spring整合mybatis配置事务
    1、修改applicationContext.xml为:<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.......
  • mybatis-续中续
    日志工厂如果数据库操作,出现了异常,需要记性排错,日志就是最好的助手!曾经:soutdebug现在:日志工厂SLF4JLOG4J(3.5.9起废弃)【掌握】LOG4J2【掌握】JDK_LOGGINGCO......
  • spring-mybatis
    对我来说,mybatis有几个主要核心模块吧。包括:插件(Plugin)、缓存、动态sql解析,这几个是比较难理解的部分。加载项目的时候会把mybatis里面的sql解析成一个个的MapperedStatem......
  • mybatis 连接 oracle使用concat关键字模糊查询
    oracle中不支持concat的三个参数的拼接,需要更正为SELECT*FROM"t_Dormitorys"where"RoomName"likeCONCAT(CONCAT('%','1'),'%')......
  • ssm controller层接收页面输入框内的值并处理(非Bean)
    需要放到form表单中<formaction="${pageContext.request.contextPath}/Dormitory/findByRoomName.do">               搜索房间号:<inputtype="text......
  • maven项目搭建web的war包,并配置到tomcat中
    1.新建项目,File→New→Project2.给项目添加一个Web模块3.在Web上,点击createartifact生成artifact4.给生成的artifact打包Artifacts:重要的一环,一个modu......
  • maven ssm 连接oracle
    复制dependency到pom.xml中,并下载jar包(页面上有)mvninstall:install-file-Dfile=ojdc6.jar-DgroupId=com.oracle-DartifactId=ojdbc6-Dversion=11.2.0.3-Dpackaging=ja......
  • 关于mybatis中基本类型 条件判断问题
    Mybatis默认采用ONGL解析参数,所以会自动采用对象树的形式取Integer.xxx。Integer对象没有xxx属性。如果不解析参数,mybatis自动识别传入的参数,不会报错。解决办法1.修改selec......
  • mybatis 中 if-test 判断
    之前用都是判断参数是否为空之类的,今天要判断等于一个字符,直接写等于号反而会没有执行直接跳过,后来上网查阅了资料才知道原因是:mybatis是用OGNL表达式来解析的,在OGNL的表......