mybits操作指南

第一、一对一:

<resultMap type="com.zktx.platform.entity.tb.Module" id="BaseResultMap">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="class_name" jdbcType="VARCHAR" property="class_name"/>
        <result column="description" jdbcType="VARCHAR" property="description"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="priority" jdbcType="INTEGER" property="priority"/>
        <result column="sn" jdbcType="VARCHAR" property="sn"/>
        <result column="url" jdbcType="VARCHAR" property="url"/>
        <result column="parent_id" jdbcType="INTEGER" property="parent_id"/>
        <association column="parent_id" property="parent_Module" select="selectById"></association>
    </resultMap>
    
    <select id="queryList" resultMap="BaseResultMap">
        select * from tb_module m1 left join tb_module m2 on m1.parent_id =m2.id 
    </select>
    <select id="selectById"  parameterType="java.lang.Integer" resultType="com.zktx.platform.entity.tb.Module">
        select * from tb_module where id=#{parent_id}
    </select>

对应java的dao层代码:

List<Module> queryList();

第二、批量删除

<delete id="deleteByIds" parameterType="java.util.List">
        delete from tb_module where id in
        <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
            #{item,jdbcType=INTEGER}
        </foreach>
    </delete>

对应的java的dao层代码:

void deleteByIds(List<Integer> ids);

 第三、批量增加

<insert id="insertBatch" parameterType="java.util.List">
        insert into tb_role_permission (permission_id,role_id) VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.permission_id,jdbcType=INTEGER},#{item.role_id,jdbcType=INTEGER})
        </foreach>
    </insert>

对应的java代码:

void insertBatch(List<RolePermission> rolePermissions);

第四、插入数据,返回id

<insert id="insertSelective" parameterType="com.zktx.platform.entity.tb.Module" >
        <selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">
            SELECT LAST_INSERT_ID() AS id
        </selectKey>
        insert into tb_module
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="class_name!=null">
                class_name,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="class_name!=null">
                #{class_name,jdbcType=VARCHAR},
            </if>
    </trim>
    </insert>

java代码(返回的id在参数module中):

public void insertSelective(Module module) {
        int id = moduleMapper.insertSelective(module);
        if (id > 0) {
            List<Permission> permissions = module.getPermissions();
            if (null != permissions && permissions.size() > 0) {
                for (Permission permission : permissions) {
                    permission.setModule_id(module.getId());
                }
                permissionMapper.insertBatch(permissions);
            }
        }
    }

 第五、批量插入返回数据:

批量时,传入list,获取时类同单个,mybatis自动把自增的id装入list中的对象的id,mapper.xml写法如:

<insert id="insertBatch" parameterType="java.util.List"  useGeneratedKeys="true" keyProperty="id">
        insert into tb_permission (name,sn,description,module_id) values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.name},#{item.sn},#{item.description},#{item.module_id})
        </foreach>
    </insert>

java代码:

// 添加对应权限
                    permissionMapper.insertBatch(permissions);

                    // 为菜单权限增加角色
                    List<RolePermission> rolePermissions = new ArrayList<RolePermission>();
                    for (Permission permission : permissions) {
                        String[] roleidstr = roleids.split(",");
                        for (int i = 0; i < roleidstr.length; i++) {
                            RolePermission rolePermission = new RolePermission();
                            rolePermission.setPermission_id(permission.getId());
                            rolePermission.setRole_id(Integer.parseInt(roleidstr[i]));
                            rolePermissions.add(rolePermission);
                        }

                    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值