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盘的文件夹,里面有文字描述,很简单实用。
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盘的文件夹,里面有文字描述,很简单实用。
相关文章推荐
- struts2笔记03-ActionContext
- CSS布局中的问题
- Hacker-1
- leetcode---Word Pattern
- 让自己的软件出现在选择打开列表的软件中
- Android 开发艺术与探究 第一章 Activity 的生命周期和启动模式
- 动态数组
- DOS下操作数据库基本语法(简明扼要)
- myBatis(二)方法、控制、接口使用
- swing布局管理器简介
- 数据库中的 Date 函数研究
- 215. Kth Largest Element in an Array
- 记毕设中遇到的菜鸡问题----3
- 【转】理解Java Integer的缓存策略
- POJ2479&&POJ2593 Maximum sum&&Max Sequence(最大连续和)
- 腾讯机试题1:打印蛇形矩阵
- myBatis配置(一)
- java网络编程
- Ten sort algorithms in interviews
- [线段树][CODEVS 1080]题解