由於目前所用mybatis-plus版本爲3.1.1,感受是個半成品,全部在實體類上的註解只能支持單表,沒有一對一和一對多關係映射,且該功能還在開發中,相信mybatis-plus開發團隊在不久的未來應該會實現此功能。java
因爲本人開發習慣的緣由,實在是太討厭大量的xml充斥在整個項目中,尤爲是表的mapper.xml,雖然有代碼生成器能夠生成,可是有些複雜的查詢仍是須要手寫配置文件裏的動態sql,這點比較反感(至於爲何反感,也是有多方面緣由的)。mysql
不過多是大量的java開發人員已經被虐慣了,已經習慣各類配置文件,雖然有代碼生成器,可是本人以爲,這種不是真正的路子,按理說開源出去的東西讓別人用的越簡單越爽越好,而不是還須要依賴大量的工具配合才能使用(這個觀點仁者見仁智者見智吧)。spring
由於本人採用的是spring-boot進行開發,自己springboot就提倡採用不用配置自動配置的方式,因此真心但願mybatis(不是mybatis-plus)這點須要繼續努力。sql
基於這點,採用了mybatis-plus裏的已經實現好的方法來進行了取巧的操做,廢話很少說,直接上代碼。數據庫
數據庫是mysql。apache
demo的思路是這樣的:學生表(Student),學生班級表(StudentClass),在學生的實體類裏經過班級Id和班級進行一對一關聯,主要經過mapper接口來實現,基於@Results、@Result、@One(或@Many)、@ResultMap這幾個mybatis裏的註解加上springboot
mybatis-plus裏的BaseMapper、QueryWrapper<ChannelEntity>結合起來實現,因此還得寫一些代碼。mybatis
若是mybatis-plus團隊把關係映射一併實現註解到實體對象上就能省大量代碼了,期待他們早日實現。app
pom文件maven
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>
能夠看到數據庫操做只需依賴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
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 }
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 }
具體的數據庫結構就不放了,參照實體類上註解就很能輕鬆構建出來,由於就兩個表。
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 }
這個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 }
主要關注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 }
最後是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 }
運行測試類若是按照查詢條件插入了多條數據,即年齡是2,班級Id是1的數據,而且班級表裏也插入了數據,結果會查詢出來,而且結果裏Student對象的studentClass屬性是已經賦了值的。
到此此示例結束,但願對你們有所幫助。