文章简介
本文介绍了springboot+mybatis实现数据库操作的两种实现方式,经测试均可运行使用。整体而言注解更加简便,但面对一些复杂查询,可能需要使用到注解方式。
注解方式
pom依赖:
<dependencies>
<!--连接数据库-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>
application.yaml配置文件
spring:
datasource:
url: jdbc:mysql://localhost:3306/mmall_learning?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver #com.mysql.cj.jdbc.Driver
Controller层
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/get_user", method = RequestMethod.GET)
public ResponseEntity<User> getUser(@RequestParam(value = "id") String userId) {
User user = userService.getUser(userId);
return ResponseEntity.status(HttpStatus.OK).body(user);
}
}
Service层
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUser(String userId) {
User user = userMapper.findById(userId);
return user;
}
}
Dao层
@Mapper
public interface UserMapper {
@Select("SELECT * FROM mmall_user WHERE ID = #{id}")
User findById(@Param("id") String id);
}
实体类
@Data
public class User {
private Integer id;
private String username;
private String password;
private String email;
}
评析:注解方式非常简便,没有额外的配置工作。
xml方式
xml方式相对来说会需要配置一些路径参数,如果各位想通过这种方式实现数据库操作,可以参照以下目录结构和对应路径配置来实现。
目录结构
pom依赖:
<dependencies>
<!--连接数据库-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<!-- Mybatis核心 仅xml方式需要加入该依赖,注解方式不需要 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
</dependencies>
application.yaml配置文件:
spring:
datasource:
url: jdbc:mysql://localhost:3306/mmall_learning?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver #com.mysql.cj.jdbc.Driver
# 配置Mybatis文件位置,告诉Spring Boot你需要扫描的位置
mybatis:
mapper-locations: classpath:com.example.demo.dao/*.xml
UserMapper.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.mmall.dao.UserMapper">
<select id="findById" parameterType="int" resultType="com.example.demo.mmall.bean.User">
SELECT * FROM mmall_user WHERE id = #{id}
</select>
</mapper>
Controller层
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/get_user", method = RequestMethod.GET)
public ResponseEntity<User> getUser(@RequestParam(value = "id") String userId) {
User user = userService.getUser(userId);
return ResponseEntity.status(HttpStatus.OK).body(user);
}
}
Service层
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUser(String userId) {
User user = userMapper.findById(Integer.valueOf(userId));
return user;
}
}
Dao层
@Mapper
public interface UserMapper {
User findById(@Param("id") Integer id);
}
实体类
@Data
public class User {
private Integer id;
private String username;
private String password;
private String email;
}
评析:XML方式需要做些额外的配置工作,包括新建xml映射文件、在application.yaml中配置扫描路径等。但总体来说,比原来spring+mybatis使用xml方式实现数据库操作的配置已经简便不少了。
测试结果
以上两种方式测试结果均如下所示,可以正常查询到所需数据。
觉得文章有帮助可以点个赞或关注支持一下哟~