1. springboot项目基本配置
springboot基本pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
主启动类,此处在 com.hex下建立
package com.hex;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
2. mysql准备工作
首先创建一个表
CREATE TABLE `sys_user` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
`user_name` VARCHAR(64) NOT NULL DEFAULT 'NULL' COMMENT '用户名',
`nick_name` VARCHAR(64) NOT NULL DEFAULT 'NULL' COMMENT '昵称',
`password` VARCHAR(64) NOT NULL DEFAULT 'NULL' COMMENT '密码',
`del_flag` INT(11) DEFAULT '0' COMMENT '删除标志(0代表未删除,1代表已删除)',
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='用户表'
引入mysql依赖
<!-- 引入mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
在resources下新建application.yml文件,配置mysql相关信息(按实际自行修改配置)
spring:
datasource:
url: jdbc:mysql://localhost:3306/db2022?characterEncoding=utf-8&serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
3. 使用mybaitsplus
引入依赖
<!-- 引入mybaitsplus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
创建user实体类,这里注意2点
- 在类定义前加注解
@TableName("sys_user")
,参数为表名 - 在主键上添加注解
@TableId
package com.hex.domain;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
/**
* 用户表(User)实体类
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("sys_user")
public class User implements Serializable {
private static final long serialVersionUID = -40356785423868312L;
/**
* 主键
*/
@TableId
private Long id;
/**
* 用户名
*/
private String userName;
/**
* 昵称
*/
private String nickName;
/**
* 密码
*/
private String password;
/**
* 删除标志(0代表未删除,1代表已删除)
*/
private Integer delFlag;
}
创建UserMapper类,继承 BaseMapper<T>
package com.hex.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hex.domain.User;
public interface UserMapper extends BaseMapper<User> {
}
在主启动类添加扫描mapper注解:@MapperScan("com.hex.mapper")
,参数为UserMapper的路径
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.hex.mapper")
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
4. 单元测试看效果
引入junit依赖
<!--单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
在test/java/com/hex
下,建立测试类 MapperTest
,
添加 @SpringBootTest
,使用 @Autowired
(或者@Resource
) 注入userMapper,如下
import com.hex.domain.User;
import com.hex.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 MapperTest {
@Autowired
public UserMapper userMapper;
@Test
public void testUserMapper(){
List<User> users = userMapper.selectList(null);
System.out.println(users);
}
}
结果可在控制台查看用户列表信息输出
2022-02-25 12:40:35.849 INFO 13272 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-02-25 12:40:38.134 INFO 13272 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
[User(id=1, userName=hex, nickName=hex, password=123456, delFlag=0)]