1、在Pom.xml文件中添加MybatisPlus依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2、配置application.yml
server:
port: 9000
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mybatis_plus_test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
#mybatis-plus 配置
mybatis-plus:
mapper-locations: classpath:mappers/*.xml
type-aliases-package: com.donleo.mp.model
configuration:
map-underscore-to-camel-case: true
3、实体类
1 /**
2 * code MybatisPlus会默认使用实体类的类名到数据中找对应的表。
3 * .@TableName指定数据库中的表名
4 */
5 @Data
6 @NoArgsConstructor
7 @AllArgsConstructor
8 @TableName(value = "user")
9 public class User {
10 /**
11 * .@TableId:
12 * value: 指定表中的主键列的列名,如果实体属性名与列名-致,可以省略不指定.
13 * type: 指定主键策略.
14 */
15 @TableId(value = "id",type = IdType.AUTO)
16 private Integer id;
17 private String name;
18 private Integer age;
19 private String email;
20 /**
21 * mybatisPlus 默认开启驼峰命名, @TableField value 可以指定数据库字段名
22 */
23 @TableField(value = "nick_name")
24 private String nickName;
25 /**
26 * exist 数据库是否存在该字段,默认为true
27 * 注:最新版本可以不用加exist,数据库中没有salary也可以插入
28 */
29 @TableField(exist = false)
30 private Double salary;
31 }
4、数据访问层实现接口BaseMapper<T>
/**
* Mapper接口
* 基于Mybatis: 在Mapper接口 中编写CRUD相关的方法提供Mapper接 口所对应的SQL映射文件以及方法对应的SQL语句.
* 基于MP: 让XxxMapper接口继承 BaseMapper接口即可.
* BaseMapper<T> :泛 型指定的就是当前Mapper接口所操作的实体类类型
*/
public interface UserMapper extends BaseMapper<User> {
/**
* 在MybatisPlus的基础上也可以自定义接口,写xml
* @param user
* @return
*/
List<User> findBySome(User user);
}
5、测试
1 @Autowired
2 private UserMapper userMapper;
3
4 /**
5 * 通用 插入操作
6 */
7 @Test
8 void testCommonInsert() {
9 User user = new User();
10 user.setName("lia");
11 user.setAge(23);
12 user.setEmail("2425253654@qq.com");
13 user.setNickName("暖阳");
14 int result = userMapper.insert(user);
15 System.out.println("影响行数:" + result);
16
17 Integer key = user.getId();
18 System.out.println("key:" + key);
19 }
20
21 /**
22 * 通用 删除操作
23 */
24 @Test
25 void testCommonDelete() {
26 int result = userMapper.deleteById(9);
27 System.out.println("result:" + result);
28 }
29
30 /**
31 * 通用 更新操作
32 * mybatisPlu会自动进行非空判断
33 */
34 @Test
35 void testCommonUpdate() {
36 User user = new User();
37 user.setId(7);
38 user.setName("zcx");
39 user.setAge(22);
40 user.setEmail("zcx@qq.com");
41 user.setNickName("xx");
42 int result = userMapper.updateById(user);
43 System.out.println("影响行数:" + result);
44
45 Integer key = user.getId();
46 System.out.println("key:" + key);
47 }
48
49
50 /**
51 * 通用 查询操作
52 */
53 @Test
54 void TestCommonSelect() {
55 //1、根据Id查询
56 User user = userMapper.selectById(8);
57 System.out.println("user:" + user);
58
59 //2、查询所有,不带参数
60 List<User> userList = userMapper.selectList(null);
61 userList.forEach(System.out::println);
62
63 //3、自定义接口模糊查询sql
64 User user1 = new User();
65 user1.setEmail("2425");
66 List<User> bySome = userMapper.findBySome(user1);
67 System.out.println("模糊查询" + bySome);
68
69 //4、多列查询
70 Map<String, Object> columnMap = new HashMap<>();
71 columnMap.put("id", 7);
72 columnMap.put("email", "242525034@qq.com");
73 List<User> users1 = userMapper.selectByMap(columnMap);
74 System.out.println("user1:" + users1);
75 }