欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot整合mybatis-plus基于注解實現(xiàn)一對一(一對多)查詢功能

 更新時間:2021年09月15日 16:23:05   作者:wenwuxianren  
這篇文章主要介紹了springboot整合mybatis-plus基于純注解實現(xiàn)一對一(一對多)查詢功能,因為本人采用的是spring-boot進(jìn)行開發(fā),本身springboot就提倡采用不用配置自動配置的方式,所以真心希望mybatis(不是mybatis-plus)這點需要繼續(xù)努力

因為目前所用mybatis-plus版本為3.1.1,感覺是個半成品,所有在實體類上的注解只能支持單表,沒有一對一和一對多關(guān)系映射,且該功能還在開發(fā)中,相信mybatis-plus開發(fā)團(tuán)隊在不久的將來應(yīng)該會實現(xiàn)此功能。

由于本人開發(fā)習(xí)慣的原因,實在是太討厭大量的xml充斥在整個項目中,尤其是表的mapper.xml,雖然有代碼生成器可以生成,但是有些復(fù)雜的查詢還是需要手寫配置文件里的動態(tài)sql,這點比較反感(至于為什么反感,也是有多方面原因的)。

不過可能是大量的java開發(fā)人員已經(jīng)被虐慣了,已經(jīng)習(xí)慣各種配置文件,雖然有代碼生成器,但是本人覺得,這種不是真正的路子,按理說開源出去的東西讓別人用的越簡單越爽越好,而不是還需要依賴大量的工具配合才能使用(這個觀點仁者見仁智者見智吧)。

因為本人采用的是spring-boot進(jìn)行開發(fā),本身springboot就提倡采用不用配置自動配置的方式,所以真心希望mybatis(不是mybatis-plus)這點需要繼續(xù)努力。

基于這點,采用了mybatis-plus里的已經(jīng)實現(xiàn)好的方法來進(jìn)行了取巧的操作,廢話不多說,直接上代碼。

數(shù)據(jù)庫是mysql。

demo的思路是這樣的:學(xué)生表(Student),學(xué)生班級表(StudentClass),在學(xué)生的實體類里通過班級Id和班級進(jìn)行一對一關(guān)聯(lián),主要通過mapper接口來實現(xiàn),基于@Results、@Result、@One(或@Many)、@ResultMap這幾個mybatis里的注解加上

mybatis-plus里的BaseMapper、QueryWrapper<ChannelEntity>結(jié)合起來實現(xiàn),所以還得寫一些代碼。

如果mybatis-plus團(tuán)隊把關(guān)系映射一并實現(xiàn)注解到實體對象上就能省大量代碼了,期待他們早日實現(xiàn)。

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lyb</groupId>
    <artifactId>spring-mybatis-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>spring-mybatis-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        -->

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.1.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

可以看到數(shù)據(jù)庫操作只需依賴mybatis-plus-boot-starter

application.yml文件

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
    username: root
    password: liuyabin
    driver-class-name: com.mysql.jdbc.Driver
mybatis-plus:
  type-aliases-package: com.lyb.springmybatisdemo.entity
  type-aliases-super-type: java.lang.Object

Student實體類代碼,可以看到實體類上加上了mybatis-plus的注解,這里也加了@Data lombok的注解為了節(jié)省多敲代碼:

package com.lyb.springmybatisdemo.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;

@Data
@Builder
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "student")
public class Student {
    @TableId(value = "Id",type = IdType.AUTO)
    private int id;
    @TableField(value = "SName",exist = true,select = true)
    private String name;
    @TableField(value = "Age")
    private int age;
    @TableField(value = "ClassId")
    private int classId;
    @TableField(exist = false)
    private StudentClass studentClass;
}

StudentClass類代碼:

package com.lyb.springmybatisdemo.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;

@Data
@Builder
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "stuClass")
public class StudentClass {
    @TableId(value = "ClassId",type = IdType.AUTO)
    private int classId;
    @TableField(value = "ClassName")
    private String className;
}

具體的數(shù)據(jù)庫結(jié)構(gòu)就不放了,參照實體類上注解就很能輕松構(gòu)建出來,因為就兩個表。

StudentClassMapper類代碼:

package com.lyb.springmybatisdemo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lyb.springmybatisdemo.entity.StudentClass;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface StudentClassMapper extends BaseMapper<StudentClass> {
}

這個mapper很簡單,因為mybatis-plus里的BaseMapper已經(jīng)將基礎(chǔ)的單表操作給做了,并且該表沒有一對多的需求,如果現(xiàn)實中確實有一對多的需求的話,可以參照下面一對一的方式實現(xiàn)。

StudentMapper代碼:

package com.lyb.springmybatisdemo.mapper;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lyb.springmybatisdemo.entity.Student;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StudentMapper extends BaseMapper<Student> {
    @Results(id="stuMap",value = {
            @Result(property = "id",column = "Id"),
            @Result(property = "name",column = "SName"),
            @Result(property = "age",column = "Age"),
            @Result(property = "classId",column = "ClassID"),
            @Result(property = "studentClass",column = "ClassID",one = @One(select = "com.lyb.springmybatisdemo.mapper.StudentClassMapper.selectById"))
    })
    @Select("SELECT * FROM STUDENT WHERE ID=#{id}")
    Student findStudentById(int id);

    @Select("select * from Student where 1=1 and " +
            "${ew.sqlSegment}")
    @ResultMap(value = "stuMap")
    List<Student> selectStudents(@Param("ew") QueryWrapper<Student> wrapper);


}

主要關(guān)注selectStudents方法的代碼。id="stuMap"的@Results里定義了一對一的關(guān)系,可以看到有一個@One(select = "com.lyb.springmybatisdemo.mapper.StudentClassMapper.selectById")的定義,該方法并沒有在

StudentClassMapper里實現(xiàn),而是mybatis-plus在BaseMapper里幫我們實現(xiàn)了,那就可以直接方便的拿過來用了,省了我們多謝一個方法的代碼了。

selectStudents方法傳入的參數(shù)QueryWrapper<Student> wrapper可以充分利用lambda來自己構(gòu)建各種各樣的where條件,而且注意該方法的@Select的類SQL的寫法,這樣不會出錯。

接下來是就往兩張表里插入多條數(shù)據(jù),然后是測試代碼:

package com.lyb.springmybatisdemo;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyb.springmybatisdemo.entity.Student;
import com.lyb.springmybatisdemo.mapper.StudentMapper;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringMybatisDemoApplicationTests {

    @Autowired
    private StudentMapper studentMapper;


    @Test
    public  void testByWrapper(){
        QueryWrapper<Student> queryWrapper = new QueryWrapper<Student>();
        queryWrapper.lambda().eq(Student::getAge,2)
                            .eq(Student::getClassId,1);
        List<Student> students = studentMapper.selectStudents(queryWrapper);
    }
}

最后是springboot的啟動類

package com.lyb.springmybatisdemo;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.lyb.springmybatisdemo.mapper")
public class SpringMybatisDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringMybatisDemoApplication.class, args);
    }

}

運行測試類如果按照查詢條件插入了多條數(shù)據(jù),即年齡是2,班級Id是1的數(shù)據(jù),并且班級表里也插入了數(shù)據(jù),結(jié)果會查詢出來,并且結(jié)果里Student對象的studentClass屬性是已經(jīng)賦了值的。

到此這篇關(guān)于springboot整合mybatis-plus基于注解實現(xiàn)一對一(一對多)查詢功能的文章就介紹到這了,更多相關(guān)springboot整合mybatis-plus內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot自動配置的原理詳解

    SpringBoot自動配置的原理詳解

    這篇文章主要介紹了SpringBoot自動配置的原理詳解,本節(jié)更詳細(xì)地介紹了如何使用 Spring Boot,它涵蓋了諸如構(gòu)建系統(tǒng)、自動配置以及如何運行應(yīng)用程序等主題,我們還介紹了一些 Spring Boot 最佳實踐,需要的朋友可以參考下
    2023-09-09
  • Java中字符串中連續(xù)相同字符去重方法

    Java中字符串中連續(xù)相同字符去重方法

    今天小編就為大家分享一篇Java中字符串中連續(xù)相同字符去重方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • SpringBoot項目使用jasypt加解密的方法

    SpringBoot項目使用jasypt加解密的方法

    jasypt是一個通用的加解密庫,我們可以使用它在配置文件中對數(shù)據(jù)庫密碼進(jìn)行加密,以確保其安全性,接下來通過本文給大家介紹SpringBoot項目使用jasypt加解密的方法,感興趣的朋友一起看看吧
    2022-05-05
  • Mybatis統(tǒng)計sql運行時間的兩種方式

    Mybatis統(tǒng)計sql運行時間的兩種方式

    這篇文章主要介紹了Mybatis統(tǒng)計sql運行時間的方案,Spring?Boot?+?Mybatis?web項目,統(tǒng)計sql運行時間,用于分析慢sql,優(yōu)化系統(tǒng)速度,方案有兩種:自定義實現(xiàn)?Interceptor和使用現(xiàn)有依賴庫(Druid),文中通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下
    2024-11-11
  • 深入解析Spring?AI框架如何在Java應(yīng)用中實現(xiàn)智能化交互的關(guān)鍵

    深入解析Spring?AI框架如何在Java應(yīng)用中實現(xiàn)智能化交互的關(guān)鍵

    本文詳細(xì)介紹了SpringAI框架在Java應(yīng)用中的應(yīng)用,包括實體類映射、函數(shù)回調(diào)等核心功能的實現(xiàn),通過源碼分析,幫助開發(fā)者更好地理解和使用這些高級特性,提升業(yè)務(wù)效率,感興趣的朋友跟隨小編一起看看吧
    2024-11-11
  • Spring-Boot 集成Solr客戶端的詳細(xì)步驟

    Spring-Boot 集成Solr客戶端的詳細(xì)步驟

    本篇文章主要介紹了Spring-Boot 集成Solr客戶端的詳細(xì)步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Java實現(xiàn)微信公眾號自定義菜單的創(chuàng)建方法示例

    Java實現(xiàn)微信公眾號自定義菜單的創(chuàng)建方法示例

    這篇文章主要介紹了Java實現(xiàn)微信公眾號自定義菜單的創(chuàng)建方法,結(jié)合實例形式分析了java創(chuàng)建微信公眾號自定義菜單的具體步驟、實現(xiàn)方法及相關(guān)操作注意事項,需要的朋友可以參考下
    2019-10-10
  • Java實現(xiàn)文件批量重命名,移動和刪除

    Java實現(xiàn)文件批量重命名,移動和刪除

    這篇文章主要為大家介紹了如何利用Java語言實現(xiàn)批量重命名,批量移動文件,批量刪除tmp文件等功能,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-08-08
  • springboot打成jar后獲取classpath下文件失敗的解決方案

    springboot打成jar后獲取classpath下文件失敗的解決方案

    這篇文章主要介紹了使用springboot打成jar后獲取classpath下文件失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 教你用Java Swing做一個定時提醒工具

    教你用Java Swing做一個定時提醒工具

    今天給大家?guī)淼氖荍ava的相關(guān)知識,文章圍繞著如何用Java做一個定時提醒工具展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06

最新評論