1. 程式人生 > >SpringBoot的MyBatis的entity和dao類

SpringBoot的MyBatis的entity和dao類

一  實體類

package com.imooc.demo.entity;

import java.util.Date;

/**
* 區域資訊
* @author xiangze
*
*/
public class Area {
    // 主鍵ID
    private Integer areaId;
    // 名稱
    private String areaName;
    // 權重,越大越排前顯示
    private Integer priority;
    // 建立時間
    private Date createTime;
    // 更新時間
    private Date lastEditTime;

    public Integer getAreaId() {
        return areaId;
    }

    public void setAreaId(Integer areaId) {
        this.areaId = areaId;
    }

    public String getAreaName() {
        return areaName;
    }

    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }

    public Integer getPriority() {
        return priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getLastEditTime() {
        return lastEditTime;
    }

    public void setLastEditTime(Date lastEditTime) {
        this.lastEditTime = lastEditTime;
    }

}

二 dao類

package com.imooc.demo.dao;

import java.util.List;

import com.imooc.demo.entity.Area;

public interface AreaDao {
    /**
     * 列出區域列表
     *
     * @return areaList
     */
    List<Area> queryArea();

    /**
     * 根據Id列出具體區域
     *
     * @return area
     */
    Area queryAreaById(int areaId);

    /**
     * 插入區域資訊
     *
     * @param area
     * @return
     */
    int insertArea(Area area);

    /**
     * 更新區域資訊
     *
     * @param area
     * @return
     */
    int updateArea(Area area);

    /**
     * 刪除區域資訊
     *
     * @param areaId
     * @return
     */
    int deleteArea(int areaId);
}

三 dao的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.imooc.demo.dao.AreaDao">
    <select id="queryArea" resultType="com.imooc.demo.entity.Area">
        SELECT area_id, area_name,
        priority, create_time, last_edit_time
        FROM tb_area
        ORDER BY priority
        DESC
    </select>
    <select id="queryAreaById" resultType="com.imooc.demo.entity.Area">
        SELECT area_id, area_name,
        priority, create_time, last_edit_time
        FROM tb_area
        WHERE
        area_id=#{areaId}
    </select>
    <insert id="insertArea" useGeneratedKeys="true" keyProperty="areaId"
        keyColumn="area_id" parameterType="com.imooc.demo.entity.Area">
        INSERT INTO
        tb_area(area_name,priority,
        create_time,last_edit_time)
        VALUES
        (#{areaName},#{priority},
        #{createTime},#{lastEditTime})
    </insert>
    <update id="updateArea" parameterType="com.imooc.demo.entity.Area">
        update tb_area
        <set>
            <if test="areaName != null">area_name=#{areaName},</if>
            <if test="priority != null">priority=#{priority},</if>
            <if test="lastEditTime != null">last_edit_time=#{lastEditTime}</if>
        </set>
        where area_id=#{areaId}
    </update>
    <delete id="deleteArea">
        DELETE FROM
        tb_area
        WHERE
        area_id =
        #{areaId}
    </delete>
</mapper>