导包,版本什么的我就不说了 第七步:service实现类 第八步:UserMapper.xml 第九步:测试类 这样一套拥有自增ID,自动填充创建及修改日期,且支持自定义sql的MQ程序基本功能就完成了 总结:自动填充需要重写metaObjectHandler方法insertFill和updateFill
第一步:写yml文件
给个端口号
server:
port: 8081
给个数据源
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.47.130:3306/mybatis_plus?serverTimezone=GMT%2B8&characterEncoding=utf-8
username: root
password: 123456
配mybatisplus驼峰和映射位置
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:mapper
继承IService泛型实体类
public interface UserService extends IService
package com.lindashua.mybatisplus.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lindashua.mybatisplus.entity.User;
import com.lindashua.mybatisplus.mapper.UserMapper;
import com.lindashua.mybatisplus.service.UserService;
import org.springframework.stereotype.Service;
注解开启通用Service
@Service
实现UserService 并继承ServiceImpl泛型mapper和实体类
public class UserServiceImpl extends ServiceImpl
这个是为了实现自定义mapper,就和mybatis一样写就可以
select * from user
where name like “%”#{name}"%"
这里提醒一下,测试类要和我们的启动类在一个位置上,不然注解读不到spring配置package com.lindashua.mybatisplus;
import com.lindashua.mybatisplus.entity.User;
import com.lindashua.mybatisplus.mapper.UserMapper;
import com.lindashua.mybatisplus.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@SpringBootTest
public class MybatisPlusTest {
@Autowired
private UserMapper userMapper;
@Autowired
private UserService userService;
@Test
public void test1(){
System.out.println("1");
}
@Test
public void FindById(){
Object o = userMapper.selectById(1l);
System.out.println(o);
}
@Test
public void selectAllUseLike(){
List
this.strictInsertFill(metaObject, “updateTime”, LocalDateTime.class, LocalDateTime.now());
乐观锁和分页需要增加配置类,创建一个bean增强MybatisPlusInterceptor对象, mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());,开启分页或者乐观锁



