<dependencies>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.5version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.23version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.12version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
dependencies>
<mappers>
<mapper class="xyz.slienceme.dao.IUserDao"/>
mappers>
package xyz.slienceme.dao;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import xyz.slienceme.domain.User;
import java.util.List;
/**
* @Author slience_me
* @Time : 2021/6/26 11:24
* @File : IUserDao.java
* @Software : IntelliJ IDEA
* @MyBlog : https://blog.csdn.net/slience_me
* 在mybatis中针对,CRUD一共有四个注解
* @Select @Insert @Update @Delete
*/
public interface IUserDao {
/**
* 查询所有用户
* @return
*/
@Select("select * from user")
List<User> findAll();
/**
* 保存用户
* @param user
*/
@Insert("insert into user(username,address,sex,birthday)values(#{username},#{address},#{sex},#{birthday})")
void saveUser(User user);
/**
* 更新用户
* @param user
*/
@Update("update user set username=#{username},sex=#{sex},birthday=#{birthday},address=#{address} where id=#{id}")
void updateUser(User user);
/**
* 删除用户
* @param userId
*/
@Delete("delete from user where id=#{id} ")
void deleteUser(Integer userId);
/**
* 根据id查询用户
* @param userId
* @return
*/
@Select("select * from user where id=#{id} ")
User findById(Integer userId);
/**
* 根据用户名称模糊查询
* @param username
* @return
*/
// @Select("select * from user where username like #{username} ")
@Select("select * from user where username like '%${value}%' ")
List<User> findUserByName(String username);
/**
* 查询总用户数量
* @return
*/
@Select("select count(*) from user ")
int findTotalUser();
}
package xyz.slienceme.test;
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 xyz.slienceme.dao.IUserDao;
import xyz.slienceme.domain.User;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
/**
* @Author slience_me
* @Time : 2021/6/26 15:44
* @File : AnnotationCRUDTest.java
* @Software : IntelliJ IDEA
* @MyBlog : https://blog.csdn.net/slience_me
*/
public class AnnotationCRUDTest {
private InputStream in;
private SqlSessionFactory factory;
private SqlSession sqlSession;
private IUserDao userDao;
@Before
public void init() throws IOException {
in = Resources.getResourceAsStream("SqlMapConfig.xml");
factory = new SqlSessionFactoryBuilder().build(in);
sqlSession = factory.openSession();
userDao = sqlSession.getMapper(IUserDao.class);
}
@After
public void destroy() throws IOException {
sqlSession.commit();
sqlSession.close();
in.close();
}
@Test
public void testSaveUser(){
User user = new User();
user.setUsername("宋保贤");
user.setAddress("牛驼镇");
userDao.saveUser(user);
}
@Test
public void testUpdateUser(){
User user = new User();
user.setId(54);
user.setUsername("宋保贤");
user.setAddress("牛驼镇");
user.setSex("男");
userDao.updateUser(user);
}
@Test
public void testDeleteUser(){
userDao.deleteUser(52);
}
@Test
public void testFindOne(){
User user = userDao.findById(54);
System.out.println(user);
}
@Test
public void testFindByName(){
// List users = userDao.findUserByName("%mybatis%");
List<User> users = userDao.findUserByName("mybatis");
for(User user : users){
System.out.println(user);
}
}
@Test
public void testFindTotal(){
int total = userDao.findTotalUser();
System.out.println(total);
}
}
@Results(id="accountMap" ,value = {})
有主键的@Result(id = true,column = "",property = "") id为true
无主键的@Result(column = "",property = "") id为默认false省略
@Results(id="accountMap" ,value = {
@Result(id = true,column = "id",property = "id"),
@Result(column = "uid",property = "uid"),
@Result(column = "money",property = "money"),
@Result(column = "uid",property = "user",
one = @One(select="xyz.slienceme.dao.IUserDao.findById",
fetchType= FetchType.EAGER)),
})
@Result
内部写one = @One(select="",fetchType=))
@Result(column = "uid",property = "user",
one = @One(select="xyz.slienceme.dao.IUserDao.findById",
fetchType= FetchType.EAGER))
@Result
内部写many = @Many(select="",fetchType=))
@Result(property = "accounts",column = "id",
many = @Many(select = "xyz.slienceme.dao.IAccountDao.findAccountByUid",
fetchType = FetchType.LAZY))
package xyz.slienceme.domain;
import java.io.Serializable;
/**
* @Author slience_me
* @Time : 2021/6/26 16:30
* @File : Account.java
* @Software : IntelliJ IDEA
* @MyBlog : https://blog.csdn.net/slience_me
*/
public class Account implements Serializable {
private Integer id;
private Integer uid;
private Double money;
//多对一(mybatis中称之为一对一)的映射,一个账户只能属于一个用户
private User user;
}
package xyz.slienceme.domain;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
/**
* @Author slience_me
* @Time : 2021/6/26 11:22
* @File : User.java
* @Software : IntelliJ IDEA
* @MyBlog : https://blog.csdn.net/slience_me
*/
public class User implements Serializable {
private Integer userId;
private String userName;
private String userAddress;
private String userSex;
private LocalDateTime userBirthday;
private List<Account> accounts;
}
package xyz.slienceme.dao;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import xyz.slienceme.domain.Account;
import java.util.List;
/**
* @Author slience_me
* @Time : 2021/6/26 11:24
* @File : IAccountDao.java
* @Software : IntelliJ IDEA
* @MyBlog : https://blog.csdn.net/slience_me
* 在mybatis中针对,CRUD一共有四个注解
* @Select @Insert @Update @Delete
*/
public interface IAccountDao {
/**
* 查询所有账户,并且获取每个账户所属的用户信息
* @return
*/
@Select("select * from account")
@Results(id="accountMap" ,value = {
@Result(id = true,column = "id",property = "id"),
@Result(column = "uid",property = "uid"),
@Result(column = "money",property = "money"),
@Result(column = "uid",property = "user",one = @One(select="xyz.slienceme.dao.IUserDao.findById",fetchType= FetchType.EAGER)),
})
List<Account> findAll();
/**
* 根据用户id查询账户信息
* @param userId
* @return
*/
@Select("select * from account where uid = #{userId}")
List<Account> findAccountByUid(Integer userId);
}
package xyz.slienceme.dao;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import xyz.slienceme.domain.User;
import java.util.List;
/**
* @Author slience_me
* @Time : 2021/6/26 11:24
* @File : IUserDao.java
* @Software : IntelliJ IDEA
* @MyBlog : https://blog.csdn.net/slience_me
* 在mybatis中针对,CRUD一共有四个注解
* @Select @Insert @Update @Delete
*/
@CacheNamespace(blocking = true)
public interface IUserDao {
/**
* 查询所有用户
* @return
*/
@Select("select * from user")
@Results(id="userMap",value={
@Result(id=true,column = "id",property = "userId"),
@Result(column = "username",property = "userName"),
@Result(column = "address",property = "userAddress"),
@Result(column = "sex",property = "userSex"),
@Result(column = "birthday",property = "userBirthday"),
@Result(property = "accounts",column = "id",
many = @Many(select = "xyz.slienceme.dao.IAccountDao.findAccountByUid",
fetchType = FetchType.LAZY))
})
List<User> findAll();
/**
* 根据id查询用户
* @param userId
* @return
*/
@Select("select * from user where id=#{id} ")
@ResultMap("userMap")
User findById(Integer userId);
}
package xyz.slienceme.test;
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 xyz.slienceme.dao.IAccountDao;
import xyz.slienceme.dao.IUserDao;
import xyz.slienceme.domain.Account;
import xyz.slienceme.domain.User;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* @Author slience_me
* @Time : 2021/6/26 15:44
* @File : AnnotationCRUDTest.java
* @Software : IntelliJ IDEA
* @MyBlog : https://blog.csdn.net/slience_me
*/
public class AccountTest {
private InputStream in;
private SqlSessionFactory factory;
private SqlSession sqlSession;
private IAccountDao accountDao;
@Before
public void init() throws IOException {
in = Resources.getResourceAsStream("SqlMapConfig.xml");
factory = new SqlSessionFactoryBuilder().build(in);
sqlSession = factory.openSession();
accountDao = sqlSession.getMapper(IAccountDao.class);
}
@After
public void destroy() throws IOException {
sqlSession.commit();
sqlSession.close();
in.close();
}
@Test
public void testFindAll(){
List<Account> accounts = accountDao.findAll();
for (Account account : accounts) {
System.out.println("--------每个账户的信息-----------");
System.out.println(account);
System.out.println(account.getUser());
}
}
}
<settings>
<setting name="cacheEnabled" value="true"/>
settings>
@CacheNamespace(blocking = true)
开启二级缓存
@CacheNamespace(blocking = true)
public interface IUserDao {
}
@Test
public void testFindOne(){
//一级缓存测试
User user = userDao.findById(54);
System.out.println(user);
User user2 = userDao.findById(54);
System.out.println(user2);
System.out.println(user == user2);
}
package xyz.slienceme.test;
import xyz.slienceme.dao.IUserDao;
import xyz.slienceme.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;
public class SecondLevelCatchTest {
private InputStream in;
private SqlSessionFactory factory;
@Before
public void init()throws Exception{
in = Resources.getResourceAsStream("SqlMapConfig.xml");
factory = new SqlSessionFactoryBuilder().build(in);
}
@After
public void destroy()throws Exception{
in.close();
}
@Test
public void testFindOne(){
SqlSession session = factory.openSession();
IUserDao userDao = session.getMapper(IUserDao.class);
User user = userDao.findById(54);
System.out.println(user);
session.close();//释放一级缓存
SqlSession session1 = factory.openSession();//再次打开session
IUserDao userDao1 = session1.getMapper(IUserDao.class);
User user1 = userDao1.findById(54);
System.out.println(user1);
session1.close();
}
}