用spring boot整合mybatis需要添加什么依赖
时间: 2024-01-29 21:03:55 浏览: 34
要使用Spring Boot整合MyBatis,您需要添加以下依赖关系:
```xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
```
其中,`mybatis-spring-boot-starter`提供了Spring Boot集成MyBatis所需的所有依赖项,`h2`是一个内存型数据库,用于测试和开发。
请注意,以上版本号仅供参考,您可以在Maven仓库中查找最新版本。
相关问题
spring boot整合mybatis
Spring Boot整合MyBatis可以通过以下步骤完成:
1. 添加相关依赖:在pom.xml文件中添加Spring Boot和MyBatis的依赖,例如:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
```
2. 配置数据源:在application.properties文件中配置数据库连接信息,例如:
```
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
```
3. 创建MyBatis的Mapper接口:创建Mapper接口,并使用@Mapper注解标识,例如:
```
@Mapper
public interface UserMapper {
List<User> findAll();
void addUser(User user);
void updateUser(User user);
void deleteUser(int id);
}
```
4. 创建Mapper映射文件:创建Mapper映射文件,例如UserMapper.xml,编写SQL语句,并将Mapper接口与映射文件进行绑定,例如:
```
<?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.mapper.UserMapper">
<select id="findAll" resultType="com.example.entity.User">
select * from user
</select>
<insert id="addUser" parameterType="com.example.entity.User">
insert into user(name, age, gender) values(#{name}, #{age}, #{gender})
</insert>
<update id="updateUser" parameterType="com.example.entity.User">
update user set name=#{name}, age=#{age}, gender=#{gender} where id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id}
</delete>
</mapper>
```
5. 注入Mapper接口:在Service或Controller中注入Mapper接口,并使用@Autowired注解,例如:
```
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
@Override
public void addUser(User user) {
userMapper.addUser(user);
}
@Override
public void updateUser(User user) {
userMapper.updateUser(user);
}
@Override
public void deleteUser(int id) {
userMapper.deleteUser(id);
}
}
```
以上就是Spring Boot整合MyBatis的基本步骤。
spring boot 整合mybatis
### 回答1:
Spring Boot可以很方便地与MyBatis进行整合,只需要在pom.xml文件中添加相关依赖,配置数据源和MyBatis的配置文件即可。
1. 添加依赖
在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
```
2. 配置数据源
在application.properties文件中配置数据源:
```
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
```
3. 配置MyBatis
在application.properties文件中配置MyBatis:
```
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.entity
```
其中,mapper-locations表示MyBatis的映射文件所在的位置,type-aliases-package表示实体类所在的包名。
4. 编写Mapper接口和映射文件
在Mapper接口中定义SQL语句,例如:
```
public interface UserMapper {
@Select("SELECT * FROM user WHERE id = #{id}")
User getUserById(Integer id);
}
```
在映射文件中编写SQL语句,例如:
```
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="getUserById" parameterType="java.lang.Integer" resultType="com.example.demo.entity.User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
```
5. 使用Mapper接口
在需要使用Mapper接口的地方注入Mapper接口即可使用,例如:
```
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(Integer id) {
return userMapper.getUserById(id);
}
}
```
以上就是Spring Boot整合MyBatis的基本步骤。
### 回答2:
Spring Boot是一个Java web应用程序框架,它可以自动配置和快速构建应用程序。MyBatis是一种流行的持久化框架,使用XML或注释配置映射器和SQL语句。
要整合Spring Boot和MyBatis,需要进行以下步骤:
1. 添加mybatis-spring-boot-starter依赖
在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>x.x.x</version>
</dependency>
```
2. 配置数据源
在application.properties或application.yml文件中配置数据源。例如:
```
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=password
```
3. 配置MyBatis
在application.properties或application.yml文件中添加以下配置:
```
mybatis.mapper-locations=classpath:mapper/*.xml
```
这将告诉MyBatis mapper文件所在的位置。
4. 创建DAO接口
创建DAO接口,使用MyBatis注解或XML配置SQL语句。例如:
```
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user WHERE id = #{id}")
User findById(Long id);
}
```
或者:
```
public interface UserMapper {
User findById(@Param("id") Long id);
}
```
在MyBatis-Spring中,@Mapper注解会自动扫描接口并创建DAO实现类。
5. 注入DAO Bean
使用自动注入或手动注入将DAO Bean注入到服务中。例如:
```
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User findById(Long id) {
return userMapper.findById(id);
}
}
```
6. 启动应用程序
使用Spring Boot应用程序的主类启动应用程序。
综上所述,将Spring Boot和MyBatis整合是相当简单的,只需要添加依赖项,配置数据源和MyBatis,编写DAO接口以及将其注入到服务中即可。整个过程需要保持良好的代码规范和配置文件的正确性。
### 回答3:
Spring Boot是Spring Framework的一个扩展,它提供了一种快速、方便的开发方式。MyBatis是一个流行的ORM框架,它是基于Java的持久化框架,可以轻松地将Java对象映射到关系型数据库中。
Spring Boot整合MyBatis可以方便地构建Web应用程序,使得操作数据库变得更加容易。下面是Spring Boot整合MyBatis的步骤:
1. 添加依赖
添加Maven或Gradle依赖项,包括Spring Boot和MyBatis:
Maven:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
```
Gradle:
```groovy
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0'
}
```
2. 配置数据源
在application.properties中配置数据库连接池:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/myDatabase
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
```
3. 配置MyBatis
在配置文件中添加以下配置:
```properties
mybatis.mapper-locations=classpath:mapper/*.xml
```
这将加载在类路径下的所有mapper文件。
4. 创建Mapper接口
创建Mapper接口:
```java
@Repository
public interface UserMapper {
User findById(int id);
}
```
5. 创建Mapper.xml 文件
在resources/mapper目录下创建Mapper.xml文件:
```xml
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="findById" parameterType="int" resultType="com.example.demo.entity.User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
```
6. 编写Service
创建Service类:
```java
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User findById(int id){
return userMapper.findById(id);
}
}
```
7. 编写Controller
创建Controller类:
```java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public User findById(@PathVariable int id) {
return userService.findById(id);
}
}
```
当访问/user/{id}时,将通过UserService来获取User对象,并将其作为JSON响应返回。
以上就是Spring Boot整合MyBatis的步骤。简单易懂,且使用方便、灵活。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)