mybatis-plus用mapper。xml
时间: 2023-04-04 09:01:57 浏览: 56
来映射 SQL 语句和 Java 方法,可以方便地进行数据库操作。您可以在 mapper.xml 文件中定义 SQL 语句,然后在 Java 代码中调用对应的方法来执行 SQL 语句。这样可以使代码更加清晰易懂,提高开发效率。
相关问题
mybatis-plus. mapper-locations
MyBatis-Plus is an enhanced version of MyBatis, which is a popular Java persistence framework for working with SQL databases. It provides additional features and simplifies the development process.
The `mapper-locations` property in MyBatis-Plus is used to specify the XML mapper files location. These XML files contain the SQL statements and mapping configurations for MyBatis to interact with the database.
You can specify multiple locations using comma-separated values or use wildcards to scan for XML files in a directory. For example:
```properties
mybatis-plus.mapper-locations=classpath*:mapper/**/*.xml
```
This configuration will search for XML files in the `mapper` directory and its subdirectories on the classpath.
You can also specify file paths using the `file:` prefix. For example:
```properties
mybatis-plus.mapper-locations=file:/path/to/mapper/*.xml
```
This configuration will look for XML files in the specified directory on the file system.
Make sure to provide the correct location(s) of your mapper XML files so that MyBatis-Plus can find and parse them correctly.
springboot 整合 mybatis-plus 和 mapper.xml
Spring Boot 整合 MyBatis-Plus 的步骤如下:
1.添加 MyBatis-Plus 和 MyBatis-Spring-Boot-Starter 依赖。在 pom.xml 文件中添加以下依赖:
```
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
```
2.添加数据库配置。在 application.yml 文件中添加以下配置:
```
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
username: root
password: root
```
3.创建 Mapper 接口。在 mapper 接口上添加 @Mapper 注解,并且继承 BaseMapper 接口,例如:
```
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
```
4.创建 Mapper XML 文件。在 resources 目录下创建 mapper 目录,并在该目录下创建与 Mapper 接口同名的 XML 文件,例如:UserMapper.xml。
```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.demo.entity.User">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="age" property="age" />
<result column="email" property="email" />
</resultMap>
<select id="selectById" resultMap="BaseResultMap">
select * from user where id=#{id}
</select>
</mapper>
```
5.在 Service 中调用 Mapper 接口。例如:
```
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(Long id) {
return userMapper.selectById(id);
}
}
```
这样,Spring Boot 就成功整合了 MyBatis-Plus 和 Mapper XML 文件。