java中Mybatis下Mysql、Oracle分页等写法

java中Mybatis下Mysql、Oracle分页等写法

    • -----Mysql分页写法
    • -----Oracle分页写法
    • -----Mysql返回主键ID
    • -----Oracle返回主键ID

-----Mysql分页写法

<select id="selectPage" resultMap="baseResult" parameterType="cn.gold.livesys.entity.customerEntity">

<bind name="page" value="(page-1) * pageSize">
select f_id,f_name,f_team as f_team_name from T_CUSTOMER
<include refid="selectConditionlike">
limit #{page},#{pageSize}
select>

-----Oracle分页写法

<select id="selectPage" resultMap="baseResult" parameterType="cn.gold.livesys.entity.customerEntity">
select * from (
	select rownum rn,t.* from (
		select f_id,f_name,f_team as f_team_name from T_CUSTOMER
	) t
) where rn > #{pageSize} * (#{page}-1) and rn < #{pageSize} * #{page} + 1
select>

知识点:resultMap中的column=“f_team_name” 对应的sql语句里的别名



<resultMap id="baseResult" type="cn.gold.livesys.entity.customerEntity">
	
	
	<id property="id" column="f_id"/>
	<result property="name" column="f_name"/>
	<result property="teamName" column="f_team_name"/>
resultMap>

-----Mysql返回主键ID

f_id:AI(auto increment 自增)
数据库主键ID配置

<insert useGeneratedKeys="true" parameterType="cn.gold.livesys.entity.customerEntity" keyProperty="id">
insert into...
<selectKey keyProperty="id" resultType="java.lang.Integer" order="AFTER">
	select LAST_INSERT_ID() as id
selectKey>
insert>

-----Oracle返回主键ID

java中Mybatis下Mysql、Oracle分页等写法_第1张图片

ORACLE PLSQL
数据库->Sequences->创建序列 Name=T_CUSTOMER_F_ID_SEQ

cn.gold.livesys.entity.customerEntity.java
	private Integer id;//
	
Mapper.xml
<insert id="addCustomer" parameterType="cn.gold.livesys.entity.customerEntity">
	<selectKey keProperty="id" resultType="java.lang.Integer" order="BEFORE">
		select T_CUSTOMER_F_ID_SEQ.nextval as id from dual		
	selectKey>
	insert into
		...
insert>

你可能感兴趣的:(Mybatis,java)