MyBatisPlus是mybatis的增强版,官网地址https://baomidou.com/
如果学习中遇到问题可以看这篇文章
MyBatisPlus遇到的问题
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT ' ',
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 'admin', '123');
INSERT INTO `user` VALUES (2, '15717591201', '123');
INSERT INTO `user` VALUES (3, '123', '123');
INSERT INTO `user` VALUES (13, 'qwer', '123');
INSERT INTO `user` VALUES (14, '1223', '123');
INSERT INTO `user` VALUES (15, '12212', '123');
INSERT INTO `user` VALUES (16, '1232', '112');
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.3.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
使用lombok依赖,IDEA必须要安装lombok插件
方法File -> Settings ->Plugins 搜索lombok,然后点击Install安装
找到src/main/resources的application.properties
配置mysql数据库连接
配置时按照自己对应的版本进行配置
mysql5 SpringBoot2.1以下
#mysql数据库连接
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
mysql8以上(spring boot 2.1)
注 意 \color{red}{注意} 注意:driver和url的变化
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
1、这里的 url 使用了 ?serverTimezone=GMT%2B8 后缀,因为Spring Boot 2.1 集成了 8.0版本的jdbc驱动,这个版本的 jdbc 驱动需要添加这个后缀,否则运行测试用例报告如下错误:
java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or represents more
2、这里的 driver-class-name 使用了 com.mysql.cj.jdbc.Driver ,在 jdbc 8 中 建议使用这个驱动,之前的 com.mysql.jdbc.Driver 已经被废弃,否则运行测试用例的时候会有 WARN 信息
package com.w.mpdemo.entity;
import lombok.Data;
@Data
public class User {
private Integer id;
private String name;
private String password;
}
@Date就是lombk中的注解,可以自动生成get、set方法
继承于BaseMapper类,里面有各种方法
package com.w.mpdemo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.w.mpdemo.entity.User;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
在SpringBoot启动类中添加mapper扫描注解
package com.w.mpdemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.w.mpdemo.mapper")
public class MpdemoApplication {
public static void main(String[] args) {
SpringApplication.run(MpdemoApplication.class, args);
}
}
在src\test\java\com\w\mpdemo\MpdemoApplicationTests.java
中开始测试
package com.w.mpdemo;
import com.w.mpdemo.entity.User;
import com.w.mpdemo.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class MpdemoApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void findALl() {
List<User> userList = userMapper.selectList(null);
System.out.println(userList);
}
}
查询结果:
2020-08-15 13:57:36.488 INFO 13244 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-08-15 13:57:36.676 INFO 13244 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
[User(id=1, name=admin, password=123), User(id=2, name=15717591201, password=123), User(id=3, name=123, password=123), User(id=13, name=qwer, password=123), User(id=14, name=1223, password=123), User(id=15, name=12212, password=123), User(id=16, name=1232, password=112)]
如果数据库的表名和实体类名不一样可以使用@TableName注解
@TableName("user")
在配置文件中添加
#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
使用之后控制台会打印出如下日志
......
JDBC Connection [HikariProxyConnection@1023344953 wrapping com.mysql.cj.jdbc.ConnectionImpl@7c29adc8] will not be managed by Spring
==> Preparing: SELECT id,name,password FROM user
==> Parameters:
<== Columns: id, name, password
<== Row: 1, admin, 123
<== Row: 2, 15717591201, 123
<== Row: 3, 123, 123
<== Row: 13, qwer, 123
<== Row: 14, 1223, 123
<== Row: 15, 12212, 123
<== Row: 16, 1232, 112
<== Total: 7
......
重启IDEA
如果还没有用找到File -> Settings -> Build,Execution,Deployment -> Compiler -> Annotation Processors
把Enable annotation processing勾选上
再重启IDEA