文章目录
- 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") .outputDir("D://generate"); }).packageConfig(builder -> {builder.parent("com.example") .moduleName("") .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://generate//com/example/mapper")); }).strategyConfig(builder -> {builder.addInclude("receive_address") .addTablePrefix("tb_", "c_"); }).templateEngine(new FreemarkerTemplateEngine()) .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/> </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><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.2</version></dependency><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;
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;
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;
@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;
public interface ReceiveAddressMapper extends BaseMapper<ReceiveAddress> {}
IReceiveAddressService
package com.example.service;import com.example.entity.ReceiveAddress;
import com.baomidou.mybatisplus.extension.service.IService;
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;
@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;
@Controller
@RequestMapping("/receive-address")
public class ReceiveAddressController {@Autowiredprivate IReceiveAddressService addressService;@GetMapping("{addrId}")@ResponseBodypublic ReceiveAddress getById(@PathVariable("addrId") Integer arrId){ReceiveAddress address = addressService.getById(arrId);return address;}@GetMapping("")@ResponseBodypublic List<ReceiveAddress> getAll(){List<ReceiveAddress> addressList = addressService.list();return addressList;}}
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);}}