SpringBoot增删改查
Posted Jason_ou2021
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot增删改查相关的知识,希望对你有一定的参考价值。
XML mapper文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="AllUser" resultType="com.example.demo.Entity.UserEntity">
select * from user
</select>
<insert id="AddUser" useGeneratedKeys="true" keyProperty="id" parameterType="com.example.demo.Entity.UserEntity">
insert into user(name, passwd, create_time, update_time, is_delete) values(#{name},#{passwd},#{create_time},#{update_time},#{is_delete})
</insert>
<select id="GetOneUser" resultType="com.example.demo.Entity.UserEntity">
select * from user where id = (#{id})
</select>
<update id="UpdateUser" parameterType="com.example.demo.Entity.UserEntity">
update user set name = #{name}, passwd = #{passwd}, update_time = #{update_time}, is_delete = #{is_delete} where id = #{id}
</update>
<delete id="DeleteUser" parameterType="com.example.demo.Entity.UserEntity">
delete from user where id = #{id}
</delete>
</mapper>
Controller文件
package com.example.demo.controller;
import com.example.demo.Entity.UserEntity;
import com.example.demo.service.ServiceImpl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
@Controller
public class HelloController {
@Autowired
private UserServiceImpl userService;
@GetMapping("all_user")
@ResponseBody
public List<UserEntity> hello(){
return userService.AllUser();
}
@PostMapping("add_user")
@ResponseBody
public long add_user(String name, String passwd){
UserEntity userEntity = new UserEntity();
userEntity.setName(name);
userEntity.setPasswd(passwd);
userEntity.setCreate_time(new Date());
userEntity.setUpdate_time(new Date());
userEntity.setIs_delete("0");
return userService.AddUser(userEntity);
}
@GetMapping("get_one_user")
@ResponseBody
public UserEntity get_one_user(long id){
return userService.GetOneUser(id);
}
@PutMapping("update_user")
@ResponseBody
public int update_user(UserEntity user){
user.setUpdate_time(new Date());
return userService.UpdateUser(user);
}
@DeleteMapping("delete_user")
@ResponseBody
public int delete_user(long id){
return userService.DeleteUser(id);
}
}
服务接口类和mapper接口类代码一样的 注意:包名不一样
package com.example.demo.mapper;
import com.example.demo.Entity.UserEntity;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
List<UserEntity> AllUser();
long AddUser(UserEntity userEntity);
UserEntity GetOneUser(long id);
int UpdateUser(UserEntity user);
int DeleteUser(long id);
}
服务实现类
package com.example.demo.service.ServiceImpl;
import com.example.demo.Entity.UserEntity;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public List<UserEntity> AllUser() {
return userMapper.AllUser();
}
@Override
public long AddUser(UserEntity userEntity) {
return userMapper.AddUser(userEntity);
}
@Override
public UserEntity GetOneUser(long id) {
return userMapper.GetOneUser(id);
}
@Override
public int UpdateUser(UserEntity user) {
return userMapper.UpdateUser(user);
}
@Override
public int DeleteUser(long id) {
return userMapper.DeleteUser(id);
}
}
实体类
package com.example.demo.Entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.ToString;
import java.util.Date;
@Data
@ToString
public class UserEntity {
private long id;
private String name;
private String passwd;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date create_time;
@JsonFormat(shape= JsonFormat.Shape.STRING,pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date update_time;
private String is_delete;
}
数据库
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`passwd` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`create_time` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`update_time` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`is_delete` tinyint(3) NULL DEFAULT NULL COMMENT '0,否,1,是',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 22 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
SET FOREIGN_KEY_CHECKS = 1;
以上是关于SpringBoot增删改查的主要内容,如果未能解决你的问题,请参考以下文章