依赖
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
// implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.2'
implementation('com.baomidou:mybatis-plus-boot-starter:3.1.0')
implementation('com.baomidou:mybatis-plus-generator:3.1.0')
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'mysql:mysql-connector-java'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
- 主要是mybatis-plus-boot-starter这个注解,里面包含了mybatis,不需要重复引入
配置文件
server:
port: 10000
spring:
datasource:
url: jdbc:mysql://localhost:3307/JLCredit
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
#mapper的xml文件所在
mapper-locations: classpath:mapper/*.xml
configuration:
#开启驼峰命名
map-underscore-to-camel-case: true
- 普通的连接mysql数据库
- mybatis若要在xml文件中写sql,需要注意xml文件位置,这里是直接放在了resources/mapper路径下
开启事务
@EnableTransactionManagement
@SpringBootApplication
public class MybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusApplication.class, args);
}
}
使用
配置
@Configuration
public class MyBatisPlusConfig {
/**
* mybatis-plus SQL执行效率插件【生产环境可以关闭】
*/
@Bean
public PerformanceInterceptor performanceInterceptor() {
return new PerformanceInterceptor();
}
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
- performanceInterceptor在开启之后每次使用mybatis-plus会打印出执行的sql语句
- paginationInterceptor则是分页工具
实体类
@Data
@TableName("user")
public class UserEntity implements Serializable {
@TableId
private String username;
private String id;
private String name;
private String phone;
@TableField(fill = FieldFill.INSERT) // 创建记录的时候需要填充
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE) // 创建和修改的时候都需要填充
private Date updateTime;
}
- 实体类@TableName映射表名
- @TableField的自动填充通过以下配置实现创建时间和修改时间的插入与更新:
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime", new Date(), metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime", new Date(), metaObject);
}
}
mapper
@Mapper
public interface UserMapper extends BaseMapper<UserEntity> {
UserEntity getUserById(@Param("id") String id);
IPage<UserEntity> getUserPage(Page<?> page);
}
server
public interface UserServer extends IService<UserEntity> {
UserEntity getUserById(String id);
IPage<UserEntity> getUserPage(Page<UserEntity> page);
void saveUser(UserEntity userEntity);
}
serverImpl
@Service
public class UserServerImpl extends ServiceImpl<UserMapper, UserEntity> implements UserServer {
@Override
public UserEntity getUserById(String id) {
return baseMapper.getUserById(id);
}
@Override
public IPage<UserEntity> getUserPage(Page<UserEntity> page) {
return baseMapper.getUserPage(page);
}
@Transactional
@Override
public void saveUser(UserEntity userEntity) {
save(userEntity);
}
}
- 在编写完实体类,以及实体类对应的mapper,server,serverImpl之后,就可以使用这个server对该实体所映射的表进行增删改查的操作,mybatisplus提供的操作类基本满足了所有单表的需求,但是多表查询依旧建议自己写个sql
- 自己写原生sql有两种,一种是通过@Select,@Updata,@Delete,@Insert在mapper中写,要么就编写xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ladyishenlong.mybatisplus.mapper.UserMapper">
<select id="getUserById" resultType="com.ladyishenlong.mybatisplus.model.UserEntity">
select i.name from user i where i.id = #{id}
</select>
<select id="getUserPage" resultType="com.ladyishenlong.mybatisplus.model.UserEntity">
select i.name from user i
</select>
</mapper>
查询
@Autowired
private UserServer userServer;
@GetMapping
public Object test() {
//分页查询
IPage<UserEntity> page = new Page<>();
page.setCurrent(0); //当前页
page.setSize(1); //每页条数
userServer.page(page, new QueryWrapper<UserEntity>().lambda()
.eq(UserEntity::getId, "666"));
//xml分页查询
Page<UserEntity> userEntityPage = new Page<>();
userEntityPage.setCurrent(0);
userEntityPage.setSize(1);
IPage<UserEntity> userEntityIPage = userServer.getUserPage(userEntityPage);
//xml语句
userServer.getUserById("666");
//自动生成查询
userServer.list(new QueryWrapper<UserEntity>().lambda()
.eq(UserEntity::getId, "666"));
return userEntityIPage;
}
插入
@Autowired
private UserServer userServer;
@GetMapping("/test2")
public Object test2() {
//插入数据,可以添加事务
UserEntity userEntity = new UserEntity();
userEntity.setId("2500");
userEntity.setName("黑魔导");
userEntity.setPhone("2500");
userEntity.setUsername("2500");
userServer.saveUser(userEntity);
return "";
}