springboot整合mybatis实现学生课表的代码
时间: 2023-11-12 21:12:38 浏览: 154
这是一个简单的 Spring Boot 整合 MyBatis 的示例,实现学生课表的增删改查功能。在此示例中,使用了 MySQL 数据库,并且使用了 MyBatis Generator 自动生成了 DAO 和实体类。
1. 配置 pom.xml 文件,添加相关依赖。
```xml
<dependencies>
<!-- Spring Boot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MySQL JDBC driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- MyBatis Generator -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>
```
2. 配置 application.properties 文件,设置数据库连接信息。
```
spring.datasource.url=jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml
```
3. 创建实体类和 DAO。
Student.java
```java
public class Student {
private Long id;
private String name;
private Integer age;
// 其他属性省略
// getter 和 setter 方法省略
}
```
StudentMapper.java
```java
@Mapper
public interface StudentMapper {
List<Student> findAll();
Student findById(Long id);
void insert(Student student);
void update(Student student);
void deleteById(Long id);
}
```
4. 使用 MyBatis Generator 自动生成实体类和 DAO。
在 pom.xml 文件中添加以下配置:
```xml
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
```
在 src/main/resources 目录下创建 generatorConfig.xml 文件,配置 MyBatis Generator:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="default" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="${spring.datasource.driver-class-name}"
connectionURL="${spring.datasource.url}"
userId="${spring.datasource.username}"
password="${spring.datasource.password}">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="com.example.springbootmybatis.entity"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper"
targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.example.springbootmybatis.dao"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="student" domainObjectName="Student"/>
</context>
</generatorConfiguration>
```
在项目根目录下运行以下命令,即可生成实体类和 DAO:
```
mvn mybatis-generator:generate
```
5. 编写 Controller,实现增删改查功能。
```java
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentMapper studentMapper;
@GetMapping("")
public List<Student> findAll() {
return studentMapper.findAll();
}
@GetMapping("/{id}")
public Student findById(@PathVariable Long id) {
return studentMapper.findById(id);
}
@PostMapping("")
public void insert(@RequestBody Student student) {
studentMapper.insert(student);
}
@PutMapping("/{id}")
public void update(@PathVariable Long id, @RequestBody Student student) {
student.setId(id);
studentMapper.update(student);
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable Long id) {
studentMapper.deleteById(id);
}
}
```
以上就是一个简单的 Spring Boot 整合 MyBatis 实现学生课表的增删改查功能的示例。
阅读全文