springboot集成mybatis plus的代码
时间: 2023-02-27 21:44:39 浏览: 133
以下是集成 MyBatis Plus 到 Spring Boot 项目的代码示例:
1. 在 pom.xml 中添加 MyBatis Plus 依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
```
2. 配置 application.properties 或 application.yml 文件,添加数据源和 MyBatis Plus 的配置:
```yml
# 数据源配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: root
# MyBatis Plus 配置
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:/mapper/*.xml
```
3. 在启动类中添加 @MapperScan 注解,用于扫描 Mapper 接口:
```java
@SpringBootApplication
@MapperScan("com.example.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
4. 编写 Mapper 接口:
```java
public interface UserMapper extends BaseMapper<User> {
}
```
5. 在 mapper 目录下编写对应的 xml 文件:
```xml
<mapper namespace="com.example.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.entity.User">
<id column="id" property="id" />
<result column="username" property="username" />
<result column="password" property="password" />
</resultMap>
<sql id="Base_Column_List">
id, username, password
</sql>
<select id="selectById" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM user
WHERE id = #{id}
</select>
</mapper>
```
以上代码仅作为参考,实际开发过程中可能需
阅读全文