一、目录结构
二、代码
1、AccountDao
1 package cn.bijian.dao; 2 3 4 import cn.bijian.model.Account; 5 import org.apache.ibatis.annotations.One; 6 import org.apache.ibatis.annotations.Result; 7 import org.apache.ibatis.annotations.Results; 8 import org.apache.ibatis.annotations.Select; 9 import org.apache.ibatis.mapping.FetchType; 10 11 import java.util.List; 12 13 public interface AccountDao { 14 /* 15 查询所有账户,采用延迟加载(一对一) 16 */ 17 @Select("select * from account") 18 @Results(id = "accountMap",value = { 19 @Result(id = true,column = "id",property = "id"), 20 @Result(column = "uid",property = "uid"), 21 @Result(column = "money",property = "money"), 22 @Result(column = "uid",property = "user",one = @One(select = "cn.bijian.dao.UserDao.findById",fetchType = FetchType.LAZY)) 23 }) 24 List<Account> findAll(); 25 26 /* 27 根据uid查询Account(一对多) 28 */ 29 @Select("select * from account where uid=#{id}") 30 List<Account> findByUid(Integer id); 31 }
2、UserDao
1 package cn.bijian.dao; 2 3 import cn.bijian.model.User; 4 import org.apache.ibatis.annotations.*; 5 import org.apache.ibatis.mapping.FetchType; 6 7 import java.util.List; 8 9 @CacheNamespace(blocking = true)//基于注解方式实现配置二级缓存 10 public interface UserDao { 11 /* 12 查询所有用户,采用延迟加载(一对多) 13 */ 14 @Select("select * from user") 15 @Results(id = "userMap",value = { 16 @Result(id = true,column = "id",property = "id"), 17 @Result(column = "id",property = "id"), 18 @Result(column = "username",property = "username"), 19 @Result(column = "sex",property = "sex"), 20 @Result(column = "address",property = "address"), 21 @Result(column = "birthday",property = "birthday"), 22 @Result(column = "id",property = "accounts",many = @Many(select = "cn.bijian.dao.AccountDao.findByUid",fetchType = FetchType.LAZY)) 23 }) 24 List<User> findAll(); 25 /* 26 根据id查询用户 27 */ 28 @Select("select * from user where id=#{id}") 29 @ResultMap("userMap") 30 User findById(Integer id); 31 /* 32 保存操作 33 */ 34 @Insert("insert into user(username,sex,birthday,address) values(#{username},#{sex},#{birthday},#{address})") 35 @SelectKey(keyProperty = "id",keyColumn = "id",resultType = Integer.class,before = false,statement = {"select last_insert_id()"}) 36 int saveUser(User user); 37 /* 38 更新操作 39 */ 40 @Update("update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id=#{id}") 41 int updateUser(User user); 42 /* 43 删除操作 44 */ 45 @Delete("delete from user where id=#{id}") 46 int deleteUser(Integer id); 47 /* 48 使用聚合函数查询 49 */ 50 @Select("select count(*) from user") 51 int findTotal(); 52 /* 53 模糊查询 54 */ 55 @Select("select * from user where address like #{address}") 56 List<User> findByAddress(String address); 57 }
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、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 <!--开启二级缓存--> 8 <setting name="cacheEnabled" value="true"/> 9 </settings> 10 <environments default="mysql"> 11 <environment id="mysql"> 12 <transactionManager type="JDBC"></transactionManager> 13 <dataSource type="POOLED"> <!--连接池--> 14 <property name="driver" value="com.mysql.cj.jdbc.Driver"/> 15 <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"/> 16 <property name="username" value="root"/> 17 <property name="password" value="123456"/> 18 </dataSource> 19 </environment> 20 </environments> 21 22 <mappers> 23 <package name="cn.bijian.dao"/> 24 </mappers> 25 </configuration>
6、MybatisTest
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.Date; 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 UserDao userDao; 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 userDao = session.getMapper(UserDao.class); 32 } 33 34 @After 35 public void destroy() throws IOException { 36 session.commit(); 37 session.close(); 38 in.close(); 39 } 40 /* 41 一对多,延迟加载 42 */ 43 @Test 44 public void testFindAll(){ 45 List<User> users = userDao.findAll(); 46 for (User user:users) { 47 System.out.println(user); 48 } 49 } 50 51 @Test 52 public void testFindById(){ 53 User user = userDao.findById(41); 54 System.out.println(user); 55 } 56 57 @Test 58 public void testSaveUser(){ 59 User user = new User(); 60 user.setUsername("aaa"); 61 user.setSex("男"); 62 user.setBirthday(new Date()); 63 user.setAddress("兰州"); 64 int res = userDao.saveUser(user); 65 System.out.println(res); 66 System.out.println(user.getId()); 67 } 68 69 @Test 70 public void testUpdateUser(){ 71 User user = userDao.findById(67); 72 user.setUsername("gggg"); 73 int res = userDao.updateUser(user); 74 System.out.println(res); 75 } 76 77 @Test 78 public void testDeleteUser(){ 79 int res = userDao.deleteUser(67); 80 System.out.println(res); 81 } 82 83 @Test 84 public void testFindTotal(){ 85 int total = userDao.findTotal(); 86 System.out.println(total); 87 } 88 89 @Test 90 public void testFindByAddress(){ 91 List<User> users = userDao.findByAddress("%兰州%"); 92 for (User user:users) { 93 System.out.println(user); 94 } 95 } 96 }
7、MybatisTest2
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 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 AccountDao accountDao; 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 accountDao = session.getMapper(AccountDao.class); 31 } 32 33 @After 34 public void destroy() throws IOException { 35 session.commit(); 36 session.close(); 37 in.close(); 38 } 39 /* 40 一对一,延迟加载 41 */ 42 @Test 43 public void testFindAll(){ 44 List<Account> accounts = accountDao.findAll(); 45 for (Account account:accounts) { 46 System.out.println(account); 47 } 48 } 49 50 @Test 51 public void testFindByUid(){ 52 List<Account> accounts = accountDao.findByUid(41); 53 for (Account account:accounts) { 54 System.out.println(account); 55 } 56 } 57 }