plus

<!--mybatis plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.1.9</version>
        </dependency>

        <!-- mybatis plus start -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatisplus-spring-boot-starter</artifactId>
            <version>1.0.5</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.1.9</version>
        </dependency>
/**
     * <p>
     * 插入(批量),该方法不适合 Oracle
     * </p>
     *
     * @param entityList 实体对象列表
     * @return boolean
     */
    boolean insertBatch(List<T> entityList);

 

对象

package com.cn.oa.admin.model.obj;

import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import lombok.Data;

import java.io.Serializable;

/**
 * Created by Administrator on 2019/11/10 0010.
 */
@Data
@TableName("gs_develop")
public class Example extends Model<Example> {

    /**
     * 主键ID
     */
    @TableId(type = IdType.AUTO)
    private Integer id;

    /**
     * 1、有效    -1、无效
     */
    private Integer status;

    @TableField("create_id")
    private Integer createId;
    @TableField("create_time")
    private String createTime;
    @TableField("modify_id")
    private Integer modifyId;
    @TableField("modify_time")
    private String modifyTime;

    //审核 1=通过  2=驳回  3=待审核
    @TableField(exist = false)
    private Integer checks;

    //    驳回理由
    @TableField(exist = false)
    private String reject;

    private static final long serialVersionUID = 1L;

    @Override
    protected Serializable pkVal() {
        return this.id;
    }


}

 

xml

<!--insertCostTypeList 批量新增二级-->
    <insert id="insertCostTypeList" parameterType="List">
        INSERT INTO  cost_type(type_name,father_id,`status`,create_id,create_time) VALUES
        <foreach collection="List" item="cost" index="index" separator=",">
            (#{cost.typeName},#{cost.fatherId},#{cost.status},#{cost.createId},#{cost.createTime})
        </foreach>
    </insert>
INSERT INTO  cost_type(type_name,father_id,`status`,create_id,create_time) 
VALUES (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?)

 

plus:

XXService.insertBatch()

 

<!--updateCostType 批量修改-->
    <update id="updateCostTypeBatch" parameterType="list">
        <foreach collection="List" item="cost" separator=";">
            UPDATE  cost_type
            SET type_name=#{cost.typeName},
            modify_id=#{cost.modifyId},
            modify_time=#{cost.modifyTime}
            WHERE id=#{cost.id}
        </foreach>
    </update>
UPDATE  cost_type SET type_name=?, modify_id=?, modify_time=? WHERE id=? ; 
UPDATE  cost_type SET type_name=?, modify_id=?, modify_time=? WHERE id=? ;
<!--deleteCostTypeByFather 根据id组修改一级状态-->
    <update id="deleteCostTypeById" parameterType="list">
        UPDATE  cost_type SET `status`=-1 WHERE id IN
        <foreach collection="List" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>
<!--deleteCostTypeByFather 根据id组修改一级状态-->
    <delete id="deleteCostTypeById" parameterType="list">
        delete cost_type WHERE id IN
        <foreach collection="List" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

 

plus:

XXService.updateBatchById()

 

sql 复杂查询,plus不方便做复杂的多表查询

<!--a List 二级为list-->
    <resultMap id="subTypeListResult" type="com.zking.zkingOAS.FDadmin.model.vo.CostSubTypeVO">
        <id column="sid" property="sid"/>
        <result column="stypeName" property="stypeName"/>
        <result column="sstatus" property="sstatus"/>
        <collection property="subTypes" ofType="com.zking.zkingOAS.FDadmin.model.entity.CostTypes">
            <id column="id" property="id"/>
            <result column="typeName" property="typeName"/>
        </collection>
    </resultMap>

<select id="selectCostSubTypeById" resultMap="subTypeListResult" parameterType="int">
        SELECT
        o.id AS sid,
        o.type_name AS stypeName,
        o.`status` AS sstatus,
        s.type_name AS typeName,
        s.id AS id
        FROM cost_type o
        LEFT JOIN  cost_type s on o.id=s.father_id
        WHERE o.father_id=0
        AND o.`status`!=-1
        AND o.id=#{id}
    </select>

 

新增 的循环

<foreach collection="List" item="cost" index="index" separator=",">
            (#{cost.typeName},#{cost.fatherId},#{cost.status},#{cost.createId},#{cost.createTime})
        </foreach>

collection="List"

List:是需要新增的集合,循环后,组装出成insert 的sql

 

删除的plus:

xxService.deleteBatchIds()

查询有以下方法:

xxService.selectBatchIds
xxService.selectPage()

 

增加、删除。修改等常用方法:

增加
Service.insert() 
           Service.insertAllColumn()


删除
           Service.delete(new EntityWrapper<>())
           Service.deleteById()


修改
            Service.update(new EntityWrapper<>())
           Service.updateById()
Service.updateAllColumnById()


查询

            Service.selectById()
            Service.selectList()
           Service.selectCount()
            Service.selectOne()

 

insert 简单的增加方法,参数是需要增加的对象

insert后,对象的主键id会自动获取  (即填充到放进insert里的对象) 可以直接使用 

deleteById根据主键id进行删除

 

selectList是根据条件查询出list

selectCount是根据条件统计

selectOne是根条件查询出一个对象,实际是调用selectList,然后取第一个对象

 

Obj obj=new Obj();

obj.setCode(1);

EntityWrapper<Obj> wrapper=new EntityWrapper(obj);

EntityWrapper可以先设置  set一个对象的基本属性 

 

EntityWrapper<> wrapper=new EntityWrapper();
            wrapper.eq("",0);
            wrapper.in("",new Integer[]{1,3,5});
EntityWrapper是条件构造器,eq是等于。in是范围查,第一个参数是数据库表的字段名,第一个参数是 值,即条件

updateById方法在插入时,会根据实体类的每个属性进行非空判断,只有非空的属性所对应的字段才会出现在拼接的SQL语句中。

updateAllColumnById方法在插入时,不管属性是否为空,属性所对应的字段都会出现在拼接的SQL语句中。