SSM项目——个人中心(初)

一、分析

  • 每一个账户都有一个用户信息、账户信息的实体类,所以这两个实体类的id必须和账户的id一致。

    • 在注册账户的时候给用户信息和账户信息的id进行复制即可(账户id : 自增)。
    • 用户信息和账户信息有id之后执行各自业务层的添加信息操作。
    // 初始化账号信息
    Account account = new Account();
    account.setId(logininfo.getId());
    this.accountService.add(account);
    // 初始化用户信息
    Userinfo userinfo = new Userinfo();
    userinfo.setId(logininfo.getId());
    this.userinfoService.add(userinfo);
    

二、实现步骤

1、逆向工程

  • 利用逆向工程(generatorConfig.xml类)自动生成用户信息类和账户信息类与字典和字典项的Mapper(accountuserinfosystemdictionarysystemdictionaryitem

    <table tableName="account" domainObjectName="Account">
       <!-- 参考 javaModelGenerator 的 constructorBased属性 -->
       <property name="constructorBased" value="false" />
       <generatedKey column="id" sqlStatement="JDBC" />
    </table>
    <table tableName="userinfo" domainObjectName="Userinfo">
       <!-- 参考 javaModelGenerator 的 constructorBased属性 -->
       <property name="constructorBased" value="false" />
       <generatedKey column="id" sqlStatement="JDBC" />
    </table>
    <table tableName="systemdictionary" domainObjectName="SystemDictionary">
       <!-- 参考 javaModelGenerator 的 constructorBased属性 -->
       <property name="constructorBased" value="false" />
       <generatedKey column="id" sqlStatement="JDBC" />
    </table>
    <table tableName="systemdictionaryitem" domainObjectName="SystemDictionaryItem">
       <!-- 参考 javaModelGenerator 的 constructorBased属性 -->
       <property name="constructorBased" value="false" />
       <generatedKey column="id" sqlStatement="JDBC" />
    </table>
    

**注意:通过逆向工程生成的Mapper和XML文件存在大小写不匹配和错误代码,需要检查!**3

2、用户信息(Account)

  • 查看并修改resultMap的大小写

    <resultMap id="BaseResultMap" type="com.xmg.p2p.base.domain.Account">
      <id column="id" jdbcType="BIGINT" property="id" />
      <result column="tradePassword" jdbcType="VARCHAR" property="tradePassword" />
      <result column="usableAmount" jdbcType="DECIMAL" property="usableAmount" />
      <result column="freezedAmount" jdbcType="DECIMAL" property="freezedAmount" />
      <result column="borrowLimit" jdbcType="DECIMAL" property="borrowLimit" />
      <result column="version" jdbcType="INTEGER" property="version" />
      <result column="unReceiveInterest" jdbcType="DECIMAL" property="unReceiveInterest" />
      <result column="unReceivePrincipal" jdbcType="DECIMAL" property="unReceivePrincipal" />
      <result column="unReturnAmount" jdbcType="DECIMAL" property="unReturnAmount" />
      <result column="remainBorrowLimit" jdbcType="DECIMAL" property="remainBorrowLimit" />
      <result column="verify" jdbcType="VARCHAR" property="verify" />
    </resultMap>
    
  • 查看并修改用户信息的insert方法

    • 自动生成的id是自增,删除自增即可!
    • id的获取方法:
      • 在注册账户的业务层中,成功注册账户后,账户就自动生成了一个id(账户的ID是自增)
      • 然后把账户的ID赋值给用户信息和账户信息的实体类中(setId()
      • 总结:只要成功创建好账户之后账户信息和用户信息的ID也就躺在实体类中了。
      • 实体类中有了id,#{id}也就能获取到账户的id了,这样就实现了三个表用一个id。
    <insert id="insert" keyColumn="id" keyProperty="id">
      insert into account (id,tradePassword, usableAmount, freezedAmount,
        borrowLimit, version, unReceiveInterest, 
        unReceivePrincipal, unReturnAmount, remainBorrowLimit, 
        verify)
      values (#{id}, #{tradePassword,jdbcType=VARCHAR}, #{usableAmount,jdbcType=DECIMAL}, #{freezedAmount,jdbcType=DECIMAL},
        #{borrowLimit,jdbcType=DECIMAL}, 0, #{unReceiveInterest,jdbcType=DECIMAL},
        #{unReceivePrincipal,jdbcType=DECIMAL}, #{unReturnAmount,jdbcType=DECIMAL}, #{remainBorrowLimit,jdbcType=DECIMAL},
        #{verify,jdbcType=VARCHAR})
    </insert>
    
    • 自动生成的#{xx}名和实体类中的字段名大小写不一样,需要检查并改正。
  • 修改用户信息(乐观锁

    • 字段 version为版本信息,是乐观锁控制的重要信息。
    • 在创建用户信息的时候给版本字段默认为0,
    • 在修改的时候让此字段+1 version = version+1
    • 添加修改条件:and version = #{version}
    • 说明:
      • 创建用户的时候给版本初始化一个0,在进行修改的时候,需要看数据库的版本与实体类的版本是否一致(version = #{version}), 一致就进行修改操作。
      • 在进行修改的时候让版本号进行+1 version = version+1,

3、账户信息(Userinfo)

方法与用户信息一致。不再做解释。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值