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、数据库中添加数据
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
经检查发现此处出错
问题解决
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();
}