一、分析
-
每一个账户都有一个用户信息、账户信息的实体类,所以这两个实体类的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(account
、userinfo
、systemdictionary
、systemdictionaryitem
)<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,
- 创建用户的时候给版本初始化一个0,在进行修改的时候,需要看数据库的版本与实体类的版本是否一致(
- 字段
3、账户信息(Userinfo)
方法与用户信息一致。不再做解释。