mybatis 如何用update修改关联表Service业务层怎么实现

/**软件环境**/

框架:spring springMVC mybatis
数据库:mysql

/**报错信息**/

图片说明

/**代码提示**/

//数据层mybatis xml文件

<?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.app.dao.mapper.MemberPermissionMapper">
    <resultMap type="MemberPermission" id="MemberPermission">
        <id column="id" property="id"/>
        <result column="member_id" property="member_id"/>
        <result column="ttys" property="ttys"/>
        <result column="key" property="key"/>
        <result column="lose_time" property="lose_time"/>
        <result column="note" property="note"/>
        <result column="update_time" property="update_time"/>
        <!-- 关联member表 -->
        <association property="member" javaType="com.app.bean.Member">
            <id column="member_id" property="id"/>
            <result column="phone" property="phone"/>
            <result column="name" property="name"/>
            <result column="password" property="password"/>
        </association>
    </resultMap>
<update id="updateMemberLoginPermission" parameterType="map">
        UPDATE dt_member_permission AS t1
        LEFT JOIN dt_member AS t2 ON t1.member_id=t2.id
        <set>
            <if test="key!=null and key !=''">t1.key=#{key},
            </if>
        </set>
        WHERE t1.ttys=#{key}
        AND t2.phone=#{phone}
        AND t2.password=#{password}
    </update>

//service业务层接口实现类提示 phone和newpwd字段是在关联表member里面

    @Override
    public int updateMemberLoginPermission( String ttys, String phone, String newpwd, String key_dm) {

       MemberPermission memberPermission= new MemberPermission();
        memberPermission.setTtys(ttys);
        memberPermission.setKey_dm(key_dm);

        Member member = new Member();
        member.setPhone(phone);
        member.setPassword(newpwd);
        return memberPermissionDao.updateMemberLoginPermission(memberPermission,member);
    }

//javabean实体类提示

package com.app.bean;

import java.util.Date;

/**
 * 用户权限组合表
 * pjc
 * 2019.2.25
 */
public class MemberPermission {
    private int id;
    private int member_id;
    private String ttys;//终端类型
    private String key;
    private Date lose_time;
    private String note;
    private Date update_time;

    public MemberPermission() {
    }

    public MemberPermission(int id, int member_id, String ttys, String key, Date lose_time, String note, Date update_time) {
        this.id = id;
        this.member_id = member_id;
        this.ttys = ttys;
        this.key = key;
        this.lose_time = lose_time;
        this.note = note;
        this.update_time = update_time;
    }

    public int getId() {
        return id;
    }
        /**后面省略**

//关联表Javabean提示

public class Member {
    private int id;
    private String name;
    private String password;
    private int gender;
    private Date birthday;
    private String phone;
    private String email;
    private Date regtime;
    private Date losetime;
    private Date losetime_jl;
    private Date losetime_tz;
    private Date losetime_bj;
    private Date losetime_sz;
    private String nickname;
    private String head;
    private int status;
    private int type;
    private String note;
    private String departments;
    private String hospital;
    private String province;
    private String city;
    private String country;
    private String key;//用户授权码实现单点登录
    private String key_jl;//实现经络app单点登录
    private String key_tz;//实现体质app单点登录
    private String key_bj;//实现保健机构用户单点登录
    private String key_sz;//实现手诊用户单点登录
    private Date record_time;
    private String advice;//医师建议

//mapper.java文件提示

图片说明

/**尝试过的办法1: key字段已经统一改为key_dm和数据库实体类全部统一 去掉@param注解 修改mybatis.xml**/

//dao层接口代码提示 mapper.java文件


public interface MemberPermissionMapper {

    public int updateMemberLoginPermission(MemberPermission memberPermission,Member member);//根据终端类型 用户手机号密码修改动态key 关联表
}

//service层接口提示

package com.app.service;

import java.util.Date;
import java.util.List;
import java.util.Map;


import com.app.bean.Member;


public interface MemberService {
    public int updateMemberLoginPermission(String ttys,String phone, String newpwd, String key_dm);

}

//service层接口实现类提示

  @Override
    public int updateMemberLoginPermission( String ttys, String phone, String newpwd, String key_dm) {

       MemberPermission memberPermission= new MemberPermission();
        memberPermission.setTtys(ttys);
        memberPermission.setKey_dm(key_dm);

        Member member = new Member();
        member.setPhone(phone);
        member.setPassword(newpwd);
        return memberPermissionDao.updateMemberLoginPermission(memberPermission,member);
    }

//mybatis.xml

  <?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.app.dao.mapper.MemberPermissionMapper">
    <resultMap type="com.app.bean.MemberPermission" id="MemberPermission">
        <id column="id" property="id"/>
        <result column="member_id" property="member_id"/>
        <result column="ttys" property="ttys"/>
        <result column="key_dm" property="key_dm"/>
        <result column="lose_time" property="lose_time"/>
        <result column="note" property="note"/>
        <result column="update_time" property="update_time"/>
        <!-- 关联member表 -->
        <association property="member" javaType="com.app.bean.Member">
            <id column="member_id" property="id"/>
            <result column="phone" property="phone"/>
            <result column="name" property="name"/>
            <result column="password" property="password"/>
        </association>
    </resultMap>
    <update id="updateMemberLoginPermission" parameterType="com.app.bean.Member">
        UPDATE dt_member_permission AS t1
        LEFT JOIN dt_member AS t2 ON t1.member_id=t2.id
        <set>
            <if test="key_dm!=null and key_dm !=''">t1.key_dm=#{key_dm},
            </if>
        </set>
        WHERE t1.ttys=#{ttys}
        AND t2.phone=#{phone}
        AND t2.password=#{password}
    </update>

//报错提示
图片说明

/**尝试方法2 已解决: 关联对象传参问题**/

别用实体类传参,使用map。
Map map = new HashMap();
map.put("ttys",ttys);
map.put("phone",phone);
map.put("key_dm",key_dm);
map.put("password",password);
xml中的update语句修改成:

UPDATE dt_member_permission AS t1
LEFT JOIN dt_member AS t2 ON t1.member_id=t2.id

t1.key_dm=#{key_dm}

WHERE t1.ttys=#{ttys}
AND t2.phone=#{phone}
AND t2.password=#{password}

5个回答

你的mybatis xml中parameterType的值是'map',但是你service层传的是一个你直接写的对象,肯定是没有getPhone方法啊

u012503756
bulidfer service接口实现类 改用map传参皆可 真是糊涂了关联查询方法一直都是map传参的 update也是如此
11 个月之前 回复
u012503756
bulidfer @Override public int updateMemberLoginPermission( String ttys, String phone, String newpwd, String key_dm) { // MemberPermission memberPermission= new MemberPermission(); // memberPermission.setTtys(ttys); // memberPermission.setKey_dm(key_dm); // // Member member = new Member(); // member.setPhone(phone); // member.setPassword(newpwd); Map map = new HashMap(); map.put("ttys",ttys); map.put("phone",phone); map.put("key_dm",key_dm); map.put("password",newpwd); return memberPermissionDao.updateMemberLoginPermission(map); }
11 个月之前 回复
qq_43242099
ThompJie 回复bulidfer: parameterType='包名.类名',写全了就好了或者用Map也可以啊,但是光看你的service,你都没有put进去,肯定是不行的
11 个月之前 回复
u012503756
bulidfer 谢谢 parameterType 改成memberPemmisson实体不行 才换成map的
11 个月之前 回复

如果非要关联修改,个人建议使用触发器或存储过程

你的mapper 的java文件呢?在mapper的java文件中用@Param("phone") 修饰一下参数就可以了

听我的!!mapper方法不用变去掉@Param("phone")注解。直接传对象不用解释。
xml修改如下
图片说明

u012503756
bulidfer 不行啊 Parameter 'key' not found. Available parameters are [0, 1, param1, param2]
11 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问