我是靠谱客的博主 迅速钢笔,这篇文章主要介绍MyBatis注解配置的一些问题(注解嵌套结果和注解嵌套查询的区别),现在分享给大家,希望可以做个参考。

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

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

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

在这里插入图片描述

Employee实体如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
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实体如下:

复制代码
1
2
3
4
5
6
public class Department { private Integer id; private String deptName; } //略去setter getter

首先先介绍嵌套结果

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

Dao层:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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();

嵌套查询

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
@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>
例如:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
/** * 批量添加 * * @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);
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
/** * 批量修改 * * @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);
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
/** * 批量删除 * * @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);

最后

以上就是迅速钢笔最近收集整理的关于MyBatis注解配置的一些问题(注解嵌套结果和注解嵌套查询的区别)的全部内容,更多相关MyBatis注解配置内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(105)

评论列表共有 0 条评论

立即
投稿
返回
顶部