springboot整合mybatis-plus基于纯注解实现一对一(一对多)查询

因为目前所用mybatis-plus版本为3.1.1,感觉是个半成品,所有在实体类上的注解只能支持单表,没有一对一和一对多关系映射,且该功能还在开发中,相信mybatis-plus开发团队在不久的将来应该会实现此功能。

由于本人开发习惯的原因,实在是太讨厌大量的xml充斥在整个项目中,尤其是表的mapper.xml,虽然有代码生成器可以生成,但是有些复杂的查询还是需要手写配置文件里的动态sql,这点比较反感(至于为什么反感,也是有多方面原因的)。

不过可能是大量的java开发人员已经被虐惯了,已经习惯各种配置文件,虽然有代码生成器,但是本人觉得,这种不是真正的路子,按理说开源出去的东西让别人用的越简单越爽越好,而不是还需要依赖大量的工具配合才能使用(这个观点仁者见仁智者见智吧)。

因为本人采用的是spring-boot进行开发,本身springboot就提倡采用不用配置自动配置的方式,所以真心希望mybatis(不是mybatis-plus)这点需要继续努力。

基于这点,采用了mybatis-plus里的已经实现好的方法来进行了取巧的操作,废话不多说,直接上代码。

数据库是mysql。

demo的思路是这样的:学生表(Student),学生班级表(StudentClass),在学生的实体类里通过班级Id和班级进行一对一关联,主要通过mapper接口来实现,基于@Results、@Result、@One(或@Many)、@ResultMap这几个mybatis里的注解加上

mybatis-plus里的BaseMapper、QueryWrapper结合起来实现,所以还得写一些代码。

如果mybatis-plus团队把关系映射一并实现注解到实体对象上就能省大量代码了,期待他们早日实现。

pom文件

 1 
 2  3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     4.0.0
 5     
 6         org.springframework.boot
 7         spring-boot-starter-parent
 8         2.1.4.RELEASE
 9          
10     
11     com.lyb
12     spring-mybatis-demo
13     0.0.1-SNAPSHOT
14     jar
15     spring-mybatis-demo
16     Demo project for Spring Boot
17 
18     
19         UTF-8
20         UTF-8
21         1.8
22     
23 
24     
25         
32 
33         
34             com.baomidou
35             mybatis-plus-boot-starter
36             3.1.1
37         
38 
39         
40             org.projectlombok
41             lombok
42             1.18.8
43             provided
44         
45 
46         
47             com.baomidou
48             mybatis-plus
49             3.1.1
50         
51 
52         
53             mysql
54             mysql-connector-java
55             runtime
56         
57         
58             com.baomidou
59             mybatis-plus-generator
60             3.1.1
61         
62         
63             org.springframework.boot
64             spring-boot-starter-test
65             test
66         
67     
68 
69     
70         
71             
72                 org.springframework.boot
73                 spring-boot-maven-plugin
74             
75         
76     
77 
78 
View Code

可以看到数据库操作只需依赖mybatis-plus-boot-starter

application.yml文件

1 spring:
2   datasource:
3     url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
4     username: root
5     password: liuyabin
6     driver-class-name: com.mysql.jdbc.Driver
7 mybatis-plus:
8   type-aliases-package: com.lyb.springmybatisdemo.entity
9   type-aliases-super-type: java.lang.Object
View Code

Student实体类代码,可以看到实体类上加上了mybatis-plus的注解,这里也加了@Data lombok的注解为了节省多敲代码:

 1 package com.lyb.springmybatisdemo.entity;
 2 
 3 import com.baomidou.mybatisplus.annotation.IdType;
 4 import com.baomidou.mybatisplus.annotation.TableField;
 5 import com.baomidou.mybatisplus.annotation.TableId;
 6 import com.baomidou.mybatisplus.annotation.TableName;
 7 import lombok.*;
 8 
 9 @Data
10 @Builder
11 @ToString
12 @EqualsAndHashCode
13 @NoArgsConstructor
14 @AllArgsConstructor
15 @TableName(value = "student")
16 public class Student {
17     @TableId(value = "Id",type = IdType.AUTO)
18     private int id;
19     @TableField(value = "SName",exist = true,select = true)
20     private String name;
21     @TableField(value = "Age")
22     private int age;
23     @TableField(value = "ClassId")
24     private int classId;
25     @TableField(exist = false)
26     private StudentClass studentClass;
27 }
View Code

StudentClass类代码:

 1 package com.lyb.springmybatisdemo.entity;
 2 
 3 import com.baomidou.mybatisplus.annotation.IdType;
 4 import com.baomidou.mybatisplus.annotation.TableField;
 5 import com.baomidou.mybatisplus.annotation.TableId;
 6 import com.baomidou.mybatisplus.annotation.TableName;
 7 import lombok.*;
 8 
 9 @Data
10 @Builder
11 @ToString
12 @EqualsAndHashCode
13 @NoArgsConstructor
14 @AllArgsConstructor
15 @TableName(value = "stuClass")
16 public class StudentClass {
17     @TableId(value = "ClassId",type = IdType.AUTO)
18     private int classId;
19     @TableField(value = "ClassName")
20     private String className;
21 }
View Code

具体的数据库结构就不放了,参照实体类上注解就很能轻松构建出来,因为就两个表。

StudentClassMapper类代码:

1 package com.lyb.springmybatisdemo.mapper;
2 
3 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4 import com.lyb.springmybatisdemo.entity.StudentClass;
5 import org.apache.ibatis.annotations.Mapper;
6 
7 @Mapper
8 public interface StudentClassMapper extends BaseMapper {
9 }
View Code

这个mapper很简单,因为mybatis-plus里的BaseMapper已经将基础的单表操作给做了,并且该表没有一对多的需求,如果现实中确实有一对多的需求的话,可以参照下面一对一的方式实现。

StudentMapper代码:

 1 package com.lyb.springmybatisdemo.mapper;
 2 
 3 
 4 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 5 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 6 import com.lyb.springmybatisdemo.entity.Student;
 7 import org.apache.ibatis.annotations.*;
 8 
 9 import java.util.List;
10 
11 @Mapper
12 public interface StudentMapper extends BaseMapper {
13     @Results(id="stuMap",value = {
14             @Result(property = "id",column = "Id"),
15             @Result(property = "name",column = "SName"),
16             @Result(property = "age",column = "Age"),
17             @Result(property = "classId",column = "ClassID"),
18             @Result(property = "studentClass",column = "ClassID",one = @One(select = "com.lyb.springmybatisdemo.mapper.StudentClassMapper.selectById"))
19     })
20     @Select("SELECT * FROM STUDENT WHERE ID=#{id}")
21     Student findStudentById(int id);
22 
23     @Select("select * from Student where 1=1 and " +
24             "${ew.sqlSegment}")
25     @ResultMap(value = "stuMap")
26     List selectStudents(@Param("ew") QueryWrapper wrapper);
27 
28 
29 }
View Code

主要关注selectStudents方法的代码。id="stuMap"的@Results里定义了一对一的关系,可以看到有一个@One(select = "com.lyb.springmybatisdemo.mapper.StudentClassMapper.selectById")的定义,该方法并没有在

StudentClassMapper里实现,而是mybatis-plus在BaseMapper里帮我们实现了,那就可以直接方便的拿过来用了,省了我们多谢一个方法的代码了。

selectStudents方法传入的参数QueryWrapper wrapper可以充分利用lambda来自己构建各种各样的where条件,而且注意该方法的@Select的类SQL的写法,这样不会出错。

接下来是就往两张表里插入多条数据,然后是测试代码:

 1 package com.lyb.springmybatisdemo;
 2 
 3 import com.baomidou.mybatisplus.core.conditions.Wrapper;
 4 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 5 import com.lyb.springmybatisdemo.entity.Student;
 6 import com.lyb.springmybatisdemo.mapper.StudentMapper;
 7 import org.junit.Assert;
 8 import org.junit.Test;
 9 import org.junit.runner.RunWith;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.boot.test.context.SpringBootTest;
12 import org.springframework.test.context.junit4.SpringRunner;
13 
14 import java.util.List;
15 
16 @RunWith(SpringRunner.class)
17 @SpringBootTest
18 public class SpringMybatisDemoApplicationTests {
19 
20     @Autowired
21     private StudentMapper studentMapper;
22 
23 
24     @Test
25     public  void testByWrapper(){
26         QueryWrapper queryWrapper = new QueryWrapper();
27         queryWrapper.lambda().eq(Student::getAge,2)
28                             .eq(Student::getClassId,1);
29         List students = studentMapper.selectStudents(queryWrapper);
30     }
31 }
View Code

最后是springboot的启动类

 1 package com.lyb.springmybatisdemo;
 2 
 3 
 4 import org.mybatis.spring.annotation.MapperScan;
 5 import org.springframework.boot.SpringApplication;
 6 import org.springframework.boot.autoconfigure.SpringBootApplication;
 7 
 8 @SpringBootApplication
 9 @MapperScan("com.lyb.springmybatisdemo.mapper")
10 public class SpringMybatisDemoApplication {
11 
12     public static void main(String[] args) {
13         SpringApplication.run(SpringMybatisDemoApplication.class, args);
14     }
15 
16 }
View Code

运行测试类如果按照查询条件插入了多条数据,即年龄是2,班级Id是1的数据,并且班级表里也插入了数据,结果会查询出来,并且结果里Student对象的studentClass属性是已经赋了值的。

到此此示例结束,希望对大家有所帮助。

 

转载于:https://www.cnblogs.com/wenwuxianren/p/11032180.html

你可能感兴趣的:(springboot整合mybatis-plus基于纯注解实现一对一(一对多)查询)