使用SpringBoot去集成Mybatis-Plus
具体步骤
你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。
1.导入我们的依赖
<dependencies>
<!-- springboot 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- ali 连接池依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- ali json依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.mapper.asl.version}</version>
</dependency>
<!-- mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>${mybatis.ehcache.version}</version>
</dependency>
<!-- mysql 依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 自动生成get,set方法 依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- springboot 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.创建我们测试的数据库
数据库的脚本文件如下:
/*
Navicat Premium Data Transfer
Source Server : jmh
Source Server Type : MySQL
Source Server Version : 50562
Source Host : localhost:3306
Source Schema : shiro
Target Server Type : MySQL
Target Server Version : 50562
File Encoding : 65001
Date: 22/05/2021 16:59:54
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`permission` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`salt` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 'jmh', '123456', NULL, NULL);
SET FOREIGN_KEY_CHECKS = 1;
3.创建实体类
package com.jmh.pojo;
/*
PACKAGE_NAME:com.jmh.pojo
USER:18413
DATE:2021/5/22 16:41
PROJECT_NAME:shiro-pro
面向代码面向君,不负代码不负卿————蒋明辉 */
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@TableName("user")
@AllArgsConstructor
@NoArgsConstructor
public class User {
@TableId("id")
private int id;
private String username;
private String password;
private String permission;
private String salt;
}
4.创建Dao
注意:我们使用通用Mapper可以继承BaseMapper,将泛型绑定为我们的实体类,Mybatis-plus则会自动根据我们设置的泛型来绑定增删改查的对象。
package com.jmh.dao;
/*
PACKAGE_NAME:com.jmh.dao
USER:18413
DATE:2021/5/22 16:43
PROJECT_NAME:shiro-pro
面向代码面向君,不负代码不负卿————蒋明辉 */
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jmh.pojo.User;
public interface UserDao extends BaseMapper<User> {
}
5.创建我们的测试类
这里我只做简单的查询操作,需要更详细的可以去查看官方文档
package com.jmh;
/*
PACKAGE_NAME:com.jmh
USER:18413
DATE:2021/5/22 16:43
PROJECT_NAME:shiro-pro
面向代码面向君,不负代码不负卿————蒋明辉 */
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jmh.dao.UserDao;
import com.jmh.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.junit.Test;
import java.util.List;
public class TestMP {
@Autowired
private UserDao userDao;
@Test
public void findAll(){
//这里的null表示查询的条件对象为空,即查询全部数据
List<User> users = userDao.selectList(null);
for (User user : users) {
System.out.println(user);
}
}
}