本节主要介绍如何在新项目中集成Mybatis-plus;从而完成对一个数据库表的增删查改操作。
创建一个Springboot工程
--- 此处略过
引入对应的依赖包
完整的pom.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hank</groupId>
<artifactId>mybatisdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatisdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>druid</artifactId>-->
<!-- <version>1.1.20</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
以上内容,与Mybatis-plus相关主要为以下部分
<!-- mybatis -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>druid</artifactId>-->
<!-- <version>1.1.20</version>-->
<!-- </dependency>-->
Mybatis-plus是基于Mybatis的一个增强插件,默认具备Mybatis的所有特性外加分页功能;
最后我们可以在项目结构中最终映入的Jar包资源,其中与Mybatis-plus相关的资源如下
(在IDEA工具中,通过File-->Project Structure...-->Modules-->选中自己的项目-->Dependencies)
从以上我们可以看到,虽然我们只在pom.xml中引入了mybatis-plus-boot-starter,版本为3.3.1,但是最终实际引入了其他的相关资源包。
编码部分
添加启动扫描注解
在项目启动类添加启动注解扫描;如果不在启动类配置,也可以在每个mapper上加上@Mapper,容器会自动扫描加入
@SpringBootApplication
@MapperScan("com.hank.mybatisdemo.dao")
public class MybatisdemoApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisdemoApplication.class, args);
}
}
编写实体类和Mapper类
此处以T_Temp为例;注意,这里接口类需要继承BaseMapper类,泛型为User实体对象
package com.hank.mybatisdemo.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hank.mybatisdemo.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
如果是自定义查询或操作,可以通过直接在mapper中编写对应方法,如下
public interface UserMapper extends BaseMapper<User> {
@Select("select * from t_temp where user_name like '%${name}%'")
public List<User> findByName(String name);
}
User实体对象如下
package com.hank.mybatisdemo.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName(value = "T_Temp")
public class User {
@TableId(value = "id",type= IdType.AUTO)
Integer id;
@TableField(value = "user_name")
String userName;
Integer age;
String sex;
String remark;
}
对应表的结构如下
/*Table structure for table `t_temp` */
DROP TABLE IF EXISTS `t_temp`;
CREATE TABLE `t_temp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(30) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` varchar(2) DEFAULT NULL,
`remark` varchar(225) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb4;
配置文件
spring:
datasource:
# type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.mysql.cj.jdbc.MysqlDataSource
编写测试用例验证
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserTest {
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Autowired
UserMapper userMapper;
@Test
public void testUser(){
List<User> userList=userMapper.selectList(null);
userList.forEach(System.out::println);
}
@Test
public void testUser2(){
for (int i = 0; i <10 ; i++) {
User u=new User();
u.setUserName("张三"+i);
Random random = new Random();
u.setAge(random.nextInt());
u.setSex(i%2>1?"男":"女");
int row=userMapper.insert(u);
Assert.assertEquals(1, row);
}
}
}
继续支持XML形式
- 配置中加入 mapper.xml扫描地址
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
- 增加UserMapper.xml在resource的mappger目录
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hank.mybatisplugdemo.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.hank.mybatisplugdemo.entity.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
<result column="sex" property="sex" jdbcType="VARCHAR" />
<result column="remark" property="remark" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, user_name, age,sex,remark
</sql>
<select id="selectByName" parameterType="string" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM t_temp
WHERE user_name like '%${name}%'
</select>
</mapper>
修改对应UserMappger.java文件,增加对应的方法
public interface UserMapper extends BaseMapper<User> {
List<User> selectByName(@Param("name")String name);
}
测试类增加测试方法进行效果测试
@Test
public void selectByName() {
List<User> users = userMapper.selectByName("张");
users.forEach(System.out::println);
}
想要了解更多信息,可关注本公众号(一起学开源);或请长按以下二维码添加助手。将拉你加入社区进行更多交流