10. Mybatis annotation development
10.1 What is annotation development
The initial configuration information of Mybatis is based on XML, and the mapping statement (SQL) is also defined in XML. And to MyBatis 3 provides a new annotation-based configuration. Using the annotation development method can reduce the need to write Mapper mapping files.
10.2 Commonly used annotations
annotation | describe |
---|---|
@Insert | New configuration |
@Update | configuration update |
@Delete | configuration delete |
@Select | configuration query |
@Options | Configure the return of the primary key, turn off the secondary cache and other functions |
@Result | result set encapsulation |
@Results | Use with @Result to encapsulate multiple result sets |
@ResultMap | Refers to the package defined by @Results |
@One | One-to-one result set encapsulation |
@Many | One-to-many result set encapsulation |
@SelectProvider | Dynamic SQL mapping |
@CacheNamespace | L2 cache |
@Param | Enter multiple parameters |
@Mapper | Give the mapper DAO to Spring management, and integrate it with |
10.3 Entity classes
10.3.1 Department class
public class Dept {
private Integer deptno;
private String dname;
private String loc;
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
@Override
public String toString() {
return "Dept{" +
"deptno=" + deptno +
", dname='" + dname + '\'' +
", loc='" + loc + '\'' +
'}';
}
}
10.3.2 Employee class
public class Emp {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Date hiredate;
private Double sal;
private Double comm;
private Integer deptno;
private Dept dept;
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Integer getMgr() {
return mgr;
}
public void setMgr(Integer mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public Double getComm() {
return comm;
}
public void setComm(Double comm) {
this.comm = comm;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Emp{" +
"empno=" + empno +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", mgr=" + mgr +
", hiredate=" + hiredate +
", sal=" + sal +
", comm=" + comm +
", deptno=" + deptno +
", dept=" + dept +
'}';
}
}
10.4 Single table addition, deletion, modification and difference
10.4.1 mapper interface
public interface DeptMapper {
@Select("select deptno,dname,loc from dept")
List<Dept> select();
@Select("select deptno,dname,loc from dept where deptno = #{deptno}")
Dept selectById(Integer deptno);
@Insert("insert into dept(dname,loc) values (#{dname}, #{loc})")
@Options(useGeneratedKeys = true, keyProperty = "deptno", keyColumn = "deptno")
void insert(Dept dept);
@Update("update dept set dname = #{dname},loc=#{loc} where deptno = #{deptno}")
void update(Dept dept);
@Delete("delete from dept where deptno=#{deptno}")
void delete(Integer deptno);
}
10.4.2 Testing
public class AnnotationTest {
@Test
public void testSelect() {
SqlSession sqlSession = MybatisUtil.getSession();
DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
List<Dept> list = deptMapper.select();
for (Dept dept : list) {
System.out.println(dept);
}
sqlSession.close();
}
@Test
public void testSelectById() {
SqlSession sqlSession = MybatisUtil.getSession();
DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
Dept dept = deptMapper.selectById(10);
System.out.println(dept);
sqlSession.close();
}
@Test
public void testInsert() {
SqlSession sqlSession = MybatisUtil.getSession();
DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
Dept dept = new Dept();
dept.setDname("aa");
dept.setLoc("aa");
deptMapper.insert(dept);
sqlSession.commit();
System.out.println("主键:" + dept.getDeptno());
sqlSession.close();
}
@Test
public void testUpdate() {
SqlSession sqlSession = MybatisUtil.getSession();
DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
Dept dept = new Dept();
dept.setDeptno(41);
dept.setDname("bb");
dept.setLoc("bb");
deptMapper.update(dept);
sqlSession.commit();
sqlSession.close();
}
@Test
public void testDelete() {
SqlSession sqlSession = MybatisUtil.getSession();
DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
deptMapper.delete(41);
sqlSession.commit();
sqlSession.close();
}
}
10.5 One-to-one relationship mapping
10.5.1 mapper interface
public interface EmpMapper {
/*
* 手动映射resultMap标签
* @Results + @Result注解替代
* @Results = resultMap标签
* @Result = resultMap标签的子标签id和result
*/
@Select("select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp")
@Results(id = "selectResultMap",
value = {
@Result(id = true, column = "empno", property = "empno"),
@Result(column = "ename", property = "ename"),
@Result(column = "job", property = "job"),
@Result(column = "mgr", property = "mgr"),
@Result(column = "hiredate", property = "hiredate"),
@Result(column = "sal", property = "sal"),
@Result(column = "comm", property = "comm"),
@Result(column = "deptno", property = "deptno"),
@Result(column = "deptno", property = "dept", javaType = Dept.class,
one = @One(select = "com.newcapec.dao.DeptDao.selectById", fetchType = FetchType.LAZY))
})
List<Emp> select();
@Select("select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp where empno=#{empno}")
@ResultMap("selectResultMap")
Emp selectById(Integer empno);
}
10.5.2 Testing
@Test
public void testSelectEmp() {
SqlSession sqlSession = MybatisUtil.getSession();
EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
List<Emp> empList = empMapper.select();
for (Emp emp : empList) {
System.out.println(emp);
}
sqlSession.close();
}
@Test
public void testSelectEmpById() {
SqlSession sqlSession = MybatisUtil.getSession();
EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
Emp emp = empMapper.selectById(7369);
System.out.println(emp);
sqlSession.close();
}