mysql
mysql-connector-java
5.1.45
com.baomidou
mybatis-plus-boot-starter
3.4.0
[](()2.2 在application.yml进行配置数据源 [](()2.2.1 新建applicaiton.yml` 环境隔离spring:
profiles:
active: dev
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】
server:
compression:
请求gzip压缩enabled: true
mime-types: text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json,application/xml
min-response-size: 1024
application:
name: edu-front-web
模板视图 thymeleafthymeleaf:
cache: false
prefix: classpath:/templates/
mode: HTML
encoding: UTF-8
json的转换配置jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
locale: zh_CN
generator:
write-numbers-as-strings: true
write-bigdecimal-as-plain: true
##mybatis的原生态支持
mybatis-plus:
mapper-locations: classpath*:/mapper
SET FOREIGN_KEY_CHECKS=0;
– Table structure for user
DROP TABLE IF EXISTS user;
CREATE TABLE user (
id int(11) NOT NULL AUTO_INCREMENT,
nickname varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT ‘用户名’,
password varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT ‘密码’,
age int(11) DEFAULT NULL COMMENT ‘年龄’,
male int(11) DEFAULT NULL COMMENT ‘性别’,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
[](()2.2.4 新建实体package com.example.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
@Data // getter/setter
@ToString // toString
@AllArgsConstructor // 有参构造函数
@NoArgsConstructor // 无参构造函数
@TableName(“user”)
@Accessors(chain = true)
@ApiModel(description = “用户实体”)
public class User {
// 用户编号
@TableId(type = IdType.AUTO)
@ApiModelProperty(value = “用户编号”, required = true)
private Integer id;
// 用户昵称
@ApiModelProperty(value = “用户昵称”, required = true)
private String nickname;
// 用户密码
@ApiModelProperty(value = “用户密码”, required = true)
private String password;
// 用户年龄
@ApiModelProperty(value = “用户年龄”, required = true)
private Integer age;
// 用户性别 0 女 1 男 2 保密
@ApiModelProperty(value = “用户性别”, required = true)
private Integer male;
}
[](()2.2.5 新建mapperpackage com.example.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.User;
public interface UserMapper extends BaseMapper {
}
[](()2.2.6 mybatis和spring融合在启动类上增加@MapperScan("com.kuangstudy.mapper")
package com.example;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(“com.example.mapper”)
public class StudyBootsMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(StudyBootsMybatisApplication.class, args);
}
}
[](()2.2.7 定义户Service接口和实现类package com.example.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.entity.User;
public interface UserService extends IService {
}
package com.example.service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.User;
import com.example.mapper.UserMapper;
@Service
public class UserServiceImpl extends ServiceImpl
}
[](()2.2.8 测试用例package com.example;



