一、目录结构
二、代码
1、AccountDao
1 package cn.bijian.dao; 2 3 4 import cn.bijian.model.Account; 5 6 import java.util.List; 7 8 public interface AccountDao { 9 /* 10 查询所有账户及其用户信息(使用assocation实现延迟加载) 11 */ 12 List<Account> findAll(); 13 /* 14 (使用collection实现延迟加载) 15 */ 16 List<Account> findByUid(Integer id); 17 18 }
2、UserDao
1 package cn.bijian.dao; 2 3 import cn.bijian.model.User; 4 5 import java.util.List; 6 7 public interface UserDao { 8 /* 9 根据id查询(使用assocation实现延迟加载) 10 */ 11 User findById(Integer id); 12 /* 13 查询所有用户及其账户信息(使用collection实现延迟加载) 14 */ 15 List<User> findAll(); 16 }
3、Account
1 package cn.bijian.model; 2 3 import java.io.Serializable; 4 5 public class Account implements Serializable { 6 private Integer id; 7 private Integer uid; 8 private Double money; 9 private User user; 10 11 public Integer getId() { 12 return id; 13 } 14 15 public void setId(Integer id) { 16 this.id = id; 17 } 18 19 public Integer getUid() { 20 return uid; 21 } 22 23 public void setUid(Integer uid) { 24 this.uid = uid; 25 } 26 27 public Double getMoney() { 28 return money; 29 } 30 31 public void setMoney(Double money) { 32 this.money = money; 33 } 34 35 public User getUser() { 36 return user; 37 } 38 39 public void setUser(User user) { 40 this.user = user; 41 } 42 43 @Override 44 public String toString() { 45 return "Account{" + 46 "id=" + id + 47 ", uid=" + uid + 48 ", money=" + money + 49 ", user=" + user + 50 '}'; 51 } 52 }
4、User
1 package cn.bijian.model; 2 3 import java.io.Serializable; 4 import java.util.Date; 5 import java.util.List; 6 7 public class User implements Serializable { 8 private Integer id; 9 private String username; 10 private Date birthday; 11 private String sex; 12 private String address; 13 private List<Account> accounts; 14 15 public Integer getId() { 16 return id; 17 } 18 19 public void setId(Integer id) { 20 this.id = id; 21 } 22 23 public String getUsername() { 24 return username; 25 } 26 27 public void setUsername(String username) { 28 this.username = username; 29 } 30 31 public Date getBirthday() { 32 return birthday; 33 } 34 35 public void setBirthday(Date birthday) { 36 this.birthday = birthday; 37 } 38 39 public String getSex() { 40 return sex; 41 } 42 43 public void setSex(String sex) { 44 this.sex = sex; 45 } 46 47 public String getAddress() { 48 return address; 49 } 50 51 public void setAddress(String address) { 52 this.address = address; 53 } 54 55 public List<Account> getAccounts() { 56 return accounts; 57 } 58 59 public void setAccounts(List<Account> accounts) { 60 this.accounts = accounts; 61 } 62 63 @Override 64 public String toString() { 65 return "User{" + 66 "id=" + id + 67 ", username='" + username + '\'' + 68 ", birthday=" + birthday + 69 ", sex='" + sex + '\'' + 70 ", address='" + address + '\'' + 71 ", accounts=" + accounts + 72 '}'; 73 } 74 }
5、AccountDao.xml
<?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="cn.bijian.dao.AccountDao"> <!--(使用assocation实现延迟加载)--> <resultMap id="accountMap" type="cn.bijian.model.Account"> <id column="id" property="id"/> <result column="uid" property="uid"/> <result column="money" property="money"/> <association property="user" javaType="cn.bijian.model.User" select="cn.bijian.dao.UserDao.findById" column="uid"> </association> </resultMap> <select id="findAll" resultMap="accountMap"> select * from account </select> <!--(使用collection实现延迟加载)--> <select id="findByUid" parameterType="int" resultType="cn.bijian.model.Account"> select * from account where uid = #{uid} </select> </mapper>
6、UserDao.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="cn.bijian.dao.UserDao"> 7 <cache></cache> <!-- 开启二级缓存(2) --> 8 <!--(使用assocation实现延迟加载)--> 9 <select id="findById" parameterType="int" resultType="cn.bijian.model.User" useCache="true"> 10 <!-- 11 开启二级缓存(3) useCache="true" 12 针对每次查询如果需要最新的数据,则需设置useCache="false",禁用二级缓存 13 --> 14 select * from user where id = #{uid} 15 </select> 16 <!--(使用collection实现延迟加载)--> 17 <resultMap id="userMap" type="cn.bijian.model.User"> 18 <id column="id" property="id"></id> 19 <result column="username" property="username"></result> 20 <result column="sex" property="sex"></result> 21 <result column="address" property="address"></result> 22 <result column="birthday" property="birthday"></result> 23 <collection property="accounts" ofType="cn.bijian.model.Account" 24 select="cn.bijian.dao.AccountDao.findByUid" column="id"> 25 <!--column="id"用于指定select属性的sql语句参数来源,参数来自于user的id列--> 26 </collection> 27 </resultMap> 28 <select id="findAll" resultMap="userMap"> 29 select * from user 30 </select> 31 32 </mapper>
7、SqlMapConfig.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <settings> 7 <setting name="lazyLoadingEnabled" value="true"/> 8 <setting name="aggressiveLazyLoading" value="false"/> 9 <setting name="cacheEnabled" value="true"/><!--开启二级缓存(1)--> 10 </settings> 11 12 <environments default="mysql"> 13 <environment id="mysql"> 14 <transactionManager type="JDBC"></transactionManager> 15 <dataSource type="POOLED"> <!--连接池--> 16 <property name="driver" value="com.mysql.cj.jdbc.Driver"/> 17 <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"/> 18 <property name="username" value="root"/> 19 <property name="password" value="123456"/> 20 </dataSource> 21 </environment> 22 </environments> 23 24 <mappers> 25 <mapper resource="cn/bijian/dao/UserDao.xml"></mapper> 26 <mapper resource="cn/bijian/dao/AccountDao.xml"></mapper> 27 </mappers> 28 29 </configuration>
8、MybatisTest
1 package cn.bijian; 2 3 import cn.bijian.dao.AccountDao; 4 import cn.bijian.model.Account; 5 import org.apache.ibatis.io.Resources; 6 import org.apache.ibatis.session.SqlSession; 7 import org.apache.ibatis.session.SqlSessionFactory; 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 import org.junit.After; 10 import org.junit.Before; 11 import org.junit.Test; 12 13 14 import java.io.IOException; 15 import java.io.InputStream; 16 import java.util.List; 17 18 19 public class MybatisTest { 20 private InputStream in; 21 private SqlSessionFactory sessionFactory; 22 private SqlSession session; 23 private AccountDao accountDao; 24 25 @Before 26 public void init() throws IOException { 27 in = Resources.getResourceAsStream("SqlMapConfig.xml"); 28 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); 29 sessionFactory = builder.build(in); 30 session = sessionFactory.openSession(); 31 accountDao = session.getMapper(AccountDao.class); 32 } 33 34 @After 35 public void destroy() throws IOException { 36 // session.commit(); 37 session.close(); 38 in.close(); 39 } 40 /* 41 只查账户信息不查用户信息(使用assocation实现延迟加载) 42 */ 43 @Test 44 public void testFindAll(){ 45 List<Account> accounts = accountDao.findAll(); 46 } 47 48 }
9、MybatisTest2
1 package cn.bijian; 2 3 import cn.bijian.dao.UserDao; 4 import cn.bijian.model.User; 5 import org.apache.ibatis.io.Resources; 6 import org.apache.ibatis.session.SqlSession; 7 import org.apache.ibatis.session.SqlSessionFactory; 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 import org.junit.After; 10 import org.junit.Before; 11 import org.junit.Test; 12 13 import java.io.IOException; 14 import java.io.InputStream; 15 import java.util.List; 16 17 18 public class MybatisTest2 { 19 private InputStream in; 20 private SqlSessionFactory sessionFactory; 21 private SqlSession session; 22 private UserDao userDao; 23 24 @Before 25 public void init() throws IOException { 26 in = Resources.getResourceAsStream("SqlMapConfig.xml"); 27 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); 28 sessionFactory = builder.build(in); 29 session = sessionFactory.openSession(); 30 userDao = session.getMapper(UserDao.class); 31 } 32 33 @After 34 public void destroy() throws IOException { 35 // session.commit(); 36 session.close(); 37 in.close(); 38 } 39 /* 40 (使用collection实现延迟加载) 41 */ 42 @Test 43 public void testFindAll(){ 44 List<User> users = userDao.findAll(); 45 } 46 47 }
10、MybatisTest3
1 package cn.bijian; 2 3 import cn.bijian.dao.UserDao; 4 import cn.bijian.model.User; 5 import org.apache.ibatis.io.Resources; 6 import org.apache.ibatis.session.SqlSession; 7 import org.apache.ibatis.session.SqlSessionFactory; 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 import org.junit.After; 10 import org.junit.Before; 11 import org.junit.Test; 12 13 import java.io.IOException; 14 import java.io.InputStream; 15 import java.util.List; 16 17 18 public class MybatisTest3 { 19 private InputStream in; 20 private SqlSessionFactory sessionFactory; 21 private SqlSession session; 22 private UserDao userDao; 23 24 @Before 25 public void init() throws IOException { 26 in = Resources.getResourceAsStream("SqlMapConfig.xml"); 27 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); 28 sessionFactory = builder.build(in); 29 session = sessionFactory.openSession(); 30 userDao = session.getMapper(UserDao.class); 31 } 32 33 @After 34 public void destroy() throws IOException { 35 // session.commit(); 36 session.close(); 37 in.close(); 38 } 39 /* 40 (测试一级缓存,session范围的缓存) Total: 1 41 */ 42 @Test 43 public void testFindById(){ 44 User user = userDao.findById(41); 45 System.out.println("第一次查询:"+user); 46 //清空缓存 47 // session.clearCache(); 48 // userDao = session.getMapper(UserDao.class); 49 50 User user2 = userDao.findById(41); 51 System.out.println("第二次查询:"+user2); 52 System.out.println(user==user2); 53 } 54 /* 55 一级缓存的同步功能:(以update举例,判断user==user2) 56 当执行session.close()后,再次获取session并查询id=?的User对象时又重新执行了sql语句,从数据库查询。 57 */ 58 }
11、MybatisTest4
1 package cn.bijian; 2 3 import cn.bijian.dao.UserDao; 4 import cn.bijian.model.User; 5 import org.apache.ibatis.io.Resources; 6 import org.apache.ibatis.session.SqlSession; 7 import org.apache.ibatis.session.SqlSessionFactory; 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 import org.junit.After; 10 import org.junit.Before; 11 import org.junit.Test; 12 13 import java.io.IOException; 14 import java.io.InputStream; 15 16 /* 17 测试二级缓存,mapper范围的缓存 18 */ 19 public class MybatisTest4 { 20 private InputStream in; 21 private SqlSessionFactory sessionFactory; 22 23 @Before 24 public void init() throws IOException { 25 in = Resources.getResourceAsStream("SqlMapConfig.xml"); 26 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); 27 sessionFactory = builder.build(in); 28 } 29 30 @After 31 public void destroy() throws IOException { 32 in.close(); 33 } 34 /* 35 (测试一级缓存,session范围的缓存) 36 第一次查询后关闭一级缓存,再次执行二次查询的时候,并没有对数据库发出sql语句,此时数据来自二级缓存。 37 */ 38 @Test 39 public void testFirst(){ 40 SqlSession session1 = sessionFactory.openSession(); 41 UserDao dao1 = session1.getMapper(UserDao.class); 42 User user1 = dao1.findById(41); 43 System.out.println(user1); 44 session1.close();//一级缓存消失 45 46 SqlSession session2 = sessionFactory.openSession(); 47 UserDao dao2 = session2.getMapper(UserDao.class); 48 User user2 = dao2.findById(41); 49 System.out.println(user2); 50 session1.close();//一级缓存消失 51 52 System.out.println(user1==user2); 53 } 54 }