myBatis(三) xml文件的写法,返回主键,快速生成类jar

一:一对多关联
property是属性名,column是表名
        <resultMap type="User" id="userResult">
<result column="user_id" property="id"/>         //这里是属性和列名不一致的情况下需要
<result column="user_name" property="name"/>
<result column="user_birthday" property="birthday"/>
<result column="user_salary" property="salary"/>
<association property="inter" javaType="Inter">       //一对多,Inter是一个对象,是user外键关联表
                                                                                                          User对象里面的属性private Inter inter
<id property="id" column="id"/>            //id是主键
<result property="name" column="name"/>
</association>
</resultMap>


     <select id="findById"   resultMap="userResult"  parameterType="Map">
select *
from s_user u,inter i
where i.user_id=u.user_id and  u.user_id = #{id}
<if test="flag==x">
and u.user_name="tom"
</if>
</select>

更新后写法:
因为再不用自动生成实体,所以属性和列名是一样的,能简化不少
实体类
     private Seller seller;  
     <resultMap id="xxx" type="实体类" autoMapping="true" extends="map">
        <association property="seller" resultMap="map" />
    </resultMap>
    <resultMap id="map" type="Seller" autoMapping="true">
        <id property="id" column="seller_id" />
    </resultMap>

如果是private List<Seller> list
则这里需要的是  collection,一对多
association  一对一


因为程式比较固定,这里记录注意的几点和几个实用技术
一、返回新创建的主键
<insert id="xx" parameterType="xx" useGeneratedKeys="truekeyProperty="id"> 主键一般是id
…… 
二、快速生成各个类
mybatis-generator-core-1.3.2
看自己的d盘的文件夹,里面有文字描述,很简单实用。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值