写一个掌握Mybatis注解的编写方法的java ee 代码
时间: 2024-12-24 22:29:04 浏览: 9
当然可以!下面是一个示例代码,展示了如何在 Java EE 应用中使用 MyBatis 注解来实现基本的 CRUD 操作以及一对多的查询。
### 1. 创建实体类
#### Student.java
```java
public class Student {
private int id;
private String name;
private int age;
private Class clazz;
// Getters and Setters
}
```
#### Class.java
```java
import java.util.List;
public class Class {
private int id;
private String className;
private List<Student> students;
// Getters and Setters
}
```
### 2. 创建 Mapper 接口
#### StudentMapper.java
```java
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface StudentMapper {
@Insert("INSERT INTO t_student (name, age, class_id) VALUES (#{name}, #{age}, #{clazz.id})")
void insertStudent(Student student);
@Update("UPDATE t_student SET name = #{name}, age = #{age} WHERE id = #{id}")
void updateStudent(Student student);
@Delete("DELETE FROM t_student WHERE id = #{id}")
void deleteStudent(int id);
@Select("SELECT * FROM t_student WHERE id = #{id}")
Student getStudentById(int id);
@Select("SELECT * FROM t_student")
List<Student> getAllStudents();
}
```
#### ClassMapper.java
```java
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface ClassMapper {
@Insert("INSERT INTO t_class (class_name) VALUES (#{className})")
void insertClass(Class clazz);
@Update("UPDATE t_class SET class_name = #{className} WHERE id = #{id}")
void updateClass(Class clazz);
@Delete("DELETE FROM t_class WHERE id = #{id}")
void deleteClass(int id);
@Select("SELECT * FROM t_class WHERE id = #{id}")
Class getClassById(int id);
@Select("SELECT * FROM t_class")
List<Class> getAllClasses();
@Select("SELECT * FROM t_student WHERE class_id = #{id}")
List<Student> getStudentsByClassId(int id);
}
```
### 3. 配置 Spring 和 MyBatis
#### application.properties
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.entity
```
#### MyBatisConfig.java
```java
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan(basePackages = "com.example.mapper")
public class MyBatisConfig {
}
```
### 4. 创建 Service 层
#### StudentService.java
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentService {
@Autowired
private StudentMapper studentMapper;
public void addStudent(Student student) {
studentMapper.insertStudent(student);
}
public void updateStudent(Student student) {
studentMapper.updateStudent(student);
}
public void deleteStudent(int id) {
studentMapper.deleteStudent(id);
}
public Student getStudentById(int id) {
return studentMapper.getStudentById(id);
}
public List<Student> getAllStudents() {
return studentMapper.getAllStudents();
}
}
```
#### ClassService.java
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ClassService {
@Autowired
private ClassMapper classMapper;
public void addClass(Class clazz) {
classMapper.insertClass(clazz);
}
public void updateClass(Class clazz) {
classMapper.updateClass(clazz);
}
public void deleteClass(int id) {
classMapper.deleteClass(id);
}
public Class getClassById(int id) {
return classMapper.getClassById(id);
}
public List<Class> getAllClasses() {
return classMapper.getAllClasses();
}
public List<Student> getStudentsByClassId(int id) {
return classMapper.getStudentsByClassId(id);
}
}
```
### 5. 创建 Controller 层
#### StudentController.java
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService studentService;
@PostMapping
public void addStudent(@RequestBody Student student) {
studentService.addStudent(student);
}
@PutMapping
public void updateStudent(@RequestBody Student student) {
studentService.updateStudent(student);
}
@DeleteMapping("/{id}")
public void deleteStudent(@PathVariable int id) {
studentService.deleteStudent(id);
}
@GetMapping("/{id}")
public Student getStudentById(@PathVariable int id) {
return studentService.getStudentById(id);
}
@GetMapping
public List<Student> getAllStudents() {
return studentService.getAllStudents();
}
}
```
#### ClassController.java
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/classes")
public class ClassController {
@Autowired
private ClassService classService;
@PostMapping
public void addClass(@RequestBody Class clazz) {
classService.addClass(clazz);
}
@PutMapping
public void updateClass(@RequestBody Class clazz) {
classService.updateClass(clazz);
}
@DeleteMapping("/{id}")
public void deleteClass(@PathVariable int id) {
classService.deleteClass(id);
}
@GetMapping("/{id}")
public Class getClassById(@PathVariable int id) {
return classService.getClassById(id);
}
@GetMapping
public List<Class> getAllClasses() {
return classService.getAllClasses();
}
@GetMapping("/{id}/students")
public List<Student> getStudentsByClassId(@PathVariable int id) {
return classService.getStudentsByClassId(id);
}
}
```
### 6. 数据库脚本
#### 创建数据库和表
```sql
CREATE DATABASE ssm;
USE ssm;
CREATE TABLE t_class (
id INT AUTO_INCREMENT PRIMARY KEY,
class_name VARCHAR(255) NOT NULL
);
CREATE TABLE t_student (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
age INT NOT NULL,
class_id INT,
FOREIGN KEY (class_id) REFERENCES t_class(id)
);
```
以上代码展示了如何使用 MyBatis 注解在 Java EE 应用中进行基本的 CRUD 操作以及一对多的查询。希望这对你有所帮助!
阅读全文