最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot整合MyBatisPlus詳解

 更新時(shí)間:2023年04月18日 09:57:36   作者:Azy0619  
這篇文章詳細(xì)介紹了SpringBoot整合mybatisplus的全過程,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,需要的朋友可以參考一下

1.什么是springboot自動(dòng)裝配?

自動(dòng)裝配是springboot的核心,一般提到自動(dòng)裝配就會(huì)和springboot聯(lián)系在一起。實(shí)際上 Spring Framework 早就實(shí)現(xiàn)了這個(gè)功能。Spring Boot 只是在其基礎(chǔ)上,通過 SPI 的方式,做了進(jìn)一步優(yōu)化。

SpringBoot 定義了一套接口規(guī)范,這套規(guī)范規(guī)定:SpringBoot 在啟動(dòng)時(shí)會(huì)掃描外部引用 jar 包中的META-INF/spring.factories文件,將文件中配置的類型信息加載到 Spring 容器(此處涉及到 JVM 類加載機(jī)制與 Spring 的容器知識(shí)),并執(zhí)行類中定義的各種操作。對于外部 jar 來說,只需要按照 SpringBoot 定義的標(biāo)準(zhǔn),就能將自己的功能裝置進(jìn) SpringBoot

2.springboot注解:

@EnableAutoConfiguration:掃包范圍默認(rèn)當(dāng)前類。
@ComponentScan(" “) 掃包范圍默認(rèn)當(dāng)前類所在的整個(gè)包下面所有類。
掃包范圍大于@EnableAutoConfiguration,@ComponentScan(” ")依賴于@EnableAutoConfiguration啟動(dòng)程序。
@EnableAutoConfiguration
@ComponentScan("第三方包 ")
app.run()
@SpringBootApplication 掃包范圍同級(jí)包和當(dāng)前包。
@SpringBootApplication 底層等同于@EnableAutoConfiguration+@ComponentScan。不掃描第三方包

3.springboot整合mybatisplus實(shí)現(xiàn)增刪改查

1.首先導(dǎo)入相關(guān)依賴

   <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.9.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.7.8</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </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>
    </dependencies>

2.創(chuàng)建數(shù)據(jù)表并添加數(shù)據(jù)

DROP TABLE IF EXISTS `category`;
CREATE TABLE `category`  (
  `cid` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `cname` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`cid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = DYNAMIC;

-- ----------------------------
-- Records of category
-- ----------------------------
INSERT INTO `category` VALUES ('c001', '家電');
INSERT INTO `category` VALUES ('c002', '鞋服');
INSERT INTO `category` VALUES ('c003', '化妝品');
INSERT INTO `category` VALUES ('c004', '汽車');

-- ----------------------------
-- Table structure for products
-- ----------------------------
DROP TABLE IF EXISTS `products`;
CREATE TABLE `products`  (
  `pid` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `pname` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `price` int NULL DEFAULT NULL,
  `flag` varchar(2) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `category_id` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`pid`) USING BTREE,
  INDEX `category_id`(`category_id`) USING BTREE,
  CONSTRAINT `products_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `category` (`cid`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = DYNAMIC;

-- ----------------------------
-- Records of products
-- ----------------------------
INSERT INTO `products` VALUES ('p001', '小\r\n米電視 機(jī)', 5000, '1', 'c001');
INSERT INTO `products` VALUES ('p002', '格\r\n力空調(diào)', 3000, '1', 'c001');
INSERT INTO `products` VALUES ('p003', '美\r\n的冰箱', 4500, '1', 'c001');
INSERT INTO `products` VALUES ('p004', '籃\r\n球鞋', 800, '1', 'c002');
INSERT INTO `products` VALUES ('p005', '運(yùn)\r\n動(dòng)褲', 200, '1', 'c002');
INSERT INTO `products` VALUES ('p006', 'T\r\n恤', 300, '1', 'c002');
INSERT INTO `products` VALUES ('p009', '籃球', 188, '1', 'c002');

3.在項(xiàng)目中創(chuàng)建如下目錄

4.在resources下創(chuàng)建application.propertis并配置數(shù)據(jù)源

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql:///springboot

#日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

mybatis.mapper-locations=classpath:mapper/*.xml



5.在pojo下創(chuàng)建實(shí)體類

package com.azy.pojo;

import java.io.Serializable;

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.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * products
 * @author 
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "products")
public class Products implements Serializable {
    @TableId(type = IdType.AUTO)
    private String pid;

    public Products(String pid, String pname, Integer price) {
        this.pid = pid;
        this.pname = pname;
        this.price = price;
    }

    private String pname;

    private Integer price;

    private String flag;

    private String category_id;
    @TableField(exist = false)
    private Category category;

    private static final long serialVersionUID = 1L;
}

6.在dao層下創(chuàng)建ProductDao這個(gè)接口,由于mybatisplus封裝了單表的增刪改查,所有只需繼承BaseMapper這個(gè)接口就ok

package com.azy.dao;

import com.azy.pojo.Products;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;

/**
 * @ fileName:ProductsDao
 * @ description:
 * @ author:Azy
 * @ createTime:2023/4/11 18:57
 * @ version:1.0.0
 */
public interface ProductsDao extends BaseMapper<Products> {
    IPage<Products> findPage(IPage<Products> iPage, @Param("ew") Wrapper<Products> wrapper);
}

7.在resources下面的mapper下創(chuàng)建ProductsDao.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">
<mapper namespace="com.azy.dao.ProductsDao">
    <resultMap id="ProductsMap" type="com.azy.pojo.Products" autoMapping="true">
        <id property="pid" column="pid" jdbcType="VARCHAR"/>
        <result property="pname" column="pname" jdbcType="VARCHAR"/>
        <result property="price" column="price" jdbcType="INTEGER"/>
        <result property="flag" column="flag" jdbcType="VARCHAR"/>
        <result property="category_id" column="category_id" jdbcType="VARCHAR"/>
        <association property="category" javaType="com.azy.pojo.Category" autoMapping="true">
            <id column="cid" property="cid" jdbcType="VARCHAR"/>
            <result property="cname" column="cname" jdbcType="VARCHAR"/>
        </association>
    </resultMap>

    <select id="findPage" resultType="com.azy.pojo.Products" resultMap="ProductsMap">
        select *
        from products p
        join category c
        on p.category_id=c.cid
        <if test="ew!=null">
            <where>
                 ${ew.sqlSegment}
            </where>
        </if>
    </select>

</mapper>

8.在test包下的測試類中測試

package com.azy;

import com.azy.dao.ProductsDao;
import com.azy.dao.UserDao;
import com.azy.pojo.Products;
import com.azy.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;

@SpringBootTest
class DemoApplicationTests {
    @Resource
    private UserDao userDao;
    @Test
    void contextLoads() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.likeRight("name","_z");
        wrapper.or();
        wrapper.between("age",10,20);
        wrapper.orderByDesc("age");
        wrapper.select("name","age");
        List<User> users = userDao.selectList(wrapper);
        users.forEach(System.out::println);
    }
    @Test
    public void delete(){
        System.out.println(userDao.deleteById(5));
    }
    @Test
    public void insert(){
        User user = new User();
        user.setAge(18);
        user.setName("cxk");
        user.setEmail("123@qq.com");
        System.out.println(userDao.insert(user));
    }
    @Test
    public void update(){
        User user = new User();
        user.setAge(19);
        user.setName("azy");
        user.setEmail("321@qq.com");
        user.setId(6L);
        System.out.println(userDao.updateById(user));
    }
    @Test
    public void selectById(){
        System.out.println(userDao.selectById(4));
    }
    @Test
    public void testPage(){
        Page<User> page = new Page<>(1, 3);//current:當(dāng)前第幾頁 size:每頁顯示條數(shù)
        userDao.selectPage(page,null);//把查詢分頁的結(jié)構(gòu)封裝到page對象中
        System.out.println("當(dāng)前頁的記錄"+page.getRecords());//獲取當(dāng)前頁的記錄
        System.out.println("獲取總頁數(shù)"+page.getPages());//獲取當(dāng)前頁的總頁數(shù)
        System.out.println("獲取總條數(shù)"+page.getTotal());//獲取當(dāng)前頁的記錄
    }

//    ==============================================================
    @Resource
    private ProductsDao productsDao;
    @Test
    public void testQueryProductById(){
        System.out.println(productsDao.selectById("p008"));
    }

    @Test
    public void testDelete(){
        System.out.println(productsDao.deleteById("p008"));
    }

    @Test
    public void testInsert(){
        Products products = new Products();
        products.setPname("滑板鞋");
        products.setFlag("2");
        products.setPrice(8888);
        products.setCategory_id("c002");
        products.setPid("p009");
        System.out.println(productsDao.insert(products));
    }
    @Test
    public void testUpdate(){
        Products products = new Products();
        products.setPname("籃球");
        products.setFlag("1");
        products.setPrice(188);
        products.setCategory_id("c002");
        products.setPid("p009");
        System.out.println(productsDao.updateById(products));
    }
    @Test
    public void testProductsPage(){
        Page<Products> page = new Page<>(1, 3);//current:當(dāng)前第幾頁 size:每頁顯示條數(shù)
        productsDao.selectPage(page,null);//把查詢分頁的結(jié)構(gòu)封裝到page對象中
        System.out.println("當(dāng)前頁的記錄"+page.getRecords());//獲取當(dāng)前頁的記錄
        System.out.println("獲取總頁數(shù)"+page.getPages());//獲取當(dāng)前頁的總頁數(shù)
        System.out.println("獲取總條數(shù)"+page.getTotal());//獲取當(dāng)前頁的記錄
    }
    @Test
    public void testProductsPage2(){
        Page<Products> page=new Page<>(1,3);
        QueryWrapper<Products> wrapper=new QueryWrapper<>();
        wrapper.gt("price",1000);
        productsDao.findPage(page,wrapper);
        System.out.println("當(dāng)前頁的記錄"+page.getRecords());//獲取當(dāng)前頁的記錄
        System.out.println("獲取總頁數(shù)"+page.getPages());//獲取當(dāng)前頁的記錄
        System.out.println("獲取總條數(shù)"+page.getTotal());//獲取當(dāng)前頁的記錄
    }
}

9.最后運(yùn)行測試類

測試完成

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

相關(guān)文章

  • 從零開始講解Java微信公眾號(hào)消息推送實(shí)現(xiàn)

    從零開始講解Java微信公眾號(hào)消息推送實(shí)現(xiàn)

    微信公眾號(hào)分為訂閱號(hào)和服務(wù)號(hào),無論有沒有認(rèn)證,訂閱號(hào)每天都能推送一條消息,也就是每天只能推送一次消息給粉絲,這篇文章主要給大家介紹了關(guān)于Java微信公眾號(hào)消息推送實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • IDEA加載項(xiàng)目沒有src目錄的問題及解決

    IDEA加載項(xiàng)目沒有src目錄的問題及解決

    這篇文章主要介紹了IDEA加載項(xiàng)目沒有src目錄的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Spring中@Service注解的作用與@Controller和@RestController之間區(qū)別

    Spring中@Service注解的作用與@Controller和@RestController之間區(qū)別

    這篇文章主要介紹了Spring中@Service注解的作用與@Controller和@RestController之間的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-03-03
  • Java處理XSS漏洞的四種方法小結(jié)

    Java處理XSS漏洞的四種方法小結(jié)

    本文主要介紹了Java處理XSS漏洞的四種方法小結(jié),包含使用HTML實(shí)體編碼、使用內(nèi)容安全策略(CSP)、使用框架內(nèi)置的XSS防護(hù)和自定義過濾器等方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-05-05
  • Java定時(shí)任務(wù)實(shí)現(xiàn)優(yōu)惠碼的示例代碼

    Java定時(shí)任務(wù)實(shí)現(xiàn)優(yōu)惠碼的示例代碼

    在Java中實(shí)現(xiàn)定時(shí)任務(wù)來發(fā)放優(yōu)惠碼,我們可以使用多種方法,比如使用java.util.Timer類、ScheduledExecutorService接口,或者更高級(jí)的框架如Spring的@Scheduled注解,這篇文章主要介紹了Java定時(shí)任務(wù)實(shí)現(xiàn)優(yōu)惠碼的實(shí)例,需要的朋友可以參考下
    2024-07-07
  • Java聊天室之實(shí)現(xiàn)客戶端一對一聊天功能

    Java聊天室之實(shí)現(xiàn)客戶端一對一聊天功能

    這篇文章主要為大家詳細(xì)介紹了Java簡易聊天室之實(shí)現(xiàn)客戶端一對一聊天功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以了解一下
    2022-10-10
  • SpringBoot中使用Jsoup爬取網(wǎng)站數(shù)據(jù)的方法

    SpringBoot中使用Jsoup爬取網(wǎng)站數(shù)據(jù)的方法

    這篇文章主要介紹了SpringBoot中使用Jsoup爬取網(wǎng)站數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Spring Boot集成Druid數(shù)據(jù)庫連接池

    Spring Boot集成Druid數(shù)據(jù)庫連接池

    這篇文章主要介紹了Spring Boot集成Druid數(shù)據(jù)庫連接池,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • slf4j與jul、log4j1、log4j2、logback的集成原理

    slf4j與jul、log4j1、log4j2、logback的集成原理

    這篇文章主要介紹了slf4j與jul、log4j1、log4j2、logback的集成原理,以及通用日志框架與具體日志實(shí)現(xiàn)系統(tǒng)的機(jī)制機(jī)制介紹,包括依賴的jar包,jar沖突處理等
    2022-03-03
  • Java List接口與Iterator接口及foreach循環(huán)使用解析

    Java List接口與Iterator接口及foreach循環(huán)使用解析

    這篇文章主要介紹了Java List接口與Iterator接口及foreach循環(huán),主要包括List接口與Iterator接口及foreach循環(huán)具體的使用方法和代碼,需要的朋友可以參考下
    2022-04-04

最新評論

夏邑县| 苗栗市| 岐山县| 达拉特旗| 闻喜县| 日土县| 陇西县| 麻城市| 新余市| 深泽县| 清流县| 廉江市| 裕民县| 宾阳县| 灵丘县| 潞城市| 双城市| 桦川县| 嫩江县| 阿拉善右旗| 靖宇县| 沅陵县| 沛县| 永登县| 珲春市| 吉隆县| 陆川县| 尤溪县| 威远县| 乾安县| 龙南县| 蒙自县| 中方县| 宜良县| 治县。| 石河子市| 林甸县| 余江县| 泉州市| 洛宁县| 泰宁县|