下面给出Spring Boot结合Mybatis实现创建数据库表的方法攻略。
步骤1:创建Spring Boot项目
首先要创建一个基于Spring Boot的项目,可以使用Spring Initializr快速创建,下面是相关的POM文件配置:
<!-- MyBatis和MyBatis-Spring的依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- MySQL连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
步骤2:配置数据库连接
在 application.properties
文件中配置数据库连接信息,示例如下:
# MySQL 配置
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=123456
步骤3:编写实体类及Mapper接口
在项目中定义相应的实体类和Mapper接口。实体类应该与数据表的结构信息相同:
@Data
public class User {
private Integer id;
private String name;
private Integer age;
}
Mapper接口中定义需要实现的数据访问函数:
public interface UserMapper {
List<User> findAll();
void saveUser(User user);
}
步骤4:编写数据库表SQL语句
编写 schema.sql
文件来创建数据库表,例如下面的SQL语句:
CREATE TABLE `user`
(
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) DEFAULT NULL,
`age` INT(11) DEFAULT NULL,
PRIMARY KEY (`id`)
);
步骤5:创建数据表
在Spring Boot启动时,通过Mybatis的MapperScan注解扫描Mapper接口,然后使用org.springframework.jdbc.core.JdbcTemplate
执行上述 schema.sql
文件,来创建数据表:
@SpringBootApplication
@MapperScan("com.example.mapper")
public class Application implements CommandLineRunner {
private final JdbcTemplate jdbcTemplate;
@Autowired
public Application(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
jdbcTemplate.execute("DROP TABLE IF EXISTS `user`;");
jdbcTemplate.execute("CREATE TABLE `user` (`id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`))");
}
}
示例一:Spring Boot集成Mybatis的RESTful API
下面演示通过SpringBoot结合Mybatis实现一个简单的RESTful API来插入和查询数据,首先是编写controller代码:
@RestController
@RequestMapping("/api/v1/user")
public class UserController {
private final UserMapper userMapper;
@Autowired
public UserController(UserMapper userMapper) {
this.userMapper = userMapper;
}
@GetMapping
public List<User> findAll() {
return userMapper.findAll();
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public void saveUser(@RequestBody User user) {
userMapper.saveUser(user);
}
}
然后是编写Service代码:
@Service
public class UserServiceImpl implements UserService {
private final UserMapper userMapper;
@Autowired
public UserServiceImpl(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public List<User> findAll() {
return userMapper.findAll();
}
@Override
public void saveUser(User user) {
userMapper.saveUser(user);
}
}
最后,编写Mapper代码:
@Mapper
@Repository
public interface UserMapper {
List<User> findAll();
void saveUser(User user);
}
示例二:Spring Boot集成Mybatis的Thymeleaf页面应用
下面演示通过SpringBoot结合Mybatis实现一个简单的网站应用,使用Thymeleaf作为页面渲染。首先是编写首页 index.html
的代码:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Spring Boot + MyBatis Example</title>
</head>
<body>
<h1>Hello, Spring Boot + MyBatis</h1>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
</tr>
<tr th:each="user : ${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
</tr>
</table>
<br>
<form method="POST" th:action="@{/}">
<input type="text" name="name" required>
<input type="number" name="age" required>
<input type="submit" value="提交">
</form>
</body>
</html>
然后是编写Controller代码:
@Controller
public class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/")
public String index(Model model) {
List<User> users = userService.findAll();
model.addAttribute("users", users);
return "index";
}
@PostMapping("/")
public String saveUser(User user) {
userService.saveUser(user);
return "redirect:/";
}
}
最后是我们编写的Service代码:
public interface UserService {
List<User> findAll();
void saveUser(User user);
}
@Service
public class UserServiceImpl implements UserService {
private final UserMapper userMapper;
@Autowired
public UserServiceImpl(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public List<User> findAll() {
return userMapper.findAll();
}
@Override
public void saveUser(User user) {
userMapper.saveUser(user);
}
}
Mapper代码如前述第一例子,此处不再赘述。
这就是整个攻略的完整过程,希望能够帮助您。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:SpringBoot结合Mybatis实现创建数据库表的方法 - Python技术站