springboot整合mybatis实现增删改查
时间: 2023-06-05 10:47:24 浏览: 187
Spring Boot 集成 MyBatis 实现增删改查的步骤如下:
1. 在 pom.xml 文件中添加 MyBatis 依赖
2. 在 application.properties 文件中配置数据源信息
3. 编写 MyBatis 的映射文件
4. 编写 Mapper 接口
5. 编写 Service 和 Controller 类
6. 在启动类上添加 @MapperScan 注解,扫描 Mapper 接口
这样就可以使用 MyBatis 在 Spring Boot 中实现数据库的增删改查操作了。
相关问题
Springboot整合mybatis实现增删改查操作用swag去查看结果
Spring Boot整合MyBatis进行CRUD操作时,通常包括以下几个步骤:
1. 首先,你需要在Spring Boot项目中引入MyBatis依赖,并配置数据源、MapperScannerConfigurer扫描Mapper接口。
2. 定义实体类(Entity),映射数据库表结构;定义Mapper接口,声明执行增删改查的方法,如`insert`, `update`, `selectById`, `deleteById`等。
例如:
```java
// User.java (实体类)
public class User {
private int id;
private String name;
// getters and setters...
}
// UserMapper.java (Mapper接口)
@Mapper
public interface UserMapper {
@Insert("INSERT INTO users(name) VALUES(#{name})")
void insert(User user);
@Update("UPDATE users SET name = #{name} WHERE id = #{id}")
void update(User user);
@Select("SELECT * FROM users WHERE id = #{id}")
User selectById(@Param("id") int id);
@Delete("DELETE FROM users WHERE id = #{id}")
void deleteById(int id);
}
```
3. 使用Swag(Swagger)可以生成API文档,展示给前端开发人员。首先需要添加Swagger相关依赖,比如`swagger2-jackson2`、`springfox-swagger2` 和 `springfox-swagger-ui`。然后创建SwaggerConfig配置类,启用Swagger并设置基础路径。
4. 在Controller层,通过@Autowired注入Mapper,调用Mapper接口方法处理业务逻辑,并返回响应给前端。同时,在Controller里用@RestControllerAdvice注解的类上添加`@ApiOperation`注解来描述每个API的操作。
例如:
```java
@RestController
@RequestMapping("/api/users")
@Api(value = "用户管理", description = "用户相关的CRUD操作")
public class UserController {
@Autowired
private UserMapper userMapper;
@PostMapping
@ApiOperation("新增用户")
public ResponseEntity<?> createUser(@RequestBody User user) {
我希望通过springboot整合mybatis实现增删改查,请详细讲一讲UserMapper接口和UserMapper.xml的编写方法,最好结合例子说明
当我们使用SpringBoot整合Mybatis时,需要先在pom.xml文件中引入相关的依赖。常用的依赖有:
```xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
```
接下来,我们需要在application.properties文件中配置数据库连接信息:
```properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
```
其中,url中的db_test是数据库名称,可以根据实际情况进行修改。
接下来,我们可以开始编写UserMapper接口和UserMapper.xml。
UserMapper接口的编写:
```java
@Mapper
public interface UserMapper {
User selectById(@Param("id") int id);
List<User> selectAll();
int insert(User user);
int update(User user);
int deleteById(@Param("id") int id);
}
```
其中,@Mapper注解表示该接口是Mybatis的Mapper接口,SpringBoot会自动扫描该包下所有的Mapper接口并生成对应的实现类。
UserMapper.xml的编写:
```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="userMap" type="com.example.demo.entity.User">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="gender" property="gender"/>
</resultMap>
<select id="selectById" resultMap="userMap">
select * from user where id = #{id}
</select>
<select id="selectAll" resultMap="userMap">
select * from user
</select>
<insert id="insert" parameterType="com.example.demo.entity.User">
insert into user(name, age, gender) values(#{name}, #{age}, #{gender})
</insert>
<update id="update" parameterType="com.example.demo.entity.User">
update user set name = #{name}, age = #{age}, gender = #{gender} where id = #{id}
</update>
<delete id="deleteById">
delete from user where id = #{id}
</delete>
</mapper>
```
其中,namespace表示该Mapper接口的全限定名,resultMap用于将查询结果映射为Java对象,select、insert、update、delete分别对应查询、插入、更新、删除操作,id表示该操作的唯一标识符,parameterType表示该操作的参数类型,column表示数据库表的列名,property表示Java对象的属性名,#{id}、#{name}、#{age}、#{gender}分别表示Mybatis的占位符,用于替换参数。
以上就是使用SpringBoot整合Mybatis实现增删改查的基本方法,希望对你有所帮助。
阅读全文
相关推荐
















