StringBoot+Mybatis的增删改查(一)

StringBoot+Mybatis的增删改查(一)

技术路线:采用String boot + Mybatis +MySQL;

编译软件:IDEA

一、项目描述

该项目主要针对数据字典进行操作,首先对数据类型进行增删改查,然后通过数据类型对数据项进行增删改查。

注意:针对数据项的查询是根据数据类型的名称进行模糊查询。

例如:数据类型其中一个元素名称为 :螺母;
假设数据项中元素有: 圆形螺母、圆形螺丝、方形螺母。
点击查询,可以获得圆形螺母、方形螺母。

二、项目目录
请添加图片描述

整体项目链接地址:

StringBoot+Mybatis的增删改查(一)
StringBoot+Mybatis的增删改查(二)
StringBoot+Mybatis的增删改查(三)
StringBoot+Mybatis的增删改查(四)
StringBoot+Mybatis的增删改查(五)

三、数据类型操作

1、Controller(PartController.java)

package com.xxx.controller;
import com.xxx.entity.Part;
import com.xxx.service.PartService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class PartController {

    @Autowired
    PartService partService;

    /**
     * 查询part表中所有信息
     * @param model
     * @return
     */
    @RequestMapping("/show")
    public String to_show(Model model){
        List<Part> part=partService.getAll();
        model.addAttribute("part", part);
        return "show";
    }

    /**
     * 根据ID删除数据库中信息
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public String to_delete(int id){
        partService.deleteById(id);
        return "redirect:/show";
    }

    /**
     * 跳转到part添加页面
     * @return
     */
    @GetMapping("/insert1")
    public String insert(){
        return "insert_part";
    }

    /**
     * 增加一条数据
     * @param part
     * @return
     */
    @GetMapping("/insert")
    public String to_insert(Part part){
        partService.insert(part);
        return "redirect:/show";
    }

    /**
     * 根据ID查询其中一条数据
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/getPartById")
    public String to_getPartById(int id,Model model) {
        Part part=partService.getPartById(id);
        model.addAttribute("part", part);
        return "update_part";
    }

    /**
     * 改变数据库中一条数据
     * @param part
     * @return
     */
    @GetMapping ("/updateById")
    public String to_update(Part part){
        partService.updateById(part.id,part.pid,part.p_name,part.p_state,part.p_describe);
        return "redirect:/show";
    }

}


2、entity(Part.java)

package com.xxx.entity;

public class Part {
    /*
     * 元件属性
     * */

    public int id;
    public int pid;
    public String p_name;
    public String p_state;
    public String p_describe;

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

    public int getPid() {
        return pid;
    }
    public void setPid(int pid){
        this.pid = pid;
    }

    public String getP_name(){
        return p_name;
    }
    public void setP_name(String p_name) {
        this.p_name = p_name;
    }

    public String getP_state(){
        return p_state;
    }
    public void setP_state(String p_state) {
        this.p_state = p_state;
    }

    public String getP_describe() {
        return p_describe;
    }
    public void setP_describe(String p_describe) {
        this.p_describe = p_describe;
    }
}

3、mapper(PartMapper.java)

注意:PartMapper是接口

package com.xxx.mapper;
import com.xxx.entity.Part;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface PartMapper {

    /**
     * 查询数据库中所有信息
     * @return
     */
    List<Part> getAll();

    /**
     * 根据ID查询其中一条数据
     * @param id
     * @return
     */
    Part getPartById(int id);

    /**
     * 根据ID删除数据库中信息
     * @param id
     * @return
     */
    boolean deleteById(int id);

    /**
     * 增加一条数据
     * @param part
     * @return
     */
    boolean insert(Part part);

    /**
     * 改变数据库中一条数据
     * @param id
     * @param pid
     * @param p_name
     * @param p_state
     * @param p_describe
     * @return
     */
    boolean updateById(@Param("id") int id,@Param("pid") int pid,@Param("p_name") String p_name,@Param("p_state") String p_state,@Param("p_describe") String p_describe);

}

4、server(PartServer.java)

package com.xxx.service;

import com.xxx.mapper.PartMapper;
import com.xxx.entity.Part;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PartService {

    @Autowired
    PartMapper partMapper;

    /**
     * 查询数据库中所有信息
     * @return
     */
    public List<Part> getAll(){
        return partMapper.getAll();
    }

    /**
     * 根据ID查询其中一条数据
     * @param id
     * @return
     */
    public Part getPartById(int id){
        return partMapper.getPartById(id);
    }

    /**
     * 根据ID删除数据库中信息
     * @param id
     * @return
     */
    public boolean deleteById(int id){
        // 用于判断sql语句是否执行
        boolean flag = false;
        try{
            partMapper.deleteById(id);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 增加一条数据
     * @param part
     * @return
     */
    public boolean insert(Part part){
        boolean flag = false;
        try {
            partMapper.insert(part);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 改变数据库中一条数据
     * @param id
     * @param pid
     * @param p_name
     * @param p_state
     * @param p_describe
     * @return
     */
    public boolean updateById(int id,int pid,String p_name,String p_state,String p_describe){
        boolean flag = false;
        try {
            partMapper.updateById(id,pid,p_name,p_state,p_describe);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

}

5、mybatis(PartMapper.xml)

<?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.xxx.mapper.PartMapper">
    <!--getAll 查询所有数据-->
    <select id="getAll" resultType="com.xxx.entity.Part">
        select *
        from part
    </select>
    <!--getPartById 根据ID查询数据-->
    <select id="getPartById" resultType="com.xxx.entity.Part">
        select *
        from part
        where id=#{id}
    </select>
    <!--deleteById 根据ID删除一条数据-->
    <delete id="deleteById" parameterType="com.xxx.entity.Part">
        delete from part
        where id = #{id}
    </delete>
    <!--insert 增加一条数据-->
    <insert id="insert" parameterType="com.xxx.entity.Part">
        insert into part(pid,p_name,p_state,p_describe)
        values(#{pid},#{p_name},#{p_state},#{p_describe})
    </insert>
    <!--update 根据ID改变一条数据-->
    <update id="updateById" parameterType="com.xxx.entity.Part">
        update part
        <set>
            <if test="pid != null">
                pid = #{pid},
            </if>
            <if test="p_name != null">
                p_name = #{p_name},
            </if>
            <if test="p_state != null">
                p_state = #{p_state},
            </if>
            <if test="p_describe != null">
                p_describe = #{p_describe}
            </if>
        </set>
            where id =#{id}
    </update>
</mapper>

后续在我Java专栏里。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

傻猴儿

小编,多谢客官留下的赏钱。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值