共计 3549 个字符,预计需要花费 9 分钟才能阅读完成。
当应用 Spring Boot 和 MyBatis 来实现对数据库的操作时,能够依照以下步骤进行配置和开发:
- 确保曾经在我的项目的
pom.xml
文件中增加了 Spring Boot 和 MyBatis 的依赖。
<!-- Spring Boot 依赖 --> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
<!-- MyBatis 依赖 --> | |
<dependency> | |
<groupId>org.mybatis.spring.boot</groupId> | |
<artifactId>mybatis-spring-boot-starter</artifactId> | |
</dependency> | |
<!-- MySQL 数据库驱动依赖 --> | |
<dependency> | |
<groupId>mysql</groupId> | |
<artifactId>mysql-connector-java</artifactId> | |
</dependency> |
- 配置数据库连贯信息。在
application.properties
文件中增加以下配置:
# 数据库连贯信息 | |
spring.datasource.url=jdbc:mysql://localhost:3306/db_example | |
spring.datasource.username=root | |
spring.datasource.password=123456 | |
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver | |
# MyBatis 配置 | |
mybatis.mapper-locations=classpath:mapper/*.xml |
请依据理论状况批改数据库连贯 URL、用户名和明码。
- 创立实体类(Entity)。例如,创立一个名为
User
的实体类,与数据库表的字段一一对应。
public class User { | |
private Long id; | |
private String name; | |
private String email; | |
// ... 其余字段的 getter 和 setter 办法 | |
} |
- 创立 Mapper 接口和 XML 映射文件。
在 UserMapper.java
中定义数据库操作的办法。
@Mapper | |
public interface UserMapper {List<User> getAllUsers(); | |
User getUserById(Long id); | |
void createUser(User user); | |
void updateUser(User user); | |
void deleteUser(Long id); | |
} |
在 UserMapper.xml
中编写 SQL 语句的映射配置。
<?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.repository.UserMapper"> | |
<resultMap id="userResultMap" type="com.example.entity.User"> | |
<id property="id" column="id"/> | |
<result property="name" column="name"/> | |
<result property="email" column="email"/> | |
<!-- 其余字段映射 --> | |
</resultMap> | |
<select id="getAllUsers" resultMap="userResultMap"> | |
SELECT * FROM users | |
</select> | |
<select id="getUserById" resultMap="userResultMap"> | |
SELECT * FROM users WHERE id = #{id} | |
</select> | |
<insert id="createUser"> | |
INSERT INTO users (name, email) VALUES (#{name}, #{email}) | |
</insert> | |
<update id="updateUser"> | |
UPDATE users SET name = #{name}, email = #{email} WHERE id = | |
#{id} | |
</update> | |
<delete id="deleteUser"> | |
DELETE FROM users WHERE id = #{id} | |
</delete> | |
</mapper> |
- 创立服务层(Service)。在
UserService.java
和UserServiceImpl.java
中定义对数据库操作的业务逻辑。
public interface UserService {List<User> getAllUsers(); | |
User getUserById(Long id); | |
void createUser(User user); | |
void updateUser(User user); | |
void deleteUser(Long id); | |
} |
@Service | |
public class UserServiceImpl implements UserService { | |
private final UserMapper userMapper; | |
public UserServiceImpl(UserMapper userMapper) {this.userMapper = userMapper;} | |
@Override | |
public List<User> getAllUsers() {return userMapper.getAllUsers(); | |
} | |
@Override | |
public User getUserById(Long id) {return userMapper.getUserById(id); | |
} | |
@Override | |
public void createUser(User user) {userMapper.createUser(user); | |
} | |
@Override | |
public void updateUser(User user) {userMapper.updateUser(user); | |
} | |
@Override | |
public void deleteUser(Long id) {userMapper.deleteUser(id); | |
} | |
} |
- 创立管制层(Controller)。在
UserController.java
中定义接口办法,并解决 HTTP 申请。
@RestController | |
@RequestMapping("/users") | |
public class UserController { | |
private final UserService userService; | |
public UserController(UserService userService) {this.userService = userService;} | |
@GetMapping | |
public List<User> getAllUsers() {return userService.getAllUsers(); | |
} | |
@GetMapping("/{id}") | |
public User getUserById(@PathVariable("id") Long id) {return userService.getUserById(id); | |
} | |
@PostMapping | |
public void createUser(@RequestBody User user) {userService.createUser(user); | |
} | |
@PutMapping("/{id}") | |
public void updateUser(@PathVariable("id") Long id, @RequestBody User user) {user.setId(id); | |
userService.updateUser(user); | |
} | |
@DeleteMapping("/{id}") | |
public void deleteUser(@PathVariable("id") Long id) {userService.deleteUser(id); | |
} | |
} |
当初,能够应用 Apifox 或其余 API 测试工具来测试这些接口。例如,发送 GET 申请到 /users
能够获取所有用户列表,发送 POST 申请到 /users
能够创立新用户,发送 PUT 申请到 /users/{id}
能够更新用户信息,发送 DELETE 申请到 /users/{id}
能够删除用户。
本文由 mdnice 多平台公布
正文完