springboot使用mybatis-plus(一)

前言

因为一直使用的是mybatis,并且以前使用的是一个叫tk.mapper的框架,而如今mybatis-plus这个框架比较火,就来学习一下;html

使用

个人环境java

  • jdk1.8
  • maven3.6
  • MySQL5.7
  • springboot2.1.0

数据结构

这是使用的mybatis-plus官网的数据结构及脚本mysql

DROP TABLE IF EXISTS user;
CREATE TABLE user
(
	id BIGINT(20) NOT NULL COMMENT '主键ID',
	name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
	age INT(11) NULL DEFAULT NULL COMMENT '年龄',
	email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
	PRIMARY KEY (id)
);

复制代码
DELETE FROM user;

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
复制代码

引包

pom文件导包,这里我使用了druid;也能够不用;注意,不用导入mybatis的包,和mybatis-spring的包;git

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!--mysql-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.1.0</version>
		</dependency>
		<!-- druid的starter -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.10</version>
		</dependency>

复制代码

项目结构

  • MybatisPageConfig代码: 这是mybatis的配置类,这里主要写了mybatis-plus的分页配置;若是不配置分页,能够将mapperscan注解写到项目的启动类上面去,将该类删除;这里为了方便阅读,就写在一块儿了;
@Configuration
@MapperScan("com.example.demo.mapper")
public class MybatisPageConfig {
    /** * mybatis-plus分页插件<br> */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        return paginationInterceptor;
    }
}

复制代码
  • UserMapper 代码:
public interface UserMapper extends BaseMapper<User> {
}

复制代码
  • User代码:
@TableName("user")
public class User implements Serializable {
    private static final long serialVersionUID = 8268426372054408603L;

    @TableId("id")
    private Long id;
    @TableField("name")
    private String name;
    @TableField("age")
    private Integer age;
    @TableField("email")
    private String email;
}

复制代码

配置

配置文件,一下是个人基本配置,其中mybatis-plus还有不少配置,这里就不贴出来了;github

其余的说明一下:spring

我把配置中的驱动给注释了,由于我在pom中没有指定数据库链接的版本,因此引入的是最新的8.0.13版本;而这个最新的版本结构图以下:sql

再来一张5.0版本的结构图:数据库

若是引入的是高版本的MySQL链接,而配置中也写了驱动,会报这么一个提示:springboot

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

把驱动的配置注释就好了;由于新版本会自动帮咱们选择哪个驱动bash

spring:
  datasource:
    druid:
      url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
      username: ***
      password: ****
#      driver-class-name: com.mysql.jdbc.Driver
      initial-size: 5
      max-active: 30
      min-idle: 2
      max-wait: 1234
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 5
#    driver-class-name: com.mysql.jdbc.Driver
mybatis-plus:
#  config-location: classpath:/mapper/*.xml
  type-aliases-package: com.example.demo.model

复制代码

这里我把config-location属性也注释掉了,由于个人项目在该路径下边没有xml文件,就注释了,否则会报错,找不到xml文件;

测试

DemoApplicationTests代码:

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
	@Resource
	private UserMapper userMapper;
	@Test
	public void contextLoads() {
		User user=new User();
		user.setName("jack");
		//条件查询
		System.out.println("条件查询");
		List<User> userList = userMapper.selectList(new QueryWrapper<>(user));
		userList.forEach(one-> System.out.println(one.toString()));
		//分页
		Page page=new Page(3,2);
		IPage iPage = userMapper.selectPage(page, null);
		List records = iPage.getRecords();
		System.out.println("分页");
		records.forEach(one-> System.out.println(one.toString()));
	}
}

复制代码

结果:

条件查询
User{id=2, name='Jack', age=20, email='test2@baomidou.com'}
分页
User{id=5, name='Billie', age=24, email='test5@baomidou.com'}

复制代码

总结

本篇文章主要写入门配置,后续主要写一篇mybatis-plus的一些其余的属性的配置的文章

github地址

参考文章

相关文章
相关标签/搜索