注解开发越来越流行,Mybatis 也可以使用注解开发方式,这样我们就可以减少编写 Mapper 映射
文件了。本次我们先围绕一些基本的 CRUD 来学习。
1.@Insert:实现新增
2.@Update:实现更新
3.@Delete:实现删除
4.@Select:实现查询
5.@Result:实现结果集封装
6.@Results:可以与@Result 一起使用,封装多个结果集
7.@ResultMap:实现引用@Results 定义的封装
8.@One:实现一对一结果集封装
9.@Many:实现一对多结果集封装
10.@SelectProvider: 实现动态 SQL 映射
11.@CacheNamespace:实现注解二级缓存的使用
单表的 CRUD 操作是最基本的操作,博主上一篇博客都是基于 Mybaits 的映射文件来实现的。今天使用注解。
package com.yx.domain;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
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 + '\'' +
'}';
}
}
注意:属性名称要与数据库一致,若因故不一致,切记配置时要一一对应(使用@result)
@Select("select * from user")
List findAll();
@Select("select * from user")
@Results(id="userMap",
value= {
@Result(id=true,column="id",property="userId"),
@Result(column="username",property="userName"),
@Result(column="sex",property="userSex"),
@Result(column="address",property="userAddress"),
@Result(column="birthday",property="userBirthday")
})
List findAll();
@Insert("insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})")
@SelectKey(statement = "select last_insert_id()", keyProperty = "id", before = false, resultType=int.class,keyColumn="id")
void saveUser(User user);
@Update("update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id = #{id}")
void updateUser(User user);
@Delete("delete from user where id = #{id}")
void deleteUser(Integer id);
package com.yx.test;
import com.yx.dao.IUserDao;
import com.yx.domain.QueryVo;
import com.yx.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.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
public class mybatisTest{
private InputStream in;
private SqlSession sqlSession;
private IUserDao userDao;
@Before
public void init() throws Exception {
//1.读取配置文件
in = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建SqlSessionFactory工厂
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
//3.使用工厂生产SqlSession对象
sqlSession = factory.openSession();
//4.使用Sqlseesion创建Dao接口的代理对象
userDao = sqlSession.getMapper(IUserDao.class);
}
@After
public void destory() throws Exception {
sqlSession.commit();
//6.释放资源
sqlSession.close();
in.close();
}
@Test
public void testFindAll() throws Exception {
//5.使用代理对象执行方法
List users = userDao.findAll();
for (User user:users) {
System.out.println(user);
}
}
@Test
public void testSaveUser() throws Exception {
User user = new User();
user.setUsername("yuexiang");
user.setAddress("安徽合肥");
user.setBirthday(new Date(1995-11-16));
user.setSex("男");
System.out.println(user);
userDao.saveUser(user);
System.out.println(user.getId());
}
@Test
public void testUpdateUser(){
User user = new User();
user.setUsername("yuexiang11");
user.setAddress("安徽合肥");
user.setBirthday(new Date());
user.setSex("男");
user.setId(50);
userDao.updateUser(user);
}
@Test
public void testDeleteUser(){
userDao.deleteUser(50);
}
@Test
public void testFindUserById(){
User user = userDao.findUserById(48);
System.out.println(user);
}