MyBatis注解配置的一些问题(注解嵌套结果和注解嵌套查询的区别)

       MyBatis是一款半自动的ORM框架,使得开发者更多的关注于sql本身,无需频繁的创建和关闭连接。它与Hibernate最大的区别在于,MyBatis框架则需要开发者手写sql语句🤭,而Hibernate则同时封装了sql语句。

       首先注解式配置MyBatis较与XML配置的最大区别在于省去了诸多的复杂的XML配置,只需要在Dao层接口上打上注解,写上sql语句就可以啦🤭。话不多说,开搂!

Employee表:
在这里插入图片描述
Department表:
在这里插入图片描述
Combine表(两个表之间的联系):

在这里插入图片描述

Employee实体如下:

public class Employee {
    private Integer id;
    private String username;
    private String sex;
    private Date birthday;
    private String telephone;
    private String image;
    private String signature;
    private Department department;
}
//略去setter getter

Department实体如下:

public class Department {
    private Integer id;
    private String deptName;
}
//略去setter getter

首先先介绍嵌套结果

@Insert @Delete @Update @Select 这四个注解不做过多解释
首先先介绍两个注解@Results@Result
@Results:这个注解其实功能相当于xml配置中的resultMap标签,可以翻看一下源码,源码中有很多属性和xml配置中的resultMap标签中的属性相似。
@Result:这个注解相当于resultMap下的result标签。

Dao层:

@Select("select * from employee e inner join combine c on (e.id = c.eid) inner join department d on (c.did = d.id)")
@Results(id="employee",
    value = {
        @Result(column = "id",property = "id",id = true),
        @Result(column = "username",property = "username"),
        @Result(column = "sex",property = "sex"),
        @Result(column = "birthday",property = "birthday"),
        @Result(column = "telephone",property = "telephone"),
        @Result(column = "image",property = "image"),
        @Result(column = "signature",property = "signature"),
        @Result(column = "deptName",property = "department.deptName")
        //这里我先解释一下这个column 这个属性指的是sql语句中查出的属性,因为这里的sql语句中是个*指的是把两个表所有属性都查出来了,因此这里的deptName就是Dempartment表中的一个字段。
        //property属性对应的是实体类中的属性,而这里的Employee实体中一个属性是 private Department department 是一个Department对象;此处的department.deptName就是这个Department对象中的一个叫做deptName的属性。有点类似于ognl表达式。
    })
List<Employee> allEmployees();

嵌套查询

@Select("select * from department")
@Results(value = {
        @Result(column = "id",property = "id"),
        @Result(column = "deptName",property = "deptName"),
        @Result(column = "id",property = "employeeList",javaType = ArrayList.class,many = @Many(select="com.llq.dao.EmployeeDao.DeptEmployee"))
})
List<Department> allDepartment();


@Select("select * from employee e inner join combine c on (e.id = c.eid) inner join department d on (c.did = d.id) where d.id = #{id}")
List<Employee> DeptEmployee(int id);

注解批量删除、添加、修改
这里要注意的是在sql语句中要添加-----> <script> </script>
例如:

/**
 * 批量添加
 *
 * @param departments
 */
@Insert("<script>  insert into department (deptName) values " +
        "<foreach collection = 'departments' index = 'index' separator = ',' item = 'n' >" +
        "(#{n.deptName})  "+
        "</foreach>" +
        "  </script>")
int addListDept(@Param("departments") List<Department> departments);
/**
 * 批量修改
 *
 * @param departments
 */
@Insert("<script>  insert into department (deptName) values " +
        "<foreach collection = 'departments' index = 'index' separator = ',' item = 'n' >" +
        "(#{n.deptName})  "+
        "</foreach>" +
        "  </script>")
int addListDept(@Param("departments") List<Department> departments);
/**
 * 批量删除
 *
 * @param departments
 */
@Insert("<script>  delete department where id in " +
        "<foreach collection = ids' separator = ',' item = 'n' open='(' close =')' >" +
        "(#{n})  "+
        "</foreach>" +
        "  </script>")
int addListDept(@Param("ids") List ids);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值