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

SpringBoot整合Mybatis-plus的具體過程使用

 更新時間:2022年06月27日 08:36:12   作者:鳴鼓ming  
這篇文章主要介紹了SpringBoot?整合mybatis+mybatis-plus的步驟,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

1.MyBatisX插件

在使用mybatis或者mybatis-plus時,我們可以安裝IDEA的MyBatis的插件 - MyBatisX,

這樣我們就可以實現(xiàn)點擊接口跳轉(zhuǎn)到sql文件, 點擊sql文件可以跳轉(zhuǎn)到接口的功能, 很方便.這個插件的功能還有很多, 可以查看MyBatis-Plus官網(wǎng)

安裝方法:打開 IDEA,進入 File -> Settings -> Plugins -> Browse Repositories,輸入 mybatisx 搜索并安裝, 然后重啟IDEA。

點擊小鳥可實現(xiàn)跳轉(zhuǎn)

2.引入依賴

在pom.xml添加如下依賴

 <!--數(shù)據(jù)庫相關(guān)-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.17</version>
        </dependency>

3.編寫配置

application.properties

server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/db_springtest?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#----------------------------配置mybatis-plus---------------------------
#配置sql文件路徑
mybatis-plus.mapper-locations=classpath:/mapper/*.xml
#開啟駝峰命名映射
mybatis-plus.configuration.map-underscore-to-camel-case=true
## 自定義sql中表名帶前綴, 默認(rèn)是實體名的小寫, 如user, 但是數(shù)據(jù)庫中是t_user, 所以設(shè)置加上前綴
mybatis-plus.global-config.db-config.table-prefix=t_

#----------------------------配置druid--------------------------------
#監(jiān)控SpringBean
spring.datasource.druid.aop-patterns=com.limi.springboottest2.*  
# 底層開啟功能,stat(sql監(jiān)控),wall(防火墻)
spring.datasource.druid.filters=stat,wall

# 配置監(jiān)控頁功能
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=123456
spring.datasource.druid.stat-view-servlet.reset-enable=false

# 監(jiān)控web
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions='*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'

# 對上面filters里面的stat的詳細(xì)配置
spring.datasource.druid.filter.stat.slow-sql-millis=1000
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.enabled=true
spring.datasource.druid.filter.wall.enabled=true
spring.datasource.druid.filter.wall.config.drop-table-allow=false

4.編寫接口

UserMapper

package com.limi.springboottest2.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.limi.springboottest2.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
    User getUserById(Integer id);
}

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
        namespace:必須有值,自定義的唯一字符串
        推薦使用:dao 接口的全限定名稱 -->
<mapper namespace="com.limi.springboottest2.mapper.UserMapper">
    <select id="getUserById" resultType="com.limi.springboottest2.entity.User">
        select * from t_user where id = #{id}
    </select>
</mapper>

注意要開啟mapper接口文件的掃描

SpringbootTest2Application

package com.limi.springboottest2;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
@MapperScan("com.limi.springboottest2.mapper")//掃描mapper
public class SpringbootTest2Application {
    public static void main(String[] args) {
        //1、返回我們IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(SpringbootTest2Application.class, args);
    }
}

5.運行測試

HelloController

package com.limi.springboottest2.controller;
import com.limi.springboottest2.entity.User;
import com.limi.springboottest2.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class HelloController {
    @Autowired
    private UserMapper userMapper;
    //測試自己編寫sql語句的查詢方法
    @ResponseBody
    @GetMapping("/getUser/{id}")
    public User test1(@PathVariable("id") Integer id){
        User user = userMapper.getUserById(id);
        return user;
    }
    //測試mybatis-plus內(nèi)置的查詢方法
    @ResponseBody
    @GetMapping("/getAllUser")
    public List<User> test2(){
        List<User> userList = userMapper.selectList(null);//查詢所有
        return userList;
    }
}

6.完整代碼

pom.xml

<?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 https://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.7.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.limi</groupId>
    <artifactId>springboot-test2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-test2</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!--數(shù)據(jù)庫相關(guān)-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.17</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <!-- 下面插件作用是工程打包時,不將spring-boot-configuration-processor打進包內(nèi),讓其只在編碼的時候有用 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

application.properties

server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/db_springtest?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#----------------------------配置mybatis-plus---------------------------
#配置sql文件路徑
mybatis-plus.mapper-locations=classpath:/mapper/*.xml
#開啟駝峰命名映射
mybatis-plus.configuration.map-underscore-to-camel-case=true
## 自定義sql中表名帶前綴, 默認(rèn)是實體名的小寫, 如user, 但是數(shù)據(jù)庫中是t_user, 所以設(shè)置加上前綴
mybatis-plus.global-config.db-config.table-prefix=t_

#----------------------------配置druid--------------------------------
#監(jiān)控SpringBean
spring.datasource.druid.aop-patterns=com.limi.springboottest2.*  
# 底層開啟功能,stat(sql監(jiān)控),wall(防火墻)
spring.datasource.druid.filters=stat,wall

# 配置監(jiān)控頁功能
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=123456
spring.datasource.druid.stat-view-servlet.reset-enable=false

# 監(jiān)控web
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions='*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'

# 對上面filters里面的stat的詳細(xì)配置
spring.datasource.druid.filter.stat.slow-sql-millis=1000
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.enabled=true
spring.datasource.druid.filter.wall.enabled=true
spring.datasource.druid.filter.wall.config.drop-table-allow=false

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
        namespace:必須有值,自定義的唯一字符串
        推薦使用:dao 接口的全限定名稱 -->
<mapper namespace="com.limi.springboottest2.mapper.UserMapper">
    <select id="getUserById" resultType="com.limi.springboottest2.entity.User">
        select * from t_user where id = #{id}
    </select>
</mapper>

SpringbootTest2Application

package com.limi.springboottest2;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
@MapperScan("com.limi.springboottest2.mapper")//掃描mapper
public class SpringbootTest2Application {
    public static void main(String[] args) {
        //1、返回我們IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(SpringbootTest2Application.class, args);
    }
}

User

package com.limi.springboottest2.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {
    private Integer id;
    private String userName;
    private String passWord;
}

UserMapper

package com.limi.springboottest2.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {
    private Integer id;
    private String userName;
    private String passWord;
}

HelloController

package com.limi.springboottest2.controller;
import com.limi.springboottest2.entity.User;
import com.limi.springboottest2.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class HelloController {
    @Autowired
    private UserMapper userMapper;
    //測試自己編寫sql語句的查詢方法
    @ResponseBody
    @GetMapping("/getUser/{id}")
    public User test1(@PathVariable("id") Integer id){
        User user = userMapper.getUserById(id);
        return user;
    }
    //測試mybatis-plus內(nèi)置的查詢方法
    @ResponseBody
    @GetMapping("/getAllUser")
    public List<User> test2(){
        List<User> userList = userMapper.selectList(null);//查詢所有
        return userList;
    }
}

到此這篇關(guān)于SpringBoot整合Mybatis-plus的具體過程使用的文章就介紹到這了,更多相關(guān)SpringBoot整合mybatis-plus內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot整合Mybatis-Plus實現(xiàn)微信注冊登錄的示例代碼

    SpringBoot整合Mybatis-Plus實現(xiàn)微信注冊登錄的示例代碼

    微信是不可或缺的通訊工具,本文主要介紹了SpringBoot整合Mybatis-Plus實現(xiàn)微信注冊登錄的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • Java日期時間字符串和毫秒相互轉(zhuǎn)換的方法

    Java日期時間字符串和毫秒相互轉(zhuǎn)換的方法

    這篇文章主要為大家詳細(xì)介紹了Java日期時間字符串和毫秒相互轉(zhuǎn)換的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 25個最好的免費Eclipse插件

    25個最好的免費Eclipse插件

    這篇文章為大家分享了25個讓Java程序員更高效的Eclipse插件,感興趣的朋友可以參考一下
    2015-12-12
  • Java排序算法總結(jié)之冒泡排序

    Java排序算法總結(jié)之冒泡排序

    這篇文章主要介紹了Java排序算法總結(jié)之冒泡排序,較為詳細(xì)的分析了冒泡排序的原理與java實現(xiàn)技巧,需要的朋友可以參考下
    2015-05-05
  • springboot整合logback實現(xiàn)日志管理操作

    springboot整合logback實現(xiàn)日志管理操作

    本章節(jié)是記錄logback在springboot項目中的簡單使用,本文將會演示如何通過logback將日志記錄到日志文件或輸出到控制臺等管理操作,感興趣的朋友跟隨小編一起看看吧
    2024-02-02
  • 淺析SpringBoot多數(shù)據(jù)源實現(xiàn)方案

    淺析SpringBoot多數(shù)據(jù)源實現(xiàn)方案

    現(xiàn)在很多項目的開發(fā)過程中,可能涉及到多個數(shù)據(jù)源,像讀寫分離的場景,或者因為業(yè)務(wù)復(fù)雜,導(dǎo)致不同的業(yè)務(wù)部署在不同的數(shù)據(jù)庫上,那么這樣的場景,我們應(yīng)該如何在代碼中簡潔方便的切換數(shù)據(jù)源呢,本文介紹SpringBoot多數(shù)據(jù)源實現(xiàn)方案,感興趣的朋友跟隨小編一起看看吧
    2024-02-02
  • WxJava微信公眾號開發(fā)入門實戰(zhàn)

    WxJava微信公眾號開發(fā)入門實戰(zhàn)

    本文主要介紹了WxJava微信公眾號開發(fā)入門實戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Mybatis批量插入返回插入成功后的主鍵id操作

    Mybatis批量插入返回插入成功后的主鍵id操作

    這篇文章主要介紹了Mybatis批量插入返回插入成功后的主鍵id操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Mybatis使用JSONObject接收數(shù)據(jù)庫查詢的方法

    Mybatis使用JSONObject接收數(shù)據(jù)庫查詢的方法

    這篇文章主要介紹了Mybatis使用JSONObject接收數(shù)據(jù)庫查詢,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • JAVA如何轉(zhuǎn)換樹結(jié)構(gòu)數(shù)據(jù)代碼實例

    JAVA如何轉(zhuǎn)換樹結(jié)構(gòu)數(shù)據(jù)代碼實例

    這篇文章主要介紹了JAVA如何轉(zhuǎn)換樹結(jié)構(gòu)數(shù)據(jù)代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03

最新評論