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

-----Mysql分页写法

<select id="selectPage" resultMap="baseResult" parameterType="cn.gold.livesys.entity.customerEntity">
<!-- Mysql从0开始计数 -->
<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 &gt; #{pageSize} * (#{page}-1) and rn &lt; #{pageSize} * #{page} + 1
</select>

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

<!-- page:当前页码 -->
<!-- pageSize: 每页显示数量 -->
<resultMap id="baseResult" type="cn.gold.livesys.entity.customerEntity">
	<!-- property: 需要映射到JavaBean 的属性名称(customerEntity中的属性名)-->
	<!-- column: 数据表的列名或者标签别名。 -->
	<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

创建序列

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

cn.gold.livesys.entity.customerEntity.java
	private Integer id;//<!-- 同定义的keProperty值 -->
	
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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值