Spring Boot的增删改查

在我刚开始学习Springboot时,我的师傅(算半个)叫我先写一个Springboot的增删改查,不管用什么办法,叫我在一周内交付代码,我就去谷歌搜,看别人博客里写的各种五花八门的办法,项目在别人手中好好的,一到我的手上就开始报错,而且这个bug改完,下一个bug就在不远处等着我解决,反正在解决bug的过程中也学习了很多,跑远了,说回正题,

先建一个Springboot项目,目录结构如下


image.png

给pom.xml文件添加的依赖


    
        org.springframework.boot
        spring-boot-starter-jdbc
    
    
        com.alibaba
        druid
        1.1.10
    
    
        mysql
        mysql-connector-java
        runtime
    
    
        com.baomidou
        mybatis-plus-boot-starter
        3.0.7.1
    
    
    
        com.alibaba
        druid-spring-boot-starter
        1.1.9
    
    
        org.springframework.boot
        spring-boot-starter-thymeleaf
    
    
        org.springframework.boot
        spring-boot-starter-web
    

    
    
        org.springframework.boot
        spring-boot-devtools
        true 
    

    
        org.springframework.boot
        spring-boot-starter-test
        test
    
    
        org.apache.shiro
        shiro-core
        1.2.4
    

entity层

public class UserEntity {
    private int id;
    private int type;
    private String username;
    private String password;
    private String address;
    private int age;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

     public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public int getType() {
        return type;
    }
    public void setType(int type) {
        this.type = type;
    }
}

mapper层

@Mapper
public interface UserMapper extends BaseMapper {

/**
 * 通过id查询用户信息
 */
@Select("SELECT * FROM user_entity WHERE id = #{id}")
List findUserByName(@Param("id") int id);



/**
 * 插入用户信息
 */
@Insert("INSERT INTO user_entity(id, username,password,age,address,type) VALUES(#{id}, #{username},#{password},#{age}, #{address}, #{type})")
void insertUser(@Param("id") int id, @Param("username") String username,@Param("password") String password,@Param("age") int age, @Param("address") String address,@Param("type") int type);

/**
 * 根据 id 更新用户信息
 */
@Update("UPDATE  user_entity SET id = #{id},username = #{username},password = #{password},age=#{age},address= #{address},type=#{type} WHERE id = #{id}")
void updateUser(@Param("id") int id, @Param("username") String username,@Param("password") String password,@Param("age") int age, @Param("address") String address,@Param("type") int type);

/**
 * 根据 id 删除用户信息
 */
@Delete("DELETE from user_entity WHERE id = #{id}")
void deleteUser(@Param("id") int id);
}

service层

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * 根据id查找用户
     */
    public List selectUserByName(int id) {
        return userMapper.findUserByName(id);
    }


    /**
     * 插入两个用户
     */
    public void insertService(int id,String username,String password,int age,String address,int type) {
        userMapper.insertUser(id, username ,password, age,address,type);
    }

    /**
     * 根据id 修改用户
     */

    public void Update(int id,String username,String password,int age,String address,int type){
        userMapper.updateUser(id, username ,password, age,address,type);
    }

    /**
     * 根据id 删除用户
     */

    public void deleteService(int id) {

        userMapper.deleteUser(id);
    }
}

controller层写增删改查方法

@Controller
@RequestMapping()
public class UsersController {

@Autowired
private UserService userService;

/**
 * 根据id查找用户
 */

@RequestMapping(value = "/query",method = RequestMethod.GET)
@ResponseBody
public List testQuery(int id) {
    return userService.selectUserByName(id);
}

/**
 * 插入两个用户
 */
@RequestMapping(value ="/insert",method = RequestMethod.GET)
public String testInsert(int id,String username,String password,int age,String address,int type) {
    userService.insertService(id, username ,password, age,address,type);
    //return userService.selectAllUser();
    return "user/delete";
}

/**
 * 根据id 修改用户
 */

@RequestMapping(value = "/update", method = RequestMethod.GET)
@ResponseBody
public String update(int id,String username,String password,int age,String address,int type) {
   userService.Update(id, username , password,age,address,type);
    return "修改成功";
}

/**
 * 根据id 删除用户
 */

@RequestMapping(value = "/delete",method = RequestMethod.GET)
public String testDelete(int id) {
    userService.deleteService(id);
    return "user/delete";
}
}

自定义属性配置

application.properties文件

server.port=8088
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot_test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

其实很简单,自从学习过Springboot后我就不喜欢SpringMVC了,哈哈哈,我就是一个喜新厌旧的坏银。。。

你可能感兴趣的:(Spring Boot的增删改查)