写点什么

SpringBoot 快速整合 Mybatis&MybatisPlus(1)

  • 2021 年 11 月 11 日
  • 本文字数:2649 字

    阅读完需:约 9 分钟

<version>5.1.45</version>


</dependency>


<dependency>


<groupId>com.baomidou</groupId>


<artifactId>mybatis-plus-boot-starter</artifactId>


<version>3.4.0</version>


</dependency>

2.2 在 application.yml 进行配置数据源

2.2.1 新建 applicaiton.yml`

环境隔离

spring:


profiles:


active: dev


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

模板视图 thymeleaf

thymeleaf:


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/*.xml


type-aliases-package: com.example.entity


#logging:

level:

root: debug

2.2.2 新建 applicaiton-dev.yml`

数据源的配置

spring:


datasource:


type: com.zaxxer.hikari.HikariDataSource


driver-class-name: com.mysql.jdbc.Driver


url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=utf-8&useSSL=false


username: root


password: 123456


hikari:


connection-timeout: 60000


validation-timeout: 3000


idle-timeout: 60000


login-timeout: 5


max-lifetime: 60000


maximum-pool-size: 30


minimum-idle: 10


read-only: false

2.2.3 新建数据库和表

/*


Navicat MySQL Data Transfer


Source Server : localhost_3306


Source Server Version : 50635


Source Host : localhost:3306


Source Database : test


Target Server Type : MYSQL


Target Server Version : 50635


File Encoding : 65001


Date: 2021-07-23 10:19:25


*/


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 新建 mapper

package com.example.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;


import com.example.entity.User;


/**


  • @Auther: 长颈鹿

  • @Date: 2021/07/23/10:16

  • @Description:


*/


public interface UserMapper extends BaseMapper<User> {


}

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


【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


("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;


/**


  • @Auther: 长颈鹿

  • @Date: 2021/07/23/10:22

  • @Description:


*/


public interface UserService extends IService<User> {


}


package com.example.service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.example.entity.User;


import com.example.mapper.UserMapper;


/**


  • @Auther: 长颈鹿

  • @Date: 2021/07/23/10:23

  • @Description:


*/


@Service


public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


}

2.2.8 测试用例

package com.example;


import com.example.entity.User;


import com.example.service.UserService;


import org.junit.jupiter.api.Test;


import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest


class StudyBootsMybatisApplicationTests {


@Autowired


private UserService userService;


@Test


public void saveUser(){


User user = new User();


user.setId(1);


user.setNickname("yykkxxxx");


user.setPassword("1121455");


user.setAge(34);


user.setMale(1);


userService.saveOrUpdate(user);


}


}

3、基于 xml 的原生态的 mybatis 的支持

3.1 在 application.yml 增加支持 xml 的方式

##mybatis 的原生态支持


mybatis-plus:


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


type-aliases-package: com.kuangstudy.entity

3.2 在 UserMapper 定义方法

评论

发布
暂无评论
SpringBoot快速整合Mybatis&MybatisPlus(1)