Springboot实现最简单的增删改查

1.建立一个数据表

 2.进行代码编写

         2.1application.yml配置信息

spring:
  #profiles: dev  # 本地开发环境配置
  # 关闭thymeleaf页面缓存
  mvc:
    view:
      prefix: /WEB-INF/webpage/
      suffix: .jsp
    pathmatch:
      matching-strategy: ant_path_matcher
  thymeleaf:
    cache: false
  main:
    allow-circular-references: true



  #配置数据源
  datasource:
    platform: mysql
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/test
    druid:
      initialSize: 1
      minIdle: 200
      maxActive: 1000
      maxWait: 60000
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT 1
      validationQueryTimeout: 60000
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
      maxOpenPreparedStatements: 20
      connection-init-sqls: SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci



# MyBatis配置
mybatis:
  configuration:
    #开启驼峰命名匹配映射
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  #配置MyBatis的xml映射文件路径
  mapper-locations: classpath:/mapper/*.xml

        2.2封装实体类

package com.example.demo.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Test {
    Integer identify;
    Integer age;
    String department;
    String hone;
    String name;
    String political;
    String post;
    String sex;
    String tel;
}

  2.3写dao层

package com.example.demo.dao;

import com.example.demo.pojo.Test;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {

    void adduser(Test test);
    void deuser(Integer identify);
    void upuser(Test test);


    List<Test> seuser(Test test);

}

       2.4写映射文件

<?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.dao.UserMapper">
    <insert id="adduser">
        insert into doc values(#{identify},#{name},#{sex},#{age},#{post},#{department},#{tel},#{political},#{hone})
    </insert>
    <delete id="deuser" parameterType="Integer">
        delete from doc where identify=#{identify}
    </delete>
    <update id="upuser">
        update doc
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="sex != null and sex != ''">
                sex = #{sex},
            </if>
            <if test="age != null">
                age = #{age},
            </if>
            <if test="post != null and post != ''">
                post = #{post},
            </if>
            <if test="department != null and department != ''">
                department = #{department},
            </if>
            <if test="tel != null and tel != ''">
                tel = #{tel},
            </if>
            <if test="political != null and political != ''">
                political = #{political},
            </if>
            <if test="hone != null and hone != ''">
                hone = #{hone},
            </if>
        </set>
        where identify = #{identify}
    </update>
    <select id="seuser" resultType="com.example.demo.pojo.Test">
        select
        name, sex, age, post, department, tel, identify, political, hone
        from doc
        <where>
            <if test="name != null and name != ''">
                and name = #{name}
            </if>
            <if test="sex != null and sex != ''">
                and sex = #{sex}
            </if>
            <if test="age != null">
                and age = #{age}
            </if>
            <if test="post != null and post != ''">
                and post = #{post}
            </if>
            <if test="department != null and department != ''">
                and department = #{department}
            </if>
            <if test="tel != null and tel != ''">
                and tel = #{tel}
            </if>
            <if test="identify != null and identify != ''">
                and identify = #{identify}
            </if>
            <if test="political != null and political != ''">
                and political = #{political}
            </if>
            <if test="hone != null and hone != ''">
                and hone = #{hone}
            </if>
        </where>
    </select>


</mapper>

 3.service层

package com.example.demo.service;

import com.example.demo.dao.UserMapper;
import com.example.demo.pojo.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;
    public void adduser(Test test) {
        userMapper.adduser(test);
    }


    public void deuser(Integer identify){
        userMapper.deuser(identify);
    }
    public void upuser(Test test){
        userMapper.upuser(test);
    }
    public List<Test> seuser(Test test){
        return userMapper.seuser(test);

    }

}

4.controller

package com.example.demo.controller;

import com.example.demo.data.Result;
import com.example.demo.pojo.Test;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class Usercontroller {

    @Autowired
    UserService userService;



    @RequestMapping("/login")
    public Result add(Test test) {
        try {
            userService.adduser(test);
            return Result.sucess();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @RequestMapping("/login/php")
    public Result delete(Integer identify) {
        try {
            userService.deuser(identify);
            return Result.sucess();
        } catch (Exception e) {
            return Result.error();


        }
    }
    @RequestMapping("/pox")
    public Result update(Test test){
        try{
            userService.upuser(test);
            return Result.sucess();
        }catch (Exception e){
            return Result.error();
        }
    }
    @RequestMapping("/pho")
    public  Result select(Test test){
        try{
            List<Test> seuser = userService.seuser(test);
            return Result.sucess(seuser);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }


}

5.封装消息传给前端

package com.example.demo.data;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {
    String message;
    int code;
    Object object;

    public static Result sucess(Object object){
        return new Result("sd",1, object);
    }

    public static Result sucess(){
        return new Result("sd",1,null);
    }

    public static Result error(){
        return new Result("失败",0,"sd");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值