当前位置: 首页 > news >正文

002 springboot整合mybatis-plus

文章目录

    • TestMybatisGenerate.java
    • pom.xml
    • application.yaml
    • ReceiveAddressMapper.xml
    • receive_address.sql
    • ReceiveAddress.java
    • ReceiveAddressMapper.java
    • IReceiveAddressService
    • ReceiveAddressServiceImpl.java
    • ReceiveAddressController.java
    • TestAddressService.java
    • SpringbootDemoApplication.java

TestMybatisGenerate.java


package com.example;import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;import java.util.Collections;public class TestMybatisGenerate {public static void main(String[] args) {FastAutoGenerator.create("jdbc:mysql://localhost:3306/dicts?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai", "root", "123456").globalConfig(builder -> {builder.author("dd") // 设置作者//.enableSwagger() // 开启 swagger 模式.outputDir("D://generate"); // 指定输出目录}).packageConfig(builder -> {builder.parent("com.example") // 设置父包名.moduleName("") // 设置父包模块名.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://generate//com/example/mapper")); // 设置mapperXml生成路径}).strategyConfig(builder -> {builder.addInclude("receive_address") // 设置需要生成的表名.addTablePrefix("tb_", "c_"); // 设置过滤表前缀}).templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板.execute();}}

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.6</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>springboot_demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>springboot_demo</name><description>springboot_demo</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-starter-tomcat</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version></dependency><!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.2</version></dependency><!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-generate --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.5.1</version></dependency><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.31</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

application.yaml


server:servlet:context-path: /appport: 8080spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/dicts?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghaiusername: rootpassword: 123456

ReceiveAddressMapper.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.example.mapper.ReceiveAddressMapper"></mapper>

receive_address.sql

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- ----------------------------
-- Table structure for receive_address
-- ----------------------------
DROP TABLE IF EXISTS `receive_address`;
CREATE TABLE `receive_address`  (`addr_id` int(0) NOT NULL AUTO_INCREMENT,`receive_user_telno` bigint(0) NULL DEFAULT NULL,`receive_username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,`cust_id` int(0) NULL DEFAULT NULL,`addr_province` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '地址的省份',`addr_city` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '地址的城市',`addr_area` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '地址的区域',`addr_street` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '地址的街道',`addr_detail` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '详细地址',`status` int(0) NULL DEFAULT NULL COMMENT '状态',`version` int(0) NULL DEFAULT NULL COMMENT '版本号,用于做乐观锁',`create_time` datetime(0) NULL DEFAULT NULL COMMENT '数据添加的时间',`update_time` datetime(0) NULL DEFAULT NULL COMMENT '数据修改时间',PRIMARY KEY (`addr_id`) USING BTREE,INDEX `fk_address_customer`(`cust_id`) USING BTREE,CONSTRAINT `fk_address_customer` FOREIGN KEY (`cust_id`) REFERENCES `customer` (`cust_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of receive_address
-- ----------------------------
INSERT INTO `receive_address` VALUES (1, NULL, NULL, 1, '江苏省', '苏州市', '园区', '若水路', '若水路', 1, 1, '2023-08-11 13:47:02', NULL);
INSERT INTO `receive_address` VALUES (2, NULL, NULL, 1, '黑龙江', '大庆市', '市区', '育才路', '育才路', 1, 1, '2023-07-31 13:47:52', NULL);SET FOREIGN_KEY_CHECKS = 1;

ReceiveAddress.java


package com.example.entity;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.time.LocalDateTime;/*** <p>* * </p>** @author dd* @since 2024-04-10*/
@TableName("receive_address")
public class ReceiveAddress implements Serializable {private static final long serialVersionUID = 1L;@TableId(value = "addr_id", type = IdType.AUTO)private Integer addrId;private Long receiveUserTelno;private String receiveUsername;private Integer custId;/*** 地址的省份*/private String addrProvince;/*** 地址的城市*/private String addrCity;/*** 地址的区域*/private String addrArea;/*** 地址的街道*/private String addrStreet;/*** 详细地址*/private String addrDetail;/*** 状态*/private Integer status;/*** 版本号,用于做乐观锁*/private Integer version;/*** 数据添加的时间*/private LocalDateTime createTime;/*** 数据修改时间*/private LocalDateTime updateTime;public Integer getAddrId() {return addrId;}public void setAddrId(Integer addrId) {this.addrId = addrId;}public Long getReceiveUserTelno() {return receiveUserTelno;}public void setReceiveUserTelno(Long receiveUserTelno) {this.receiveUserTelno = receiveUserTelno;}public String getReceiveUsername() {return receiveUsername;}public void setReceiveUsername(String receiveUsername) {this.receiveUsername = receiveUsername;}public Integer getCustId() {return custId;}public void setCustId(Integer custId) {this.custId = custId;}public String getAddrProvince() {return addrProvince;}public void setAddrProvince(String addrProvince) {this.addrProvince = addrProvince;}public String getAddrCity() {return addrCity;}public void setAddrCity(String addrCity) {this.addrCity = addrCity;}public String getAddrArea() {return addrArea;}public void setAddrArea(String addrArea) {this.addrArea = addrArea;}public String getAddrStreet() {return addrStreet;}public void setAddrStreet(String addrStreet) {this.addrStreet = addrStreet;}public String getAddrDetail() {return addrDetail;}public void setAddrDetail(String addrDetail) {this.addrDetail = addrDetail;}public Integer getStatus() {return status;}public void setStatus(Integer status) {this.status = status;}public Integer getVersion() {return version;}public void setVersion(Integer version) {this.version = version;}public LocalDateTime getCreateTime() {return createTime;}public void setCreateTime(LocalDateTime createTime) {this.createTime = createTime;}public LocalDateTime getUpdateTime() {return updateTime;}public void setUpdateTime(LocalDateTime updateTime) {this.updateTime = updateTime;}@Overridepublic String toString() {return "ReceiveAddress{" +"addrId=" + addrId +", receiveUserTelno=" + receiveUserTelno +", receiveUsername=" + receiveUsername +", custId=" + custId +", addrProvince=" + addrProvince +", addrCity=" + addrCity +", addrArea=" + addrArea +", addrStreet=" + addrStreet +", addrDetail=" + addrDetail +", status=" + status +", version=" + version +", createTime=" + createTime +", updateTime=" + updateTime +"}";}
}

ReceiveAddressMapper.java


package com.example.mapper;import com.example.entity.ReceiveAddress;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;/*** <p>*  Mapper 接口* </p>** @author dd* @since 2024-04-10*/
public interface ReceiveAddressMapper extends BaseMapper<ReceiveAddress> {}

IReceiveAddressService


package com.example.service;import com.example.entity.ReceiveAddress;
import com.baomidou.mybatisplus.extension.service.IService;/*** <p>*  服务类* </p>** @author dd* @since 2024-04-10*/
public interface IReceiveAddressService extends IService<ReceiveAddress> {}

ReceiveAddressServiceImpl.java


package com.example.service.impl;import com.example.entity.ReceiveAddress;
import com.example.mapper.ReceiveAddressMapper;
import com.example.service.IReceiveAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;/*** <p>*  服务实现类* </p>** @author dd* @since 2024-04-10*/
@Service
public class ReceiveAddressServiceImpl extends ServiceImpl<ReceiveAddressMapper, ReceiveAddress> implements IReceiveAddressService {}

ReceiveAddressController.java

package com.example.controller;import com.example.entity.ReceiveAddress;
import com.example.service.IReceiveAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;/*** <p>*  前端控制器* </p>** @author dd* @since 2024-04-10*/
@Controller
@RequestMapping("/receive-address")
public class ReceiveAddressController {@Autowiredprivate IReceiveAddressService addressService;/*** 根据主键查询* @param arrId* @return*/@GetMapping("{addrId}")@ResponseBodypublic ReceiveAddress getById(@PathVariable("addrId") Integer arrId){//URL :  http://localhost:8080/app/receive-address/103ReceiveAddress address = addressService.getById(arrId);return address;}/*** 查询所有     //URL :  http://localhost:8080/app/receive-address/* @return*/@GetMapping("")@ResponseBodypublic List<ReceiveAddress> getAll(){List<ReceiveAddress> addressList = addressService.list();return addressList;}/*** 分页查询* @return*///@GetMapping("page/{pageNum}")//public List<ReceiveAddress> getByPage(@PathVariable("pageNum") Integer pageNum){//    return null;//}}

TestAddressService.java


package com.example;import com.example.entity.ReceiveAddress;
import com.example.service.IReceiveAddressService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class TestAddressService {@Autowiredprivate IReceiveAddressService service;@Testpublic void getById(){ReceiveAddress address = service.getById(1);System.out.println(address);}
}

SpringbootDemoApplication.java


package com.example;import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.example.mapper")
public class SpringbootDemoApplication {public static void main(String[] args) {SpringApplication.run(SpringbootDemoApplication.class, args);}}
http://www.lryc.cn/news/340020.html

相关文章:

  • 代码随想录训练营第三十五期|第天16|二叉树part03|104.二叉树的最大深度 ● 111.二叉树的最小深度● 222.完全二叉树的节点个数
  • Mac版2024 CleanMyMac X 4.15.2 核心功能详解 cleanmymac这个软件怎么样?cleanmymac到底好不好用?
  • 【华为OD机试】执行任务赚积分【C卷|100分】
  • mybatis分页实现总结
  • Vue3——html-doc-js(html导出为word的js库)
  • 第19天:信息打点-小程序应用解包反编译动态调试抓包静态分析源码架构
  • 外观模式:简化复杂系统的统一接口
  • PHP数组去重
  • 论软件系统的架构风格,使用三段论 写一篇系统架构师论文
  • 深度挖掘响应式模式的潜力,从而精准优化AI与机器学习项目的运行效能,引领技术革新潮流
  • 企业级网络安全:入侵防御实时阻止,守护您的业务安全
  • (一)Java八股——Redis
  • 2024.4.15力扣每日一题——设计哈希映射
  • 数据结构DAY4--哈希表
  • MySQL二阶段和三阶段提交
  • 代码随想录算法训练营第四十二天|01背包问题、416. 分割等和子集
  • JVM主要知识点详解
  • hot100 -- 链表(中)
  • 数据结构面试常见问题
  • 蓝桥杯2024年第十五届省赛真题-R 格式(高精度乘法 + 加法)
  • 普通人做抖音小店真的能赚钱吗?可以,但更取决于个人
  • 基于单链表实现通讯管理系统!(有完整源码!)
  • MATLAB入门介绍
  • 【k8s】:深入理解 Kubernetes 中的污点(Taints)与容忍度(Tolerations)
  • Angular 使用DomSanitizer防范跨站脚本攻击
  • (八)PostgreSQL的数据库管理
  • 外包干了30天,技术倒退明显
  • ruoyi-nbcio-plus基于vue3的flowable的自定义业务单表例子的升级修改
  • 【ENSP】华为三层交换机配置AAA认证,开启telnet服务
  • collections模块下的Counter函数讲解