一、主键自增如何获取主键
1.selectkey的使用
order="BEFORE" 在插入之前查询主键。
<insert id="insert" parameterType="xxPojo">
<selectKey resultType="Long" order="BEFORE" keyProperty="id">
SELECT SEQ_TB_ID.NEXTVAL FROM DUAL
</selectKey>
insert into table(ID, ...) values (#{id,jdbcType=DECIMAL}, ...)
</insert>
order="AFTER" 在插入之后查询主键。
<insert id="insert" parameterType="xxPojo">
<selectKey resultType="Long" order="AFTER" keyProperty="id">
SELECT SEQ_TB_ID.CURRVAL FROM DUAL
</selectKey>
insert into table(ID, ...) values (SEQ_TB_ID.NEXTVAL, ...)
</insert>
2.useGeneratedKeys的使用(类似:MySql)
<insert id="insert" parameterType="xxPojo" useGeneratedKeys="true" keyProperty="id">
insert into person(....) values(....)
</insert>
二、多对一,association的使用。
先看物理模型好理解:
user -> account ->org
一个用户可以有多个账号,一个账号可以在多个组织下使用。从账号的角度看:可能多个账号对应一个用户(多对一),
一个账号可能对应多个组织(一对多)。
public class AccountVo {
private Integer id;//账号ID
private String password;//账号密码
private Integer userId;//用户ID
private User user;//账号的对应的用户
private List<Org> orgs;//该账号可以在那些组织下使用
}
public class User {
private Integer id;//用户ID
private String name;//用户名
private String iphone;//用户联系方式
private String sex;//用户性别
}
public class Org {
private Integer id;//组织ID
private String name;//组织名
}
非触发式式查询:对查询结果进行解析,封装。
扫描二维码关注公众号,回复:
5002056 查看本文章
<resultMap type="com.it.web.vo.AccountVo" id="AccountVoMap">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="password" jdbcType="VARCHAR"
property="password" />
<result column="user_id" jdbcType="INTEGER" property="userId" />
<association property="user"
javaType="com.it.web.pojo.User">
<id column="uid" jdbcType="INTEGER" property="id" />
<result column="uname" jdbcType="VARCHAR" property="name" />
<result column="uiphone" jdbcType="VARCHAR" property="iphone" />
<result column="usex" jdbcType="VARCHAR" property="sex" />
</association>
</resultMap>
<select id="test1" resultMap="AccountVoMap">
select a.*,b.id uid,b.name uname,b.iphone uiphone,b.sex usex from account a left join
user b on a.user_id = b.id where a.id = #{id}
</select>
触发式式查询:
<resultMap type="com.it.web.vo.AccountVo" id="AccountVoMap2">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="user_id" jdbcType="INTEGER" property="userId" />
<association property="user" column="user_id"
javaType="com.it.web.pojo.User" select="getUser">
</association>
</resultMap>
<select id="test2" resultMap="AccountVoMap2">
select * from account where id = #{id}
</select>
<select id="getUser" resultType="com.it.web.pojo.User" parameterType="INTEGER">
select * from user where id = #{user_id}
</select>
三、一对多,collection的使用
非触发式式查询:
<resultMap type="com.it.web.vo.AccountVo" id="AccountVoMap">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="password" jdbcType="VARCHAR"
property="password" />
<result column="user_id" jdbcType="INTEGER" property="userId" />
<collection property="orgs" ofType="com.it.web.pojo.Org">
<id column="oid" jdbcType="INTEGER" property="id" />
<result column="oname" jdbcType="VARCHAR" property="name" />
</collection>
</resultMap>
<select id="test" resultMap="AccountVoMap">
select a.*,d.id oid ,d.name oname from account a left join account_org c on a.id =
c.account_id left join org d on d.id = c.org_id where a.id = #{id}
</select>
触发式式查询:
<resultMap type="com.it.web.vo.AccountVo" id="AccountVoMap22222">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="user_id" jdbcType="INTEGER" property="userId" />
<collection property="orgs" ofType="com.it.web.pojo.Org" select="getOrgs" column="id">
</collection>
</resultMap>
<select id="test2" resultMap="AccountVoMap2">
select * from account where id = #{id}
</select>
<select id="getOrgs" resultType="com.it.web.pojo.Org" parameterType="INTEGER">
select * from Org where id in (select org_id from account_org where account_id = #{id})
</select>