请给出一个用springboot整合mybatis的具体例子,要详细说明每一个关键的类,接口和xml文件
时间: 2024-02-27 15:53:55 浏览: 24
好的,下面我就给你提供一个使用Spring Boot整合MyBatis的具体例子,包括关键的类、接口和XML文件。
首先,需要在pom.xml文件中添加以下依赖:
```xml
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
```
其中,Spring Boot Starter依赖包含了Spring Boot应用所需的基本组件,MyBatis Spring Boot Starter可以方便地集成MyBatis和Spring Boot,MySQL Connector用于连接MySQL数据库。
接下来,创建一个实体类User:
```java
public class User {
private Long id;
private String username;
private String password;
// 省略getter和setter方法
}
```
然后,创建一个Mapper接口UserMapper:
```java
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user WHERE id = #{id}")
User findById(@Param("id") Long id);
@Insert("INSERT INTO user(username, password) VALUES(#{user.username}, #{user.password})")
void save(@Param("user") User user);
}
```
在Mapper接口上添加@Mapper注解,表示该接口是一个映射器,Spring Boot会自动为该接口生成实现类。使用@Select注解表示该方法是一个查询语句,使用@Insert注解表示该方法是一个插入语句。
接着,创建一个Mapper XML文件UserMapper.xml:
```xml
<mapper namespace="com.example.mapper.UserMapper">
<select id="findById" parameterType="java.lang.Long" resultType="com.example.entity.User">
SELECT * FROM user WHERE id = #{id}
</select>
<insert id="save" parameterType="com.example.entity.User">
INSERT INTO user(username, password) VALUES(#{username}, #{password})
</insert>
</mapper>
```
其中,namespace属性指定了该XML文件对应的Mapper接口的全限定名,id属性指定了该语句的唯一标识符,parameterType属性指定了该语句的参数类型,resultType属性指定了该语句的返回类型。
最后,创建一个Service类UserService:
```java
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User findById(Long id) {
return userMapper.findById(id);
}
public void save(User user) {
userMapper.save(user);
}
}
```
在Service类上添加@Service注解,表示该类是一个Spring Bean。使用@Autowired注解将UserMapper注入到UserService中,可以直接调用UserMapper中的方法。这样,我们就可以在业务逻辑中使用UserMapper来操作数据库。
以上就是一个简单的使用Spring Boot整合MyBatis的例子,包括关键的类、接口和XML文件。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)