package com.itheima.test;
import com.itheima.dao.IUserDao;
import com.itheima.domain.User;
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.apache.log4j.lf5.util.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
/**
* @ClassName MybatisTest
* @Description TODO
* @Author gkz
* @Date 2019/8/19 02:29
* @Version 1.0
**/
public class MybatisTest {
public static void main(String[] args) throws IOException {
//1.读取配置文件
InputStream in= Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建SqlSessionFactory工厂
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();//构建者模式
SqlSessionFactory factory=builder.build(in);
//3.使用工厂生产SqlSession对象
SqlSession sqlSession =factory.openSession();//工厂模式
//4.使用Sqlsession创建Dao接口的代理对象
IUserDao userDao=sqlSession.getMapper(IUserDao.class);//代理模式
//5.使用代理对象执行方法
List<User>users=userDao.findAll();
for (User user : users) {
System.out.println(user);
}
//6.释放资源
sqlSession.close();
in.close();
}
}
这时候运行会发现报错,控制台给的提示还是很完善的,可以看到mybatis说不知道返回的对象要封装到哪里去,因为框架创建的时候作者也不知道我这个方法findAll通过sql语句查出来的要放到哪里去,这个时候要在IUser.xml的select节点上加上resultType属性来告诉框架你要把查询出来的东西封装到哪里,我这里对应的是com.itheima.domain.User的实体类里,再次运行可以看到控制台的打印结果已经成功.
Caused by: org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'com.itheima.dao.IUserDao.findAll'. It's likely that neither a Result Type nor a Result Map was specified.
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.validateResultMapsCount(DefaultResultSetHandler.java:287)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:189)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:64)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
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)
... 6 more
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.dao.IUserDao">
<select id="findAll" resultType="com.itheima.domain.User">//如果使用的使注解那么就要使用 class节点,对应的是接口层:com.itheima.dao.IUserDao
select * from user;
</select>
</mapper>
2019-08-19 22:13:20,955 0 [ main] DEBUG ache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
2019-08-19 22:13:21,039 84 [ main] DEBUG source.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2019-08-19 22:13:21,039 84 [ main] DEBUG source.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2019-08-19 22:13:21,039 84 [ main] DEBUG source.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2019-08-19 22:13:21,039 84 [ main] DEBUG source.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2019-08-19 22:13:21,098 143 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection
2019-08-19 22:13:21,248 293 [ main] DEBUG source.pooled.PooledDataSource - Created connection 422250493.
2019-08-19 22:13:21,248 293 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@192b07fd]
2019-08-19 22:13:21,250 295 [ main] DEBUG m.itheima.dao.IUserDao.findAll - ==> Preparing: select * from user;
2019-08-19 22:13:21,265 310 [ main] DEBUG m.itheima.dao.IUserDao.findAll - ==> Parameters:
2019-08-19 22:13:21,282 327 [ main] DEBUG m.itheima.dao.IUserDao.findAll - <== Total: 6
User{id=41, username='老王', birthday=Tue Feb 27 17:47:08 GMT+08:00 2018, sex='男', address='北京'}
User{id=42, username='小二王', birthday=Fri Mar 02 15:09:37 GMT+08:00 2018, sex='女', address='北京金燕龙'}
User{id=43, username='小二王', birthday=Sun Mar 04 11:34:34 GMT+08:00 2018, sex='女', address='北京金燕龙'}
User{id=45, username='传智播客', birthday=Sun Mar 04 12:04:06 GMT+08:00 2018, sex='男', address='北京金燕龙'}
User{id=46, username='老王', birthday=Wed Mar 07 17:37:26 GMT+08:00 2018, sex='男', address='北京'}
User{id=48, username='小马宝莉', birthday=Thu Mar 08 11:44:00 GMT+08:00 2018, sex='女', address='北京修正'}
2019-08-19 22:13:21,294 339 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@192b07fd]
2019-08-19 22:13:21,294 339 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@192b07fd]
2019-08-19 22:13:21,294 339 [ main] DEBUG source.pooled.PooledDataSource - Returned connection 422250493 to pool.
Process finished with exit code 0
mybatis的入门案例:
第一步:读取配置文件
第二步:创建SqlSessionFactory工厂
第三步:创建SqlSession
第四步:创建Dao接口的代理对象
第五步:执行dao中的方法
第六步:释放资源
注意事项:不要忘记在映射配置中告知mybatis要封装到哪个实体类中配置的方式:指定实体类的全限定类目
下面讲一下mybatis的注解使用:
mybatis基于注解的入门案例:
把IUserDao.xml移除,在dao接口的方法上使用@Select注解,并且指定SQL语句,同时需要在SqlMapConfig.xml中的mapper配置时,使用class属性指定dao接口的全限定类名。