文武闲人

C#

导航

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<ChannelEntity>结合起来实现,所以还得写一些代码。

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

pom文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.1.4.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.lyb</groupId>
12     <artifactId>spring-mybatis-demo</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <packaging>jar</packaging>
15     <name>spring-mybatis-demo</name>
16     <description>Demo project for Spring Boot</description>
17 
18     <properties>
19         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21         <java.version>1.8</java.version>
22     </properties>
23 
24     <dependencies>
25         <!--
26         <dependency>
27             <groupId>org.mybatis.spring.boot</groupId>
28             <artifactId>mybatis-spring-boot-starter</artifactId>
29             <version>2.0.0</version>
30         </dependency>
31         -->
32 
33         <dependency>
34             <groupId>com.baomidou</groupId>
35             <artifactId>mybatis-plus-boot-starter</artifactId>
36             <version>3.1.1</version>
37         </dependency>
38 
39         <dependency>
40             <groupId>org.projectlombok</groupId>
41             <artifactId>lombok</artifactId>
42             <version>1.18.8</version>
43             <scope>provided</scope>
44         </dependency>
45 
46         <dependency>
47             <groupId>com.baomidou</groupId>
48             <artifactId>mybatis-plus</artifactId>
49             <version>3.1.1</version>
50         </dependency>
51 
52         <dependency>
53             <groupId>mysql</groupId>
54             <artifactId>mysql-connector-java</artifactId>
55             <scope>runtime</scope>
56         </dependency>
57         <dependency>
58             <groupId>com.baomidou</groupId>
59             <artifactId>mybatis-plus-generator</artifactId>
60             <version>3.1.1</version>
61         </dependency>
62         <dependency>
63             <groupId>org.springframework.boot</groupId>
64             <artifactId>spring-boot-starter-test</artifactId>
65             <scope>test</scope>
66         </dependency>
67     </dependencies>
68 
69     <build>
70         <plugins>
71             <plugin>
72                 <groupId>org.springframework.boot</groupId>
73                 <artifactId>spring-boot-maven-plugin</artifactId>
74             </plugin>
75         </plugins>
76     </build>
77 
78 </project>
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<StudentClass> {
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<Student> {
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<Student> selectStudents(@Param("ew") QueryWrapper<Student> wrapper);
27 
28 
29 }
View Code

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

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

selectStudents方法传入的参数QueryWrapper<Student> 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<Student> queryWrapper = new QueryWrapper<Student>();
27         queryWrapper.lambda().eq(Student::getAge,2)
28                             .eq(Student::getClassId,1);
29         List<Student> 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属性是已经赋了值的。

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

 

posted on 2019-06-16 17:22  wenwuxianren  阅读(22204)  评论(2编辑  收藏  举报