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

SpringBoot?整合MyBatis+MyBatis-Plus+MyBatisX插件使用

 更新時(shí)間:2024年04月14日 16:35:18   作者:S-X-S  
本文主要介紹了SpringBoot?整合MyBatis+MyBatis-Plus+MyBatisX插件使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.整合MyBatis

1.需求分析

image-20240317110407816

2.數(shù)據(jù)庫表設(shè)計(jì)

CREATE DATABASE `springboot_mybatis`;

use `springboot_mybatis`;

CREATE TABLE `monster` (
 `id` INT NOT NULL AUTO_INCREMENT,
 `age` INT NOT NULL, 
 `birthday` DATE DEFAULT NULL, 
 `email` VARCHAR(255) DEFAULT NULL,
 `gender` char(1) DEFAULT NULL,
 `name` VARCHAR(255) DEFAULT NULL, 
 `salary` DOUBLE NOT NULL,
 PRIMARY KEY (`id`)
);

SELECT * FROM `monster`;

insert into monster values(null, 20, '2000-11-11', 'nmw@sohu.com', '男', '牛魔王', 5000.88);
insert into monster values(null, 10, '2011-11-11', 'bgj@sohu.com', '女', '白骨精', 2000.00);

3.數(shù)據(jù)庫環(huán)境配置

1.新建maven項(xiàng)目

image-20240317111137777

2.pom.xml 引入依賴

    <!--導(dǎo)入springboot父工程-->
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.5.3</version>
    </parent>

    <!--引入相關(guān)依賴-->
    <dependencies>
        <!--常規(guī)依賴-->
        <!--web場景啟動(dòng)器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--引入測試場景啟動(dòng)器-->
        <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ù)庫配置-->
        <!--引入data-jdbc數(shù)據(jù)源-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <!--mysql依賴使用版本仲裁-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- 引入 druid 依賴 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.17</version>
        </dependency>
        <!--MyBatis場景啟動(dòng)器-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
    </dependencies>

3.application.yml 配置數(shù)據(jù)源

  • 數(shù)據(jù)庫名
  • 用戶名
  • 密碼
  • 驅(qū)動(dòng)是mysql8的(因?yàn)樯厦媸褂昧税姹局俨茫?/li>
server:
  port: 8080
spring:
  datasource: #配置數(shù)據(jù)源
    url: jdbc:mysql://localhost:3306/springboot_mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

4.Application.java 編寫啟動(dòng)類

package com.sun.springboot.mybatis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

5.測試

package com.sun.springboot.mybatis;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@SpringBootTest
public class ApplicationTest {
    //依賴注入
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Test
    public void t1() {
        //查看目前數(shù)據(jù)源
        System.out.println(jdbcTemplate.getDataSource().getClass());
    }
}

image-20240317135531900

6.配置類切換druid數(shù)據(jù)源

package com.sun.springboot.mybatis.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@Configuration
public class DruidDataSourceConfig {

    //注入一個(gè)德魯伊數(shù)據(jù)源
    @ConfigurationProperties("spring.datasource") //讀取yaml配置文件的參數(shù),獲取數(shù)據(jù)源配置
    @Bean
    public DataSource dataSource() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setFilters("stat, wall"); //開啟sql監(jiān)控
        return druidDataSource;
    }

    //配置德魯伊監(jiān)控sql功能
    @Bean
    public ServletRegistrationBean statViewServlet() {
        StatViewServlet statViewServlet = new StatViewServlet();
        ServletRegistrationBean<StatViewServlet> registrationBean =
                new ServletRegistrationBean<>(statViewServlet, "/druid/*");
        //配置登錄監(jiān)控頁面用戶名和密碼
        registrationBean.addInitParameter("loginUsername", "root");
        registrationBean.addInitParameter("loginPassword", "root");
        return registrationBean;
    }

    //配置webStatFilter
    @Bean
    public FilterRegistrationBean webStatFilter() {
        WebStatFilter webStatFilter = new WebStatFilter();
        FilterRegistrationBean<WebStatFilter> filterRegistrationBean =
                new FilterRegistrationBean<>(webStatFilter);
        //默認(rèn)對所有 URL 請求監(jiān)控
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        //排除 URL
        filterRegistrationBean.addInitParameter
                ("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}

7.測試數(shù)據(jù)源是否成功切換

package com.sun.springboot.mybatis;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@SpringBootTest
public class ApplicationTest {
    //依賴注入
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Test
    public void t1() {
        //查看目前數(shù)據(jù)源
        System.out.println(jdbcTemplate.getDataSource().getClass());
    }
}

image-20240317153854191

4.Mybatis基礎(chǔ)配置

1.編寫映射表的bean

package com.sun.springboot.mybatis.bean;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.util.Date;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@Data
public class Monster {
    private Integer id;
    private Integer age;
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date birthday;
    private String email;
    private String name;
    private String gender;
    private Double salary;
}

2.MonsterMapper.java 編寫mapper接口

使用注解注入容器

package com.sun.springboot.mybatis.mapper;

import com.sun.springboot.mybatis.bean.Monster;
import org.apache.ibatis.annotations.Mapper;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@Mapper //將接口注入容器
public interface MonsterMapper {
    public Monster getMonsterById(Integer id);
}

3.MonsterMapper.xml 編寫mapper.xml實(shí)現(xiàn)mapper接口

使用namespace指定要實(shí)現(xiàn)的接口

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--指定要實(shí)現(xiàn)的接口-->
<mapper namespace="com.sun.springboot.mybatis.mapper.MonsterMapper">
    <select id="getMonsterById" resultType="com.sun.springboot.mybatis.bean.Monster" parameterType="Integer">
        select * from monster where id = #{id}
    </select>
</mapper>

image-20240317160222655

4.application.yml 掃描mapper.xml配置文件的位置

掃描類路徑下mapper文件夾下的所有文件

mybatis:
  #指定要掃描的mapper.xml
  mapper-locations: classpath:mapper/*.xml

5.測試

package com.sun.springboot.mybatis;

import com.sun.springboot.mybatis.bean.Monster;
import com.sun.springboot.mybatis.mapper.MonsterMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@SpringBootTest
public class ApplicationTest {
    //依賴注入
    @Resource
    private JdbcTemplate jdbcTemplate;
    //注意這里注入的是MonsterMapper的代理對象
    @Resource
    private MonsterMapper monsterMapper;

    @Test
    public void t1() {
        //查看目前數(shù)據(jù)源
        System.out.println(jdbcTemplate.getDataSource().getClass());
    }

    @Test
    public void t2() {
        //測試mybatis
        Monster monsterById = monsterMapper.getMonsterById(1);
        System.out.println(monsterById);
    }
}

image-20240317162322200

5.MyBatis高級配置

1.方式一:在application.yml中配置mybatis.config-location指定mybatis-config.xml配置文件的位置

2.方式二:直接在application.yml中配置

mybatis:
  #指定要掃描的mapper.xml
  mapper-locations: classpath:mapper/*.xml
  #配置類型別名包,這樣只要在這個(gè)包下的類型都可以簡寫
  type-aliases-package: com/sun/springboot/mybatis/bean
  #輸出日志
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

6.繼續(xù)編寫Service層和Controller層

1.MonsterService.java

package com.sun.springboot.mybatis.service;

import com.sun.springboot.mybatis.bean.Monster;

/**
 * @author 孫顯圣
 * @version 1.0
 */
public interface MonsterService {
    public Monster getMonsterById(Integer id);
}

2.MonsterServiceImpl.java

package com.sun.springboot.mybatis.service.Impl;

import com.sun.springboot.mybatis.bean.Monster;
import com.sun.springboot.mybatis.mapper.MonsterMapper;
import com.sun.springboot.mybatis.service.MonsterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@Service
public class MonsterServiceImpl implements MonsterService {
    @Resource
    private MonsterMapper monsterMapper; //返回代理對象
    @Override
    public Monster getMonsterById(Integer id) {
        return monsterMapper.getMonsterById(id);
    }
}

3.測試

package com.sun.springboot.mybatis;

import com.sun.springboot.mybatis.bean.Monster;
import com.sun.springboot.mybatis.mapper.MonsterMapper;
import com.sun.springboot.mybatis.service.MonsterService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@SpringBootTest
public class ApplicationTest {
    //依賴注入
    @Resource
    private MonsterService monsterService;

    @Test
    public void getMonsterById() {
        Monster monsterById = monsterService.getMonsterById(1);
        System.out.println(monsterById);
    }
}

image-20240317171924248

4.MonsterController.java

package com.sun.springboot.mybatis.Controller;

import com.sun.springboot.mybatis.bean.Monster;
import com.sun.springboot.mybatis.service.MonsterService;
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 javax.annotation.Resource;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@Controller
public class MonsterController {
    @Resource
    private MonsterService monsterService;

    @GetMapping("/getMonster/{id}") //路徑參數(shù)的請求
    @ResponseBody //響應(yīng)一個(gè)json
    public Monster getMonsterById(@PathVariable("id") Integer id) {
        Monster monsterById = monsterService.getMonsterById(id);
        return monsterById;
    }
}

5.測試

image-20240317172341555

6.解決時(shí)間問題

image-20240317172733899

7.完整文件目錄

image-20240317175408170

2.整合MyBatis-Plus

1.MyBatis-Plus基本介紹

image-20240317173128759

2.數(shù)據(jù)庫表設(shè)計(jì)

CREATE DATABASE `springboot_mybatisplus`;

USE `springboot_mybatisplus`;

CREATE TABLE `monster` (
`id` INT NOT NULL AUTO_INCREMENT,
`age` INT NOT NULL, 
`birthday` DATE DEFAULT NULL, 
`email` VARCHAR(255) DEFAULT NULL, 
`gender` CHAR(1) DEFAULT NULL, 
`name` VARCHAR(255) DEFAULT NULL, 
`salary` DOUBLE NOT NULL,
PRIMARY KEY (`id`)
);
SELECT * FROM `monster`;
INSERT INTO monster VALUES(NULL, 20, '2000-11-11', 'xzj@sohu.com', '男', ' 蝎 子 精 ',
15000.88);
INSERT INTO monster VALUES(NULL, 10, '2011-11-11', 'ytj@sohu.com', '女', ' 玉 兔 精 ',
18000.88);

3.數(shù)據(jù)庫環(huán)境配置

1.創(chuàng)建maven項(xiàng)目

image-20240317173858334

2.pom.xml 導(dǎo)入依賴

  <!--導(dǎo)入springboot父工程-->
  <parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>2.5.3</version>
  </parent>

  <!--引入相關(guān)依賴-->
  <dependencies>
    <!--常規(guī)依賴-->
    <!--web場景啟動(dòng)器-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--lombok-->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <!--引入測試場景啟動(dòng)器-->
    <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ù)庫配置-->
    <!--mysql依賴使用版本仲裁-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <!-- 引入 druid 依賴 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.17</version>
    </dependency>
    
    <!--引入MyBatis-Plus場景啟動(dòng)器,會(huì)自動(dòng)引入jdbc和MyBatis-->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.4.3</version>
    </dependency>
  </dependencies>

3.application.yml 配置數(shù)據(jù)源

  • 數(shù)據(jù)庫名稱
  • 用戶名
  • 密碼
server:
  port: 8080
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot_mybatisplus?useSSL=false&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root

4.DruidDataSourceConfig.java 配置類切換druid數(shù)據(jù)源

package com.sun.springboot.mybatisplus.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@Configuration
public class DruidDataSourceConfig {

    //注入一個(gè)德魯伊數(shù)據(jù)源
    @ConfigurationProperties("spring.datasource") //讀取yaml配置文件的參數(shù),獲取數(shù)據(jù)源配置
    @Bean
    public DataSource dataSource() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setFilters("stat, wall"); //開啟sql監(jiān)控
        return druidDataSource;
    }

    //配置德魯伊監(jiān)控sql功能
    @Bean
    public ServletRegistrationBean statViewServlet() {
        StatViewServlet statViewServlet = new StatViewServlet();
        ServletRegistrationBean<StatViewServlet> registrationBean =
                new ServletRegistrationBean<>(statViewServlet, "/druid/*");
        //配置登錄監(jiān)控頁面用戶名和密碼
        registrationBean.addInitParameter("loginUsername", "root");
        registrationBean.addInitParameter("loginPassword", "root");
        return registrationBean;
    }

    //配置webStatFilter
    @Bean
    public FilterRegistrationBean webStatFilter() {
        WebStatFilter webStatFilter = new WebStatFilter();
        FilterRegistrationBean<WebStatFilter> filterRegistrationBean =
                new FilterRegistrationBean<>(webStatFilter);
        //默認(rèn)對所有 URL 請求監(jiān)控
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        //排除 URL
        filterRegistrationBean.addInitParameter
                ("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}

5.編寫啟動(dòng)類Application.java,測試運(yùn)行

package com.sun.springboot.mybatisplus;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

image-20240317175826043

4.MyBatis-Plus基礎(chǔ)配置

1.編寫映射表的bean

package com.sun.springboot.mybatisplus.bean;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.util.Date;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@Data
public class Monster {
    private Integer id;
    private Integer age;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date birthday;
    private String email;
    private String name;
    private String gender;
    private Double salary;
}

2.MonsterMapper.java 編寫Mapper接口

package com.sun.springboot.mybatisplus.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sun.springboot.mybatisplus.bean.Monster;
import org.apache.ibatis.annotations.Mapper;

/**
 * @author 孫顯圣
 * @version 1.0
 */
//直接繼承BaseMapper接口
@Mapper //注入容器
public interface MonsterMapper extends BaseMapper<Monster> {
    //如果提供的方法不夠用再自定義方法
}

3.測試接口方法使用

package com.sun.springboot.mybatisplus;

import com.sun.springboot.mybatisplus.bean.Monster;
import com.sun.springboot.mybatisplus.mapper.MonsterMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@SpringBootTest
public class MonsterMapperTest {
    //注入針對Mapper接口的代理對象
    @Resource
    private MonsterMapper monsterMapper;
    @Test
    public void t1() {
        Monster monster = monsterMapper.selectById(1);
        System.out.println(monster);
    }
}

image-20240317181409483

5.MyBatis-Plus高級配置

application.yml 進(jìn)行配置

#進(jìn)行mybatis-plus配置
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

6.繼續(xù)編寫Service層和Controller層

1.MonsterService.java

package com.sun.springboot.mybatisplus.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.springboot.mybatisplus.bean.Monster;
import com.sun.springboot.mybatisplus.mapper.MonsterMapper;
import com.sun.springboot.mybatisplus.service.MonsterService;
import org.springframework.stereotype.Service;

/**
 * 這里
 * @author 孫顯圣
 * @version 1.0
 */
@Service
public class MonsterServiceImpl extends ServiceImpl<MonsterMapper, Monster> implements MonsterService {
    //自定義方法實(shí)現(xiàn)
}

2.MonsterServiceImpl.java

package com.sun.springboot.mybatisplus.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.springboot.mybatisplus.bean.Monster;
import com.sun.springboot.mybatisplus.mapper.MonsterMapper;
import com.sun.springboot.mybatisplus.service.MonsterService;
import org.springframework.stereotype.Service;

/**
 * 這里
 * @author 孫顯圣
 * @version 1.0
 */
@Service
public class MonsterServiceImpl extends ServiceImpl<MonsterMapper, Monster> implements MonsterService {
    //自定義方法實(shí)現(xiàn)
}

3.測試

package com.sun.springboot.mybatisplus;

import com.sun.springboot.mybatisplus.bean.Monster;
import com.sun.springboot.mybatisplus.mapper.MonsterMapper;
import com.sun.springboot.mybatisplus.service.MonsterService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@SpringBootTest
public class MonsterServiceTest {
    @Resource
    private MonsterService monsterService;
    @Test
    public void t1() {
        Monster byId = monsterService.getById(2);
        System.out.println(byId);
    }
}

image-20240317193644393

4.細(xì)節(jié)說明

  • 簡單來說就是MonsterServiceImpl只需要實(shí)現(xiàn)MonsterService接口的方法
  • 可以調(diào)用IService接口的方法,也可以調(diào)用MonsterService接口的方法

image-20240317193513003

5.MonsterController.java

package com.sun.springboot.mybatisplus.controller;

import com.sun.springboot.mybatisplus.bean.Monster;
import com.sun.springboot.mybatisplus.service.MonsterService;
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 javax.annotation.Resource;

/**
 * @author 孫顯圣
 * @version 1.0
 */
@Controller
public class MonsterController {
    @Resource
    //注入的是MonsterServiceImpl的bean對象,可以直接調(diào)用IService接口的方法
    private MonsterService monsterService;
    @GetMapping("/getMonster/{id}")
    @ResponseBody
    public Monster getMonsterById(@PathVariable("id") Integer id) {
        Monster byId = monsterService.getById(id);
        return byId;
    }
}

image-20240317194934543

7.細(xì)節(jié)說明

1.@MapperScan 掃描包下的所有Mapper

啟動(dòng)類配置注解

image-20240317200417488

2.@TableName bean的類名與表名不一致時(shí)使用

image-20240317200951971

3.MyBatis引入了哪些依賴

image-20240317201044828

8.MyBatisX快速開發(fā)

1.安裝插件

image-20240317201658927

2.使用方式

1.挑一個(gè)帶小鳥的方法

image-20240317203048085

2.直接alt + Enter

image-20240317203124474

3.生成sql語句

image-20240317203146538

4.查看生成的方法

image-20240317203209726

5.點(diǎn)擊左邊的小鳥就可以直接跳轉(zhuǎn)到指定方法或者xml

image-20240317203629654

image-20240317203639228

9.完整文件目錄

image-20240317203723886

10.MyBatis-Plus小結(jié)

image-20240317203958723

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

相關(guān)文章

  • jackson在springboot中的使用方式-自定義參數(shù)轉(zhuǎn)換器

    jackson在springboot中的使用方式-自定義參數(shù)轉(zhuǎn)換器

    這篇文章主要介紹了jackson在springboot中的使用方式-自定義參數(shù)轉(zhuǎn)換器,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java中l(wèi)ong類型與Long類型的區(qū)別和大小比較詳解

    Java中l(wèi)ong類型與Long類型的區(qū)別和大小比較詳解

    這篇文章主要給大家介紹了Java中l(wèi)ong類型與Long類型區(qū)別和大小比較的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • springboot整合mqtt客戶端示例分享

    springboot整合mqtt客戶端示例分享

    這篇文章主要介紹了springboot整合mqtt客戶端示例分享的相關(guān)資料,需要的朋友可以參考下
    2023-07-07
  • Java中?!=null?的判斷

    Java中?!=null?的判斷

    空指針異常是出現(xiàn)頻率比較高的bug,在出現(xiàn)空指針時(shí),很多小伙伴都是習(xí)慣性地加一個(gè)?!=null?的判斷,本文就來介紹一下如何使用,給感興趣的可以了解下
    2025-02-02
  • java生成mvt切片的方法實(shí)現(xiàn)

    java生成mvt切片的方法實(shí)現(xiàn)

    本文主要介紹了java生成mvt切片的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 詳解Java如何判斷ResultSet結(jié)果集是否為空

    詳解Java如何判斷ResultSet結(jié)果集是否為空

    ResultSet 表示 select 語句的查詢結(jié)果集。這篇文章主要為大家詳細(xì)介紹了Java如何判斷ResultSet結(jié)果集是否為空,感興趣的可以了解一下
    2023-02-02
  • java編寫的簡單移動(dòng)方塊小游戲代碼

    java編寫的簡單移動(dòng)方塊小游戲代碼

    這篇文章主要介紹了java編寫的簡單移動(dòng)方塊小游戲代碼,涉及Java簡單圖形繪制與事件響應(yīng)的相關(guān)技巧,需要的朋友可以參考下
    2015-12-12
  • 簡單了解Java synchronized關(guān)鍵字同步

    簡單了解Java synchronized關(guān)鍵字同步

    這篇文章主要介紹了簡單了解Java synchronized關(guān)鍵字同步,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Java?Deque基本概念和使用方法

    Java?Deque基本概念和使用方法

    Deque雙端隊(duì)列是Java?Collections?Framework的一部分,支持在兩端插入和刪除操作,它繼承自Queue接口,可以作為隊(duì)列FIFO或棧LIFO使用,本文介紹java?Deque基本概念和使用方法,感興趣的朋友一起看看吧
    2025-03-03
  • 基于Java方式實(shí)現(xiàn)數(shù)據(jù)同步

    基于Java方式實(shí)現(xiàn)數(shù)據(jù)同步

    這篇文章主要為大家詳細(xì)介紹了基于Java方式實(shí)現(xiàn)數(shù)據(jù)同步,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08

最新評論