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);
                        }

                    }

 

posted on 2017-04-28 19:25  世界之大追梦者  阅读(613)  评论(0编辑  收藏  举报

导航