1、引入MyBatis和MyBatis-Plus依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
<version>3.5.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
2、创建Mapper和xml文件
Mapper
public interface EmpMapper extends BaseMapper<Emp> {
List<Emp> query2();
void insertBatch(@Param("list") List<Emp> list);
}
xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.EmpMapper">
<insert id="insertBatch">
insert into emp (id, name, job, deptno)
values
<foreach collection="list" item="item" separator=",">
(#{item.id}, #{item.name}, #{item.job}, #{item.deptno})
</foreach>
</insert>
<select id="query2" resultType="com.example.model.Emp">
select *
from emp limit 0,5
</select>
</mapper>
3、创建Service和ServiceImpl
service
public interface EmpService extends IService<Emp> {
}
ServiceImpl
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
}
4、指定资源文件目录
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
如果不指定,可能会出现invalid bound statement not found。