我想使用springboot和mybatis实现在数据库中增加一行内容和删除一行内容的功能,请给出usercontroller,userservice,usermapper,usermapper.xml中的相关代码并解释之
时间: 2024-03-12 18:47:52 浏览: 21
好的,以下是使用Spring Boot和MyBatis实现在数据库中增加一行内容和删除一行内容的功能的相关代码和说明:
1. 首先,在pom.xml文件中添加spring-boot-starter-web、spring-boot-starter-data-jpa和mybatis-spring-boot-starter的依赖,如下:
```xml
<dependencies>
<!--web相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--JPA相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--MySQL驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--MyBatis相关依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies>
```
2. 接着,需要在application.properties文件中配置数据库连接信息和MyBatis相关配置,如下:
```
spring.datasource.url=jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
# MyBatis相关配置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.entity
```
其中,url、username、password和driver-class-name需要根据自己的数据库情况进行配置,mybatis.mapper-locations用于指定mapper.xml文件的位置,mybatis.type-aliases-package用于指定实体类的包名。
3. 创建User实体类,代码如下:
```java
public class User {
private Long id;
private String name;
private Integer age;
// 省略getter和setter
}
```
4. 创建UserMapper接口,代码如下:
```java
@Mapper
public interface UserMapper {
List<User> findAll();
int add(User user);
int delete(Long id);
}
```
其中,@Mapper注解用于声明UserMapper是一个MyBatis的映射器接口,findAll方法用于查询所有用户信息,add方法用于添加一条用户信息,delete方法用于删除指定ID的用户信息。
5. 创建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="userResultMap" type="com.example.demo.entity.User">
<id column="id" jdbcType="BIGINT" property="id"/>
<result column="name" jdbcType="VARCHAR" property="name"/>
<result column="age" jdbcType="INTEGER" property="age"/>
</resultMap>
<select id="findAll" resultMap="userResultMap">
select * from user
</select>
<insert id="add" parameterType="com.example.demo.entity.User">
insert into user(name, age) values(#{name}, #{age})
</insert>
<delete id="delete" parameterType="Long">
delete from user where id=#{id}
</delete>
</mapper>
```
其中,namespace属性用于指定映射器接口的全限定名,resultMap用于指定查询结果集的映射关系,select、insert和delete标签分别对应查询、添加和删除操作。
6. 创建UserService类,代码如下:
```java
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> findAll() {
return userMapper.findAll();
}
public int add(User user) {
return userMapper.add(user);
}
public int delete(Long id) {
return userMapper.delete(id);
}
}
```
其中,@Service注解用于声明UserService是一个Spring Bean,@Autowired注解用于自动注入UserMapper实例,findAll方法用于查询所有用户信息,add方法用于添加一条用户信息,delete方法用于删除指定ID的用户信息。
7. 创建UserController类,代码如下:
```java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/list")
public List<User> list() {
return userService.findAll();
}
@PostMapping("/add")
public int add(@RequestBody User user) {
return userService.add(user);
}
@PostMapping("/delete")
public int delete(@RequestParam("id") Long id) {
return userService.delete(id);
}
}
```
其中,@RestController和@RequestMapping注解用于声明UserController是一个RESTful风格的控制器,@Autowired注解用于自动注入UserService实例,@GetMapping和@PostMapping注解用于声明GET和POST请求对应的处理方法,@RequestParam和@RequestBody注解用于接收请求参数。
以上就是使用Spring Boot和MyBatis实现在数据库中增加一行内容和删除一行内容的功能的相关代码和说明。