零基础搭建boot+MybatisPlus的详细教程

网友投稿 362 2022-08-19


零基础搭建boot+MybatisPlus的详细教程

目录1.准备工作1.1 创建数据库表1.2 创建boot项目1.3 创建实体类(映射数据库表)2.使用mybatisPlus(操作数据库)2.1 添加mybatisPlus依赖2.2 配置数据库信息2.3 创建mapper接口2.4 配置mapper扫描2.5 test3. 常用设置3.1 设置表映射规则3.2 主键生成策略(默认基于雪花算法)3.3 全局设置3.4 字段与列名的驼峰映射(默认开启)3.5 日志设置4.基操4.1 插入 insert()4.2 删除 deleteXxx() map4.3 更新 updateXxx()5.Wrapper(条件构造器)5.16. service层使用7. 代码生成器(未完待续)

1.准备工作

1.1 创建数据库表

创建表

CREATE TABLE `login`(

`id` INT(4) primary key auto_increment,

`login_id` VARCHAR(50) UNIQUE,

`city` VARCHAR(50) DEFAULT '富平',

`password` VARCHAR(50)

)

在可视化工具中添加数据(我不太会写sql)

1.2 创建boot项目

1.3 创建实体类(映射数据库表)

2.使用mybatisPlus(操作数据库)

2.1 添加mybatisPlus依赖

com.baomidou

mybatis-plus-boot-starter

3.1.2

mysql

2.2 配置数据库信息

spring:

datasource:

url: jdbc:mysql://localhost:3306/test0314?characterEncoding=utf-8&serverTimezone=UTC

username: root

password: root

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

2.3 创建mapper接口

该接口中提供了常用的crud方法,我们只需要从容器中获取mapper操作数据即可

package com.hand.demo.mapper;

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

import com.hand.demo.entity.User;

/**

* 用户数据访问层接口

* */

public interface UserMapper extends BaseMapper {

}

2.4 配置mapper扫描

在启动类中配置我们的mapper在哪个包两种方法:@Mapper注解(麻烦);@MapperScan(在主启动类上进行配置)

@SpringBootApplication

@MapperSchttp://an("com.hand.demo.mapper")

public class Demo0318Application {

public static void main(String[] args) {

SpringApplication.run(Demo0318Application.class, args);

}

}

2.5 test

junit

junit

test

在test包下

package com.hand.demo;

import com.hand.demo.entity.User;

import com.hand.demo.mapper.UserMapper;

import org.junit.jupiter.api.Test;

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

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

import java.util.List;

@SpringBootTest

class Demo0318ApplicationTests {

@Autowired

private UserMapper userMapper;

/**

* 获取UserMapper实现类对象(mybatisPlus容器会使用动态代理生成该接口的实现类对象,并注入到spring容器中

* 所以我们只需要在这定义一个成员变量,通过注解自动注入即可)

* */

@Test

public void testQueryAll() {

List userList = userMapper.selectList(null);

System.out.println(userList);

}

}

3. 常用设置

3.1 设置表映射规则

设置表前缀配置

3.2 主键生成策略(默认基于雪花算法)

@TableId(type = IdType.AUTO)

private Long id;

3.3 全局设置

mybatis-plus:

global-config:

db-config:

table-prefix:

id-type: auto

3.4 字段与列名的驼峰映射(默认开启)

mybatis-plus:

global-config:

db-config:

table-prefix:

id-type: auto

configuration:

map-underscore-to-camel-case: false

3.5 日志设置

mybatis-plus:

global-config:

db-config:

table-prefix:

id-type: auto

configuration:

map-underscore-to-camel-case: false

log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

4.基操

4.1 插入 insert()

4.2 删除 deleteXxx() map

4.3 更新 updateXxx()

5.Wrapper(条件构造器)

5.1

Wrapper

AbstractWrapper

QueryWrapper UpdateWrapper

QueryWrapper的select可以设置需要查询的列

6. service层使用

不需要手动注入该泛型内的mapper如果需要别的mapper手动注入就行

package com.hand.demo.service;

import com.baomidou.mybatisplus.extension.service.IService;http://

import com.hand.demo.entity.User;

public interface UserService extends IService {

}

package com.hand.demo.service.Impl;

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

import com.hand.demo.entity.User;

import com.hand.demo.mapper.UserMapper;

import com.hand.demo.service.UserService;

@Service

public class UserServiceImpl extends ServiceImpl implements UserService {

}

@Autowired

private UserService userService;

@Test

public void testService() {

List list = userService.list();

System.out.println(list);

}

也有自己的批量操作等(batch)自定义方法(多表关联)

7. 代码生成器(未完待续)

每个接口都在继承相同的BaseMapper,IService(代码冗余,繁琐)MybatisPlus提供的代码生成器,一键生成mvc三层所有代码如何使用,引入下边的包

com.baomidou

mybatis-plus-generator

3.5.2

org.freemarker

freemarker


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java的jmap命令的具体使用
下一篇:Java实现超简单抖音去水印的示例详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~