快速搭建mybatis-plus项目
项目初始化
pom.xml补全mybatis-plus的依赖(mybatis-plus-boot-starter)
1
2
3
4
5<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>默认插件spring-boot-maven-plugin的skip属性改为false,否则打包的jar包无法运行
修改resources下的application.yaml (当前mysql版本5.7)
1
2
3
4
5
6
7
8
9server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/demo
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus集成
项目结构
实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19package com.example.demo1.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
public class User {
private Long id;
private String username;
private String password;
}mapper接口
1
2
3
4
5
6
7
8
9
10
11package com.example.demo1.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo1.entity.User;
import org.springframework.stereotype.Repository;
public interface UserMapper extends BaseMapper<User> {
}数据库(id字段自增)
service接口
1
2
3
4
5
6
7
8package com.example.demo1.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo1.entity.User;
public interface UserService extends IService<User> {
}service实现类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17package com.example.demo1.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo1.entity.User;
import com.example.demo1.mapper.UserMapper;
import com.example.demo1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
private UserMapper userMapper;
}控制器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43package com.example.demo1.controller;
import com.example.demo1.entity.User;
import com.example.demo1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
public class UserController {
private UserService userService;
public List<User> findAll() {
return userService.list();
}
public User findById( { Long id)
return userService.getById(id);
}
public boolean save( { User user)
return userService.save(user);
}
public boolean update( { User user)
return userService.updateById(user);
}
public boolean delete( { Long id)
return userService.removeById(id);
}
}启动类Application下添加MapperScan
1
2
3
4
5
6
7
8
9
10
11
12
13
14package com.example.demo1;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}运行测试
1
2
3http://127.0.0.1:8080/user/
http://127.0.0.1:8080/user/1
...
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 UTF!