Mybatis(dao)实现举例

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.baizhi.ty.dao.CategoryDAO">
	
		<resultMap type="Category" id="CategoryMap">
			<id column="firstId"  property="id"/>
			<result column="firstname" property="categoryname" />
			<result column="category_id" property="category_id"/>
			<result column="goodscounts" property="goodscounts"/>
			<collection property="children" javaType="java.util.List" ofType="category">
				<id column="secondId" property="id"/>
				<result column="secondname" property="categoryname"/>
				<result column="category_id" property="category_id"/>
				<result column="goodscounts2" property="goodscounts"/>
			</collection>
		</resultMap>
	
		<!-- //查询出类别
		List<Category> selectAllCategory(); -->
		<select id="selectAllCategory" resultMap="CategoryMap">
			select d1.id as firstId ,d1.categoryname as firstname,
			d1.goodscounts,
			d2.id as secondId,d2.categoryname as secondname,
			d2.category_id,d2.goodscounts	
			from d_category d1
			left join d_category d2
			on d1.id = d2.category_id
			where d1.category_id is null
		</select>
		
		<!-- //查询一级类别
		Category selectOneCategory(@Param("oneId")Integer oneId); -->
		<select id="selectOneCategory" resultMap="CategoryMap">
			select d1.id as firstId ,d1.categoryname as firstname,
			d1.goodscounts,
			d2.id as secondId,d2.categoryname as secondname,
			d2.category_id,d2.goodscounts as goodscounts2
			from d_category d1
			left join d_category d2
			on d1.id = d2.category_id
			where d1.category_id is null
				<!-- 二级类别 -->
				<if test="oneId !=null ">
				and	d2.category_id = ${oneId}
				</if>
				<!-- 一级类别  -->
				<if test="oneId == null">
				 and d1.id  = ${twoId}
				</if>
		</select>
	
	<!-- //显示位置
	Category selectpositionCategory(@Param("oneId")Integer oneId,@Param("twoId")Integer twoId); -->
	<select id="selectpositionCategory" resultMap="CategoryMap">
			select d1.id as firstId ,d1.categoryname as firstname,
			d1.goodscounts,
			d2.id as secondId,d2.categoryname as secondname,
			d2.category_id,d2.goodscounts as goodscounts2
			from d_category d1
			left join d_category d2
			on d1.id = d2.category_id
			where d1.category_id is null
				<!-- 二级类别 -->
				<if test="oneId !=null ">
				and	d2.id = ${twoId}
				</if>
				<!-- 一级类别  -->
				<if test="oneId == null">
				 and d1.id  = ${twoId}
				</if>
		</select>
		
		<!--(1) 编辑推荐 -->
		<!-- //查询书的信息
			public Good selectById(String twoId); -->
			<select id="selectById" resultType="Good">
				select * from d_good where id=#{id}
			</select>
		<!-- //查询书的信息
			List<Good> selectGood(); -->
			<select id="selectGood" resultType="Good">
				select id,oneid,twoid,goodname,goodprice,dangprice,middleprice,grade,author,fromarea,fromtime,banci,
				printtime,isbn,zishu,yeshu,kaiben,paper,baozhuang,isup,imguri,sellnum
				from d_good
				where isup=1
			</select>
		<!-- 查询图书的总个数集合
			Integer selectAllBook();-->
			<select id="selectAllBook" resultType="Integer">
			select count(*)  from d_good 
		   	 where 
		       	 <if test="oneId==null">
		         	 oneId=${twoId}
	          	  </if>
		     	  <if test="oneId!=null">
		     		 twoId=${twoId}
		     	  </if>
			 </select>
		<!--分页查询图书 --> 
			 <select id="queryPageBook" resultType="Good">
		  		select * from (
		   			 select e.*,rownum rn from
		     			(select * from (
		       				  select  * from d_good
			    		     where 
			     		  <if test="oneId==null">
			         		 oneId=${twoId}
		          	 	  </if>
			     	  	  <if test="oneId!=null">
			     		 	twoId=${twoId}
			     	 	  </if>
		  		 ) order by ${order}) e
				) where rn between ${beginResult} and ${lastResult}
			</select>
	
		<!--(2)新书上架  -->
		<!-- //新销书
			List<Good> selectNewBook();-->
			<select id="selectNewBook" resultType="Good">
				select b.* from (
			select a.*,rownum rn	 from (
			select id,oneid,twoid,goodname,goodprice,dangprice,middleprice,grade,author,fromarea,fromtime,banci,
			printtime,isbn,zishu,yeshu,kaiben,paper,baozhuang,isup,imguri,sellnum	
			from d_good
			order by fromtime desc
			)a)b
			where rn between 1 and 8
			</select>
		
		
		<!--(3)热卖图书 -->
		<!-- //热销书
			List<Good> selectHotBook(); -->
			<select id="selectHotBook" resultType="Good">
				select b.* from (
			select a.*,rownum rn	 from (
			select id,oneid,twoid,goodname,goodprice,dangprice,middleprice,grade,author,fromarea,fromtime,banci,
			printtime,isbn,zishu,yeshu,kaiben,paper,baozhuang,isup,imguri,sellnum	
			from d_good
			order by sellnum desc
			)a)b
			where rn between 1 and 8
			</select>	
		
		<!--(4)新书热卖  -->
		<!-- //新热销书
			List<Good> selectHotAndNewBook();-->
			<select id="selectHotAndNewBook" resultType="Good">
					select b.* from (
				select a.*,rownum rn from (
				select * from d_good
				order by fromtime desc,sellnum desc
				)a)b
				where rn between 1 and 8
			</select>
		
		<!--添加部门sql-->
<insert id="insertDept">
<selectKey keyProperty="id" order="BEFORE" resultType="String">
select seq_dept.nextval from dual
<!-- select sys_guid() from dual -->
</selectKey>
insert into department1(id,name) values(#{id},#{name})
</insert> </mapper>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值