下面是我为您准备的完整攻略。
Spring Boot整合Mybatis的步骤
1. 添加Mybatis和Mybatis-spring-boot-starter依赖
在pom.xml文件中,添加如下的Mybatis和Mybatis-spring-boot-starter依赖:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
2. 创建数据源配置
在application.properties或application.yml中配置数据源:
spring:
datasource:
url: jdbc:mysql://localhost:3306/test_db?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
username: root
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
3. 创建Mybatis配置
创建mybatis-config.xml文件,配置Mybatis,在其中配置Mapper扫描路径:
<!-- mybatis-config.xml文件 -->
<configuration>
<typeAliases>
<!-- 在这里配置要扫描的实体类的包路径 -->
<package name="com.example.entity" />
</typeAliases>
<mappers>
<!-- 在这里配置Mapper文件所在的包路径 -->
<mapper class="com.example.mapper.UserMapper" />
</mappers>
</configuration>
4. 创建Mapper
创建Mapper接口,定义SQL映射:
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user WHERE id = #{id}")
User findById(@Param("id") Long id);
}
5. 创建实体类
创建实体类,对应数据库的表:
public class User {
private Long id;
private String name;
private String gender;
// getter和setter方法省略
}
6. 创建Service
创建Service层,实现业务逻辑:
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User findById(Long id) {
return userMapper.findById(id);
}
}
7. 创建Controller
创建Controller层,接收请求并调用Service:
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public User findById(@PathVariable("id") Long id) {
return userService.findById(id);
}
}
8. 运行应用程序
运行应用程序,访问http://localhost:8080/user/1
,即可看到id为1的用户信息。
示例说明
示例1
下面是一个简单的示例,展示如何使用Mybatis访问数据库。
-
创建一个Spring Boot应用程序。
-
添加Mybatis和Mybatis-spring-boot-starter依赖。
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
- 创建数据源配置。
spring:
datasource:
url: jdbc:mysql://localhost:3306/test_db?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
username: root
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
- 创建Mapper。
@Mapper
public interface UserMapper {
@Insert("INSERT INTO user(name, gender) VALUES(#{name}, #{gender})")
void addUser(User user);
@Select("SELECT * FROM user WHERE id = #{id}")
User findById(Long id);
}
- 创建实体类。
public class User {
private Long id;
private String name;
private String gender;
// getter和setter方法省略
}
- 创建Service。
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public void addUser(User user) {
userMapper.addUser(user);
}
public User findById(Long id) {
return userMapper.findById(id);
}
}
- 创建Controller。
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/")
public void addUser(@RequestBody User user) {
userService.addUser(user);
}
@GetMapping("/{id}")
public User findById(@PathVariable("id") Long id) {
return userService.findById(id);
}
}
示例2
下面是另一个示例,展示如何使用Mybatis实现多表关联查询。
-
创建一个Spring Boot应用程序。
-
添加Mybatis和Mybatis-spring-boot-starter依赖。
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
- 创建数据源配置。
spring:
datasource:
url: jdbc:mysql://localhost:3306/test_db?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
username: root
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
- 创建Mapper。
@Mapper
public interface OrderMapper {
@Select("SELECT o.*, u.name AS user_name, p.name AS product_name, p.price FROM `order` o LEFT JOIN user u ON o.user_id = u.id LEFT JOIN product p ON o.product_id = p.id WHERE o.id = #{id}")
OrderVO findById(@Param("id") Long id);
}
- 创建实体类。
public class Order {
private Long id;
private Long userId;
private Long productId;
// getter和setter方法省略
}
public class User {
private Long id;
private String name;
// getter和setter方法省略
}
public class Product {
private Long id;
private String name;
private double price;
// getter和setter方法省略
}
public class OrderVO {
private Long id;
private Long userId;
private String userName;
private Long productId;
private String productName;
private double productPrice;
// getter和setter方法省略
}
- 创建Service。
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
public OrderVO findById(Long id) {
return orderMapper.findById(id);
}
}
- 创建Controller。
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private OrderService orderService;
@GetMapping("/{id}")
public OrderVO findById(@PathVariable("id") Long id) {
return orderService.findById(id);
}
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Springboot整合mybatis的步骤 - Python技术站