SpringBoot集成Mybatis的实现步骤

  • Post category:http

SpringBoot集成Mybatis的实现步骤

SpringBoot集成Mybatis是一个常见的Java Web开发任务。本文将提供详细的实现步骤,包括两个示例说明。

实现步骤

  1. 添加Mybatis和数据库驱动依赖。

在pom.xml文件中添加Mybatis和数据库驱动依赖。

<dependencies>
    <!-- Mybatis -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.0</version>
    </dependency>

    <!-- 数据库驱动 -->
    <dependency>
        <groupId>com.mysql.cj</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.26</version>
    </dependency>
</dependencies>
  1. 配置数据源和Mybatis。

在application.properties或application.yml文件中配置数据源和Mybatis。

# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
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
mybatis.type-aliases-package=com.example.demo.entity
  1. 创建实体类和Mapper接口。

创建实体类和Mapper接口,用于映射数据库表和SQL语句。

// 实体类
public class User {
    private Long id;
    private String name;
    private Integer age;
    // getter和setter方法
}

// Mapper接口
@Mapper
public interface UserMapper {
    @Select("SELECT * FROM user WHERE id = #{id}")
    User findById(Long id);
}
  1. 编写Service和Controller。

编写Service和Controller,用于处理业务逻辑和HTTP请求。

// Service
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public User findById(Long id) {
        return userMapper.findById(id);
    }
}

// Controller
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/user/{id}")
    public User findById(@PathVariable Long id) {
        return userService.findById(id);
    }
}
  1. 运行应用程序。

运行SpringBoot应用程序,并访问http://localhost:8080/user/{id},其中{id}为数据库中的用户ID。

示例1:查询用户信息

假设我们有一个名为“user”的数据库表,含id、name和age三个字段。我们想要查询id为1的用户信息。

解决方案:

  1. 创建User实体类和UserMapper接口。
// 实体类
public class User {
    private Long id;
    private String name;
    private Integer age;
    // getter和setter方法
}

// Mapper接口
@Mapper
public interface UserMapper {
    @Select("SELECT * FROM user WHERE id = #{id}")
    User findById(Long id);
}
  1. 创建UserService和UserController。
// Service
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public User findById(Long id) {
        return userMapper.findById(id);
    }
}

// Controller
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/user/{id}")
    public User findById(@PathVariable Long id) {
        return userService.findById(id);
    }
}
  1. 运行应用程序,并访问http://localhost:8080/user/1。

示例2:插入用户信息

假设我们有一个名为“user”的数据库表,包含id、name和age三个字段。我们想要插入一条新的用户信息。

解决方案:

  1. 创建User实体类和UserMapper接口。
// 实体类
public class User {
    private Long id;
    private String name;
    private Integer age;
    // getter和setter方法
}

// Mapper接口
@Mapper
public interface UserMapper {
    @Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})")
    void insert(User user);
}
  1. 创建UserService和UserController。
// Service
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public void insert(User user) {
        userMapper.insert(user);
    }
}

// Controller
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/user")
    public void insert(@RequestBody User user) {
        userService.insert(user);
    }
}
  1. 运行应用程序,并使用POST请求访问http://localhost:8080/user,传递JSON格式的用户信息。

总结

SpringBoot集成Mybatis是一个常见的Java Web开发任务。本文提供了详细的实现步骤,包括添加Mybatis和数据库驱动依赖、配置数据源和Mybatis、创建实体类和Mapper接口、编写Service和Controller等。在实际使用中,我们应该根据具体情况选择合适的解决方案,以确保能够正常运行SpringBoot应用程序。