问题:在一对多中,当我们有一个用户,他有100个账户
在查询用户的时候,要不要把关联的账户查出来?
在查询账户的时候,要不要把我关联的用户查出来
在查询用户时,用户下的账户信息应该是什么时候使用,什么时候查询的
在查询账户时,账户的所属用户信息应该是睡着查询时一起查询出来的
解决:
延迟加载:在真正使用数据时才发起查询,不用的时候不查询,按需加载立即加载:不管用不用,只要一调用方法,马上发起查询
在对应的四种表关系中:一对多,多对一,多对多
一对多,多对多:通常情况下我们都是采用延迟加载多对一,一对一:通常情况请下我们采用立即加载
我们在查询mybatis的官方文档时,lazyLoadingEnable就是延迟加载全局开关,以及aggressiveLazyLoading调用加载该对象的属性
Account实现类
package com.mybatisD.domain; import java.io.Serializable; public class Account implements Serializable { private Integer id; private Integer uid; private double money; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } @Override public String toString() { return "Account{" + "id=" + id + ", uid=" + uid + ", money=" + money + '}'; } }
User实现类
package com.mybatisD.domain; import java.io.Serializable; import java.util.Date; import java.util.List; public class User implements Serializable { private Integer id; private String username; private Date birthday; private String sex; private String address; //多对多的关系映射,一个用户可以具备多个角色 private Listroles; public List getRoles() { return roles; } public void setRoles(List roles) { this.roles = roles; } private List account; //一对多关系映射,主表实体应该包含从表实体的集合引用 public List getAccount() { return account; } public void setAccount(List account) { this.account = account; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + ''' + ", birthday=" + birthday + ", sex='" + sex + ''' + ", address='" + address + ''' + '}'; } }
IAccountDao接口
package com.mybatisD.dao; import com.mybatisD.domain.Account; import com.mybatisD.domain.AccountUser; import java.util.List; public interface IAccountDao { //查询所有账户 List findAll(); //查询账户,并且带有用户名称和地址信息 List findAllAccount(); }
IUserDao接口
package com.mybatisD.dao; import com.mybatisD.domain.QueryVo; import com.mybatisD.domain.User; import org.apache.ibatis.annotations.Select; import java.util.List; public interface IUserDao { // 查询所有 *** 作 // @Select("select * from user") 这是用注解方法 //根据查询一个用户 User findById(Integer userId); }
IAccountDao.xml配置文件
IUserDao.xml配置文件
SqlMapConfig.xml配置文件
其中最主要的是
配置日志信息log4j.properties
log4j.rootCategory=debug,CONSOLE, LOGFILE log4j.logger.org.apache.axis.enterprise=FATAL, ConSOLE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x -%mn log4j.appender.LOGFILE=org.apache.log4j.FileAppender log4j.appender.LOGFILE.File=d:axis.log log4j.appender.LOGFILE.Append=true log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x -%mn
配置jdbc.properties数据库
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/newsql?characterEncoding=utf-8 jdbc.username=root jdbc.password=
测试类
package com.mybatisJ.test; import com.mybatisD.dao.IAccountDao; import com.mybatisD.dao.IUserDao; import com.mybatisD.domain.Account; import com.mybatisD.domain.AccountUser; import com.mybatisD.domain.QueryVo; import com.mybatisD.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.junit.After; import org.junit.Before; import org.junit.Test; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; public class AccountTest { private InputStream in; private SqlSession sqlSession; private IAccountDao accountDao; @Before//用于在测试方法执行之前执行 public void init()throws Exception{ in = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(in); sqlSession = factory.openSession(); //4.使用SqlSession创建Dao接口的代理对象 accountDao = sqlSession.getMapper(IAccountDao.class); } @After//用于在测试方法之后执行 public void destory() throws Exception{ //添加提交事物,不添加无法提交 sqlSession.commit(); sqlSession.close(); in.close(); } //查询连接方法其中包括用户的一些信息 @Test public void testFindAll() throws Exception{ List accounts = accountDao.findAll(); for (Account account: accounts) { System.out.println("-----------------------"); System.out.println(account); System.out.println(account.getUser()); } } //查询account的信息 @Test public void findAllAccount(){ List accountUsers = accountDao.findAllAccount(); for (AccountUser au: accountUsers) { System.out.println(au); } } }
没有配置延迟加载时数据都是直接加载出来的其他信息
Account与User实现类都不变,与上面的一致
改变的为IAccountDao接口
package com.mybatisD.dao; import com.mybatisD.domain.Account; import com.mybatisD.domain.AccountUser; import java.util.List; public interface IAccountDao { //查询所有账户 List findAll(); //查询账户,并且带有用户名称和地址信息 List findAllAccount(); //加入 List findAccountByUid(Integer uid); }
IAccountDao.xml的配置
IUserDao.xml配置信息
select * from user select * from user where id=#{userid}
测试类
package com.mybatisJ.test; import com.mybatisD.dao.IAccountDao; import com.mybatisD.dao.IUserDao; import com.mybatisD.domain.Account; import com.mybatisD.domain.AccountUser; import com.mybatisD.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.junit.After; import org.junit.Before; import org.junit.Test; import java.io.InputStream; import java.util.List; public class userTest { private InputStream in; private SqlSession sqlSession; private IUserDao userDao; @Before//用于在测试方法执行之前执行 public void init()throws Exception{ in = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(in); sqlSession = factory.openSession(); //4.使用SqlSession创建Dao接口的代理对象 userDao = sqlSession.getMapper(IUserDao.class); } @After//用于在测试方法之后执行 public void destory() throws Exception{ //添加提交事物,不添加无法提交 sqlSession.commit(); sqlSession.close(); in.close(); } //查询连接方法其中包括用户的一些信息 @Test public void testFindAll() throws Exception{ Listusers = userDao.findAll(); for (User user: users) { System.out.println(user); System.out.println(user.getAccount()); } } }
结果,延迟加载就是在用到这个信息才会加载
什么是缓存:存在于内存中的临时数据
为什么使用缓存:减少和数据库的交互次数,提高执行效率
什么样的数据能使用缓存:经常查询并且不经常改变的、数据的正确与否对最终的结果影响不大
不能使用缓存:经常改变的数据,数据的正确与否对最终的结果影响很大如:商品的库存,银行的汇率
一级缓存是SqlSession范围的缓存,当调用SqlSession的修改,添加,删除,commit()提交,close()等方法时,就会清空一级缓存
一级缓存
它指的是Mybatis中SqlSession对象的缓存,当我们执行查询之后,查询的结果会同时存入到SqlSession为我们提供一块区域中,该区域的结构是一个Map,当我们再次查询同样的数据,Mybatis会先去SqlSession中查询是否有,有的话直接用,当SqlSession对象消失后,Mybatis的 一级缓存也就消失了
代码:
配置IUserDao.xml
在这里插入代码片`` select * from user
测试中最主要的
@Test public void testFirstLevelchatch() throws Exception{ User user1 = userDao.findById(2); System.out.println("第一次查询"+user1); sqlSession.close();//是为了清除缓存 //再次获取SqlSession //sqlSession.clearcache(); 也是可以清空缓存 sqlSession =factory.openSession(); userDao = sqlSession.getMapper(IUserDao.class); User user2 = userDao.findById(2); System.out.println("第二次查询"+ user2); System.out.println(user1=user2); }
测试类
package com.mybatisJ.test; import com.mybatisD.dao.IAccountDao; import com.mybatisD.dao.IUserDao; import com.mybatisD.domain.Account; import com.mybatisD.domain.AccountUser; import com.mybatisD.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.junit.After; import org.junit.Before; import org.junit.Test; import java.io.InputStream; import java.util.List; public class userTest { private InputStream in; private SqlSessionFactory factory; private SqlSession sqlSession; private IUserDao userDao; @Before//用于在测试方法执行之前执行 public void init()throws Exception{ in = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); factory = builder.build(in); sqlSession = factory.openSession(); //4.使用SqlSession创建Dao接口的代理对象 userDao = sqlSession.getMapper(IUserDao.class); } @After//用于在测试方法之后执行 public void destory() throws Exception{ //添加提交事物,不添加无法提交 sqlSession.commit(); sqlSession.close(); in.close(); } //一级缓存 @Test public void testFirstLevelchatch() throws Exception{ User user1 = userDao.findById(2); System.out.println("第一次查询"+user1); sqlSession.close();//是为了清除缓存 //再次获取SqlSession //sqlSession.clearcache(); 也是可以清空缓存 sqlSession =factory.openSession(); userDao = sqlSession.getMapper(IUserDao.class); User user2 = userDao.findById(2); System.out.println("第二次查询"+ user2); System.out.println(user1=user2); } }
两次查询地址不一样,使用两次Connection第二次为缓存
当使用update更新用户信息,一级缓存和数据库不一致了怎么办
他会再次向数据库进行查询,不会再缓存里面查询
一级缓存是SqlSession范围的缓存,当调用SqlSession的修改,添加,删除,commit()提交,close()等方法时,就会清空一级缓存
它指的是Mybatis中SqlsessionFactory对象的缓存,由同一个SqlSessionFactory对象创建,其中创建的SqlSession共享其缓存
二级缓存的使用步骤:
1.让Mybatis框架支持二级缓存(在SqlMapConfig.xml中配置)2.让当前的映射文件支持二级缓存(在IUserDao.xml中配置)3.让当前的 *** 作支持二级缓存(在select标签中配置)
配置SqlMapConfig.xml开启二级缓存
配置IUserDao.xml配置开启二级缓存
select * from user select * from user where id=#{userid}
测试方法
package com.mybatisJ.test; import com.mybatisD.dao.IUserDao; import com.mybatisD.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.junit.After; import org.junit.Before; import org.junit.Test; import java.io.InputStream; import java.util.Date; import com.mybatisD.dao.IAccountDao; import com.mybatisD.dao.IUserDao; import com.mybatisD.domain.Account; import com.mybatisD.domain.AccountUser; import com.mybatisD.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.junit.After; import org.junit.Before; import org.junit.Test; import java.io.InputStream; import java.util.Date; import java.util.List; public class SecondLeaveCatcheTest { private InputStream in; private SqlSessionFactory factory; @Before//用于在测试方法执行之前执行 public void init()throws Exception{ in = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); factory = builder.build(in); } @After//用于在测试方法之后执行 public void destory() throws Exception{ in.close(); } //一级缓存 @Test public void tesSecondChatch() throws Exception { SqlSession sqlSession1 = factory.openSession(); IUserDao dao1 =sqlSession1.getMapper(IUserDao.class); User user1 = dao1.findById(2); System.out.println(user1); sqlSession1.close(); //一级缓存消失 SqlSession sqlSession2 = factory.openSession(); IUserDao dao2 =sqlSession2.getMapper(IUserDao.class); User user2 = dao2.findById(2); System.out.println(user2); sqlSession2.close(); System.out.println( user1 == user2); } }
结果
其中的
System.out.println( user1 == user2);
输出的是false
是使用注解将IUserDao.xml的配置进行注解
步骤:
环境搭建单表CRUD *** 作(代理Dao方式)多表查询的配置
最输入项目名称
下面进行设置Maven工程的配置,因为我是自己配置的所有不会出现什么麻烦
导入依赖
设置实现User类其中加入Account类是为了实现多对多的数据模式
package com.mybatisD.daomain; import java.io.Serializable; import java.util.Date; import java.util.List; public class User implements Serializable { private Integer id; private String username; private String address; private String sex; private Date birthday; //一对多关系映射,一个用户对应多个账户 private List accounts; public List getAccounts() { return accounts; } public void setAccounts(List accounts) { this.accounts = accounts; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + ''' + ", address='" + address + ''' + ", sex='" + sex + ''' + ", birthday=" + birthday + '}'; } }
设置Account类其中加入User为的是实现多对多数据模式
package com.mybatisD.daomain; import java.io.Serializable; public class Account implements Serializable { private Integer id; private Integer uid; private double money; //多对一(mybatis中称之为一对一)的映射,一个账户只能属于一个用户 private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } @Override public String toString() { return "Account{" + "id=" + id + ", uid=" + uid + ", money=" + money + '}'; } }
IUserDao接口类
package com.mybatisD.dao; import com.mybatisD.daomain.User; import org.apache.ibatis.annotations.*; import org.apache.ibatis.mapping.FetchType; import java.util.List; @CacheNamespace(blocking = true) //注解开启二级缓存,要在SqlMapConfig.xml里面开启二级缓存 public interface IUserDao { @Select("select * from user") ListfindAll();//查询所有用户 @Insert("insert into user (username,address,sex,birthday) values(#{username},#{address},#{sex},#{birthday})") void saveUser(User user); @Update("update user set username=#{username} ,sex=#{sex},address=#{address},birthday=#{birthday} where id =#{id}") void updateUser(User user); @Delete("delete from user where id = #{id}") void deleteUser(Integer id); @Select("select * from user where id = #{id}") User findById(Integer id); @Select("select * from user where username like #{username}") //需要在测试类加入双%%号 // @Select("select * from user where username like '%${value}%'") List findUserByName(String username); @Select("select count(*) from user") int findTotalUser(); @Select("select * from user") @Results(value = { @Result(property = "accounts", column = "id", many = @Many(select = "com.mybatisD.dao.IAccount.findAccountByUid",fetchType = FetchType.LAZY))//LAZY是延迟 }) List findAllAccount();//查询所有用户 }
IAccountDao接口类
package com.mybatisD.dao; import com.mybatisD.daomain.Account; import org.apache.ibatis.annotations.One; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.mapping.FetchType; import java.util.List; public interface IAccountDao { @Select("select * from account") @Results(value = { @Result(id = true,column = "id",property = "id"), @Result(column = "uid",property = "uid"), @Result(column = "money",property = "money"), @Result(property = "user" ,column = "uid" ,one=@One(select = "com.mybatisD.dao.IUserDao.findById" ,fetchType = FetchType.EAGER)) }) List findAll(); @Select("select * from account where uid = #{id}") List findAccountByUid(Integer id); }
SqlMapConfig.xml配置
jdbc.properties数据连接
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/newsql?characterEncoding=utf-8 jdbc.username=root jdbc.password=
日志信息配置log4j.properties
log4j.rootCategory=debug,CONSOLE, LOGFILE log4j.logger.org.apache.axis.enterprise=FATAL, ConSOLE log4j.appender.ConSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x -%mn log4j.appender.LOGFILE=org.apache.log4j.FileAppender log4j.appender.LOGFILE.File=d:axis.log log4j.appender.LOGFILE.Append=true log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x -%mn
User测试实现类
package com.mybateisT.TestD; import com.mybatisD.dao.IUserDao; import com.mybatisD.daomain.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.junit.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.Date; import java.util.List; public class MybatisAnnoTest { private InputStream in; private SqlSessionFactory factory; private SqlSession session; private IUserDao userDao; @Before public void init() throws IOException { //1.获取字节输入流 in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2.根据字节输入流构建SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //3.根据SqlSessionFactory生产一个SqlSession session = factory.openSession(); //4.使用Selsession获取Dao代理对象 userDao = session.getMapper(IUserDao.class); } @After public void destory() throws IOException { session.commit(); session.close(); in.close(); } @Test public void TestFindAll() throws IOException { //5.执行Dao方法 Listusers = userDao.findAll(); for (User user: users) { System.out.println(user); } } @Test public void TestSave() { User user = new User(); user.setUsername("test to annotation"); user.setAddress("北极"); user.setSex("女"); user.setBirthday(new Date()); userDao.saveUser(user); System.out.println(user); } @Test public void TestUpdate() { User user = new User(); user.setId(3); user.setUsername("mybatis to annotation update"); user.setAddress("北极"); user.setSex("nv"); user.setBirthday(new Date()); userDao.updateUser(user); System.out.println(user); } @Test public void TestDelete() { List users = userDao.findAll(); userDao.deleteUser(12); for (User user:users) { System.out.println(user); } } @Test public void TestFindOne() { User users = userDao.findById(1); System.out.println(users); } @Test public void TestFindName() { List users = userDao.findUserByName("%赵%"); for (User user: users) { System.out.println(users); } } @Test public void TestTotal() { int total = userDao.findTotalUser(); System.out.println(total); } @Test public void testFindAccount(){ List users = userDao.findAllAccount(); for (User user:users) { System.out.println("-----用户信息------"); System.out.println(user); System.out.println(user.getAccounts()); } } }
Account测试实现类
package com.mybateisT.TestD; import com.mybatisD.dao.IAccount; import com.mybatisD.dao.IUserDao; import com.mybatisD.daomain.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.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; public class AccountTest { private InputStream in; private SqlSessionFactory factory; private SqlSession session; private IAccount iAccount; @Before public void init() throws IOException { //1.获取字节输入流 in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2.根据字节输入流构建SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //3.根据SqlSessionFactory生产一个SqlSession session = factory.openSession(); //4.使用Selsession获取Dao代理对象 iAccount = session.getMapper(IAccount.class); } @After public void destory() throws IOException { session.commit(); session.close(); in.close(); } @Test public void testFindAllAccount(){ List accounts = iAccount.findAll(); for (Account account:accounts) { System.out.println("--------每个账户信息---------"); System.out.println(account); System.out.println(account.getUser()); } } }
二级缓存测试实现类
package com.mybateisT.TestD; import com.mybatisD.dao.IUserDao; import com.mybatisD.daomain.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.junit.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; public class SecondLevelCatchTest { private InputStream in; private SqlSessionFactory factory; private SqlSession session; private IUserDao userDao; @Before public void init() throws IOException { //1.获取字节输入流 in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2.根据字节输入流构建SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //3.根据SqlSessionFactory生产一个SqlSession session = factory.openSession(); //4.使用Selsession获取Dao代理对象 userDao = session.getMapper(IUserDao.class); } @After public void destory() throws IOException { in.close(); } @Test public void TestFindOne() throws IOException { //5.执行Dao方法 User user = userDao.findById(1); System.out.println(user); session.close();//释放一级缓存 SqlSession session1 = factory.openSession();//再次打开session IUserDao userDao1 = session1.getMapper(IUserDao.class); User user1 = userDao1.findById(1); System.out.println(user1); System.out.println(user==user1); } }
文件结构
数据库表
注解开发配置二级缓存现在SqlMapConfig.xml先配置
在IUserDao接口类加入注解@CacheNamespace开启二级缓存
@CacheNamespace(blocking = true) //注解开启二级缓存,要在SqlMapConfig.xml里面开启二级缓存 public interface IUserDao {
编写测类
package com.mybateisT.TestD; import com.mybatisD.dao.IUserDao; import com.mybatisD.daomain.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.junit.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; public class SecondLevelCatchTest { private InputStream in; private SqlSessionFactory factory; private SqlSession session; private IUserDao userDao; @Before public void init() throws IOException { //1.获取字节输入流 in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2.根据字节输入流构建SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //3.根据SqlSessionFactory生产一个SqlSession session = factory.openSession(); //4.使用Selsession获取Dao代理对象 userDao = session.getMapper(IUserDao.class); } @After public void destory() throws IOException { in.close(); } @Test public void TestFindOne() throws IOException { //5.执行Dao方法 User user = userDao.findById(1); System.out.println(user); session.close();//释放一级缓存 SqlSession session1 = factory.openSession();//再次打开session IUserDao userDao1 = session1.getMapper(IUserDao.class); User user1 = userDao1.findById(1); System.out.println(user1); System.out.println(user==user1); } }2.还有什么问题可以留言给我,会尽心解答的,觉得文章不错的话点个赞把
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)