设为首页 加入收藏

TOP

Spring boot 整合各种功能(四)
2023-08-26 21:10:53 】 浏览:58
Tags:Spring boot

(4)dao接口和映射文件

public interface EmpDao {
    
    //增加
    public int insert(Emp emp);
    
    //删除
    public int deleteById(Integer id);
    
    //修改
    public int update(Emp emp);
    
    //根据id查询员工信息
    public Emp selectById(Integer id);
    
    //查询所有
    public List<Emp> selectAll();
}

<?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">
<!--namespace必须和dao接口的名称一模一样-->
<mapper namespace="com.ykq.dao.EmpDao">

    <!--返回你添加时数据库生成的主键值-->
    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into tbl_emp values(null,#{name},#{salary},#{birthday},#{deptId},#{headImg})
    </insert>
    <update id="update">
        update tbl_emp set name=#{name},salary=#{salary},birthday=#{birthday},dept_id=#{deptId},headImg=#{headImg}
        where id=#{id}
    </update>
    <delete id="deleteById">
        delete from tbl_emp where id=#{id}
    </delete>
    <!--sql片段-->
    <sql id="empSql">
        id,name,salary,birthday,dept_id deptId,headImg
    </sql>
    <select id="selectById" resultType="com.ykq.entity.Emp">
         select
          <include refid="empSql"/>
          from tbl_emp where id=#{id}
    </select>

    <!--查询所有员工携带部门信息-->
    <resultMap id="baseEmpMapper" type="com.ykq.entity.Emp" autoMapping="true">
          <id property="id" column="id"/>
        <result property="deptId" column="dept_id"/>
        <association property="dept" javaType="com.ykq.entity.Dept" autoMapping="true">
             <id property="did" column="did"/>
             <result property="dname" column="d_name"/>
        </association>
    </resultMap>
    <select id="selectAll" resultMap="baseEmpMapper">
        select * from tbl_emp e join tbl_dept d on e.dept_id=d.did
    </select>


</mapper>

(5)service业务层

public interface EmpService {
    
    public Result deleteById(int id);
    
    public Result update(Emp emp);
    
    public Result insert(Emp emp);
    
    public Result findById(Integer id);
    
    public Result findAll(Integer current,Integer pageSize);
}
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpDao empDao;
    @Override
    @Transactional
    public Result deleteById(int id) {
        int row = empDao.deleteById(id);
        return row>0?new Result(200,"删除成功",null):new Result(500,"删除失败",null);
    }

    @Override
    public Result update(Emp emp) {
        int row = empDao.update(emp);
        return row>0?new Result(200,"删除成功",null):new Result(500,"删除失败",null);
    }

    @Override
    public Result insert(Emp emp) {
        int row = empDao.insert(emp);
        return row>0?new Result(200,"删除成功",emp):new Result(500,"删除失败",null);
    }

    @Override
    public Result findById(Integer id) {
        Emp emp = empDao.selectById(id);
        return new Result(200,"查询成功",emp);
    }

    @Override
    public Result findAll(Integer current, Integer pageSize) {
        PageHelper.startPage(current,pageSize);
首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇java与es8实战之一:以builder pa.. 下一篇《深入理解Java虚拟机》读书笔记..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目