1. 创建 Spring Boot 项目
你可以使用 Spring Initializr(https://start.spring.io/ )来创建一个新的 Spring Boot 项目,添加以下依赖:
- Spring Web
- MyBatis Framework
- MySQL Driver
2. 添加 TkMyBatis 依赖
在pom.xml中添加 TkMyBatis 的依赖:
xml
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.3.5</version>
</dependency>
3. 配置数据库连接
在application.properties中配置数据库连接信息:
properties
spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
4. 创建实体类
创建User实体类,对应数据库中的user表:
java
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "user")
public class User {
@Id
private Integer id;
private String username;
private String password;
// 构造方法、Getter和Setter方法
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
5. 创建 Mapper 接口
创建UserMapper接口,继承tk.mybatis.mapper.common.Mapper:
java
import tk.mybatis.mapper.common.Mapper;
public interface UserMapper extends Mapper<User> {
}
6. 创建 Service 接口和实现类
创建UserService接口:
java
import com.github.pagehelper.PageInfo;
import java.util.List;
public interface UserService {
int addUser(User user);
int deleteUser(Integer id);
int updateUser(User user);
User getUserById(Integer id);
PageInfo<User> getUsersByCondition(User user, int pageNum, int pageSize);
}
创建UserServiceImpl实现类:
java
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public int addUser(User user) {
return userMapper.insertSelective(user);
}
@Override
public int deleteUser(Integer id) {
return userMapper.deleteByPrimaryKey(id);
}
@Override
public int updateUser(User user) {
return userMapper.updateByPrimaryKeySelective(user);
}
@Override
public User getUserById(Integer id) {
return userMapper.selectByPrimaryKey(id);
}
@Override
public PageInfo<User> getUsersByCondition(User user, int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> users = userMapper.select(user);
return new PageInfo<>(users);
}
}
7. 创建 Controller 类
创建UserController类,处理 HTTP 请求:
java
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@PostMapping
public int addUser(@RequestBody User user) {
return userService.addUser(user);
}
@DeleteMapping("/{id}")
public int deleteUser(@PathVariable Integer id) {
return userService.deleteUser(id);
}
@PutMapping
public int updateUser(@RequestBody User user) {
return userService.updateUser(user);
}
@GetMapping("/{id}")
public User getUserById(@PathVariable Integer id) {
return userService.getUserById(id);
}
@GetMapping
public PageInfo<User> getUsersByCondition(@RequestParam(required = false) String username,
@RequestParam(required = false) String password,
@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "10") int pageSize) {
User user = new User();
user.setUsername(username);
user.setPassword(password);
return userService.getUsersByCondition(user, pageNum, pageSize);
}
}
8. 启动 Spring Boot 应用
创建Application类启动 Spring Boot 应用:
java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan(basePackages = "com.example.demo.mapper") // 修改为你的Mapper接口所在的包名
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
9.测试接口
你可以使用 Postman 或其他工具来测试以下接口:
- 添加用户:
POST http://localhost:8080/users,请求体为 JSON 格式的用户信息。 - 删除用户:
DELETE http://localhost:8080/users/{id} - 修改用户:
PUT http://localhost:8080/users,请求体为 JSON 格式的用户信息。 - 查询用户:
GET http://localhost:8080/users/{id} - 多条件分页查询:
GET http://localhost:8080/users?username=xxx&password=xxx&pageNum=1&pageSize=10
通过以上步骤,你就可以使用 Spring Boot 整合 TkMyBatis 实现用户的添加、删除、修改、查询以及多条件分页查询功能。
4975

被折叠的 条评论
为什么被折叠?



