Table of contents:
- springboot addition, deletion, modification and query
-
- Method to realize
- 1. Create the entity class corresponding to the data table, for example:
- 2. Create the corresponding Mapper interface based on the entity class, for example:
- 3. Define the corresponding SQL statement in the Mapper interface, for example:
- 4. Inject Mapper in the Service layer and perform corresponding business processing, for example:
- 5. Finally, perform corresponding request processing in the Controller layer, for example:
springboot addition, deletion, modification and query
Method to realize
First, make sure you have integrated the Spring Boot and MyBatis frameworks and configured the data source. Next, you can follow the steps below to implement the add, delete, modify, and check functions:
1. Create the entity class corresponding to the data table, for example:
public class User {
private Long id;
private String name;
private Integer age;
// 省略getter和setter方法
}
2. Create the corresponding Mapper interface based on the entity class, for example:
@Mapper
public interface UserMapper {
List<User> findAll();
User findById(Long id);
void save(User user);
void update(User user);
void delete(Long id);
}
3. Define the corresponding SQL statement in the Mapper interface, for example:
<select id="findAll" resultType="com.example.user.entity.User">
select * from user
</select>
<select id="findById" parameterType="Long" resultType="com.example.user.entity.User">
select * from user where id = #{
id}
</select>
<insert id="save" parameterType="com.example.user.entity.User">
insert into user(name, age) values(#{
name}, #{
age})
</insert>
<update id="update" parameterType="com.example.user.entity.User">
update user set name = #{
name}, age = #{
age} where id = #{
id}
</update>
<delete id="delete" parameterType="Long">
delete from user where id = #{
id}
</delete>
4. Inject Mapper in the Service layer and perform corresponding business processing, for example:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
@Override
public User findById(Long id) {
return userMapper.findById(id);
}
@Override
public void save(User user) {
userMapper.save(user);
}
@Override
public void update(User user) {
userMapper.update(user);
}
@Override
public void delete(Long id) {
userMapper.delete(id);
}
}
5. Finally, perform corresponding request processing in the Controller layer, for example:
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public User findById(@PathVariable("id") Long id) {
return userService.findById(id);
}
@GetMapping
public List<User> findAll() {
return userService.findAll();
}
@PostMapping
public void save(@RequestBody User user) {
userService.save(user);
}
@PutMapping
public void update(@RequestBody User user) {
userService.update(user);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable("id") Long id) {
userService.delete(id);
}
}
The above code is for reference only, and the specific implementation details and styles can be adjusted according to needs.