java springboot+mybatis实现人员增删改查的全部过程和代码
时间: 2024-12-04 07:29:55 浏览: 8
springboot+mybatis+mysql+layUI+thymeleaf实现增删改查
在Java Spring Boot项目中使用MyBatis实现人员管理的CRUD操作通常涉及以下几个步骤:
1. **准备环境**:
- 添加Spring Boot和MyBatis依赖到`pom.xml`文件:
```xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
</dependencies>
```
2. **创建实体类Person.java**:
```java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
// getters and setters
}
```
3. **配置数据源和MyBatis**:
在application.properties中添加数据库连接信息:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=password
```
配置MyBatis扫描包路径:
```properties
mybatis.mapper-locations=classpath:mapper/*.xml
```
4. **创建Mapper接口PersonMapper.java**:
```java
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Delete;
import com.example.demo.model.Person;
public interface PersonMapper {
@Insert("INSERT INTO person(name, email) VALUES(#{name}, #{email})")
int insert(Person person);
@Select("SELECT * FROM person WHERE id = #{id}")
Person selectById(Long id);
@Update("UPDATE person SET name=#{name}, email=#{email} WHERE id=#{id}")
int update(Person person);
@Delete("DELETE FROM person WHERE id = #{id}")
int deleteById(Long id);
}
```
5. **编写Repository接口PersonRepository.java**,利用JpaRepository接口扩展:
```java
import org.springframework.data.jpa.repository.JpaRepository;
public interface PersonRepository extends JpaRepository<Person, Long> {
}
```
6. **Service层PersonService.java**:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PersonService {
private final PersonRepository repository;
@Autowired
public PersonService(PersonRepository repository) {
this.repository = repository;
}
// CRUD methods using the repository
}
```
7. **Controller层PersonController.java**:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/persons")
public class PersonController {
private final PersonService service;
@Autowired
public PersonController(PersonService service) {
this.service = service;
}
// Implement HTTP endpoints for CRUD operations
@PostMapping
public ResponseEntity<?> create(@RequestBody Person person) {
return ResponseEntity.ok(service.insert(person));
}
// ... (implement other methods like @GetMapping, @PutMapping, @DeleteMapping)
}
```
注意:这只是一个基础示例,实际应用中可能还需要处理事务、异常处理以及分页查询等。每个方法的实现细节取决于具体的数据库表结构。
阅读全文