栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot快速整合Mybatis&MybatisPlus

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot快速整合Mybatis&MybatisPlus

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

模板视图 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

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;

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 implements UserService {

}

[](()2.2.8 测试用例

package com.example;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/863419.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号