您的位置:首页 > 其它

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

2016-04-04 11:01 281 查看
一:一对多关联
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="true" keyProperty="id"> 主键一般是id
……

二、快速生成各个类
mybatis-generator-core-1.3.2

看自己的d盘的文件夹,里面有文字描述,很简单实用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: