目录
在当今的软件开发中,Spring Boot和MyBatis作为两个流行的Java框架,常常被开发者们用来构建稳健且高效的应用程序。本文将带领你逐步实现一个简单的用户管理系统,展示如何利用Spring Boot和MyBatis的强大功能,快速地实现数据持久化和业务逻辑管理。
1. 创建一个新的Spring Boot项目
首先,你可以使用Spring Initializr来创建一个新的Spring Boot项目。选择以下依赖:
- Spring Web
- MySQL Driver
- MyBatis Framework
2. 添加必要的依赖
在pom.xml
中添加以下依赖:
<dependencies>
<!-- Spring Boot Starter for MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- MySQL Driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3. 配置数据库连接
在src/main/resources/application.properties
文件中配置数据库连接信息:
# 数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
# MyBatis配置
mybatis.mapper-locations=classpath:mapper/*.xml
4. 创建实体类
在src/main/java/com/example/demo/model/User.java
中创建一个实体类:
package com.example.demo.model;
public class User {
private Long id;
private String username;
private String email;
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
5. 创建Mapper接口
在src/main/java/com/example/demo/mapper/UserMapper.java
中创建一个Mapper接口:
package com.example.demo.mapper;
import com.example.demo.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("SELECT * FROM users")
List<User> getAllUsers();
}
6. 创建Mapper XML文件
在src/main/resources/mapper/UserMapper.xml
中创建一个Mapper 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.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.demo.model.User">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="username" property="username" jdbcType="VARCHAR"/>
<result column="email" property="email" jdbcType="VARCHAR"/>
</resultMap>
<select id="getAllUsers" resultMap="BaseResultMap">
SELECT * FROM users
</select>
</mapper>
7. 创建Service类
在src/main/java/com/example/demo/service/UserService.java
中创建一个Service类:
package com.example.demo.service;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> getAllUsers() {
return userMapper.getAllUsers();
}
}
8. 创建Controller类
在src/main/java/com/example/demo/controller/UserController.java
中创建一个Controller类
package com.example.demo.controller;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List<User> getAllUsers() {
return userService.getAllUsers();
}
}
9. 启动应用程序
在src/main/java/com/example/demo/DemoApplication.java
中运行Spring Boot应用程序:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
10. 测试
启动应用程序后,访问http://localhost:8080/users
,你应该会看到从数据库中检索到的用户列表。
这就是一个完整的Spring Boot与MyBatis整合的示例。你可以根据需要扩展这个基础示例,添加更多功能和复杂的查询逻辑。
标签:java,示例,Spring,Boot,public,demo,import,com,example From: https://blog.csdn.net/apakmsls/article/details/139891218