u012503756
bulidfer
2019-03-04 10:10
采纳率: 35.3%
浏览 1.3k
已采纳

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条回答 默认 最新

  • qq_43242099
    ThompJie 2019-03-04 10:49
    已采纳

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

    点赞 评论
  • qq_16307345
    Jiunn-Nan_Khan 2019-03-04 10:53

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

    点赞 评论
  • u011976331
    嘉禾嘉禾 2019-03-04 14:31

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

    点赞 评论
  • ayuan_23
    ayuan_23 2019-03-04 17:00
    点赞 评论
  • qq_18630487
    真-酸辣土豆丝 2019-03-04 18:01

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

    点赞 评论

相关推荐