springboot整合tkmybatis 实现用户添加、删除、修改、查询、多条件分页查询

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 实现用户的添加、删除、修改、查询以及多条件分页查询功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值