好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

MyBatis 和 jeesite多表查询示例详解

有时候经常碰到多级联查,比如通过某个功能A表查角色信息,但是A表和角色表没有直接的关联关系,需要通过用户表进行关联,所以就需要多级关联查询出来了(下面的只是举例,实际应用用户和角色不会这样设计字段)

一、MyBatis xml文件手写resultMap和查询语句

1、MyBatis配置文件:因为是通过column(数据库字段名称)和property(实体类对象名称)进行数据绑定的,如果存在相同字段的名字可以用as修改字段名称也能进行特殊绑定

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

<resultMap type= "User" id= "UserResult" >

         <!--  表字段  -->

         <result property= "xxxxxxxxxId" column= "xxxxxxxxx_id" />

         <!--  通用字段和关联字段  -->

         <result property= "userId" column= "user_id" />

         <result property= "status" column= "status" />

         <result property= "createBy" column= "create_by" />

         <result property= "createTime" column= "create_time" />

         <result property= "updateBy" column= "update_by" />

         <result property= "updateTime" column= "update_time" />

         <result property= "remark" column= "remark" />

         <result property= "scriptJson" column= "script_json" />

         <!--  关联查询  -->

         <association property= "user" column= "user_id" javaType= "User" resultMap= "UserResult" />

</resultMap>

 

<resultMap type= "User" id= "UserResult" >

         <id property= "userId" column= "user_id" />

         <result property= "userName" column= "user_name" />

         <id property= "roleId" column= "role_id" />

         <!--  二级关联查询  -->

         <association property= "role" column= "role_id" javaType= "Role" resultMap= "RoleResult" />

</resultMap>

 

<resultMap type= "Role" id= "RoleResult" >

         <id property= "roleId" column= "role_id" />

         <result property= "roleName" column= "role_name" />

</resultMap>

?

1

2

3

4

5

<select id= "" parameterType= "" resultMap= "" >

     select a.*,u.role_id,r.role_name from xxxx a

     left join user u on a.user_id = u.user_id

     left join role r on r.role_id = u.role_id

</select>

二、使用jeesite中的@Table注解

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

@Table (name= "Test" , alias= "a" , label= "测试表" , columns={

         @Column (name= "testid" , attrName= "testid" , label= "测试ID" , isPK= true ),

         @Column (name= "userid" , attrName= "userid" , label= "用户id" ),

         @Column (includeEntity=DataEntity. class ),

}, joinTable = {

         @JoinTable (type= JoinTable.Type.LEFT_JOIN,entity = User. class , alias = "c" ,

                 on = "c.user_id = a.user_id" , attrName= "user" ,columns = {

                 @Column (name = "user_name" ,attrName= "username" , label= "用户名" ),

                 @Column (name = "role_id" ,attrName= "roleId" , label= "角色id" ),

         }),

         @JoinTable (type= JoinTable.Type.LEFT_JOIN,entity = Role. class , alias = "d" ,

                 on = "d.role_id = c.role_id" , attrName= "role" ,columns = {

                 @Column (name = "role_name" ,attrName= "rolename" , label= "角色名称" ),

         }),

},

         orderBy= "a.update_date DESC"

)

public class Utest extends DataEntity<Utest> {

     private static final long serialVersionUID = 1L;

     private User user;

     private Role role;

     private String testid;        // 构件ID

}

省略get、set方法,在使用的时候直接调用jeesite的finList、findPage等查询方法进行使用了数据结构就自己debug一下吧

补充:下面介绍下jeesite复杂查询语句实现

在一开始用jeesite进行开发的时候,偶尔会碰到许多问题,解决方式一般也有很多方法

一、多表查询的时候,怎么添加子表的条件进行查询?

举例:有一个文件信息表file,关联了用户表user,存储了一个user_id,需要根据用户姓名进行模糊查询

方法1:前端用beetl进行控件值绑定的时候path按 对象名.变量名 进行绑定,值得注意的是记得在User类的userName字段的注解上加上查询条件为like:queryType=QueryType.LIKE

?

1

<#form:input path="user.userName"class="form-control width-120" />

方法2:在后台代码中进行手动赋值,file实体类中增加userName变量,在后台接收到userName参数以后调用方法

?

1

file.getSqlMap().getWhere(). and ( "c.user_name" , QueryType. LIKE , "张" );

到此这篇关于MyBatis 和 jeesite多表查询的文章就介绍到这了,更多相关MyBatis 和 jeesite多表查询内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://www.cnblogs.com/yangdadaBO/p/16147888.html

查看更多关于MyBatis 和 jeesite多表查询示例详解的详细内容...

  阅读:22次