求SpringMvc+mybatis的通用dao层编写
有项目更好,最近在学,想借鉴想看看怎么写最好最简单,还有我想问问mapper和dao各自的作用,看到很多代码里mapper和dao里的内容都差不多,都是一些方法,到底为什...
有项目更好,最近在学,想借鉴
想看看怎么写最好最简单,还有我想问问mapper和dao各自的作用,看到很多代码里mapper和dao里的内容都差不多,都是一些方法,到底为什么要这样做 展开
想看看怎么写最好最简单,还有我想问问mapper和dao各自的作用,看到很多代码里mapper和dao里的内容都差不多,都是一些方法,到底为什么要这样做 展开
展开全部
mybatis是没有通用dao层的,都要一个方法写一个sql语句例如:
//先写一个dao接口
public interface CustomerDao
{
Map<String, Object> getUserByAccounts(String accounts);
Map<String, Object> getUserDetailById(int id);
void lossAccountsByUserId(String userId);
PageList<Map<String, Object>> getCanReversedList(Map<String, String> queMap);
PageList<Map<String, Object>> searchAccountsAnomaly(Map<String, String> param);
Map<String, Object> isHasAccounts(String accounts);
void accountsRecover(Map<String, Object> updateInfo);
}
//在写一个dao的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.apdplat.kc.customer.dao.CustomerDao">
<select id="getUserByAccounts" parameterType="string" resultType="map">
select * from apdp_user a where UPPER(a.username)=UPPER(#{accounts})
</select>
<select id="getUserDetailById" parameterType="int" resultType="map">
select u.id,u.id_card,u.bind_phone from kc_user_detail u where u.id=#{id}
</select>
<select id="getCanReversedList" parameterType="map" resultType="map">
select k.*,k2.app_time as CZ_TIME,k2.DISCOUNT_FEE as REVERSED_FEE from kc_phone_charge_app k left join
(select * from kc_phone_charge_app a where a.type_id=2) k2 on
k.reversed_app_id=k2.id
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(userCode)">
left join apdp_user u on k.app_user_id=u.id
</if>
where k.app_time > (SYSDATE -3) and k.TYPE_ID=1
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(CHARGE_PHONE_NO)">
and k.CHARGE_PHONE_NO like #{CHARGE_PHONE_NO}||'%'
</if>
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(SEQ_CODE)">
and k.SEQ_CODE like #{SEQ_CODE}||'%'
</if>
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(rechargeDate1)">
<![CDATA[
and to_char(k.APP_TIME,'yyyy-MM-dd') >= #{rechargeDate1}
]]>
</if>
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(rechargeDate2)">
<![CDATA[
and to_char(k.APP_TIME,'yyyy-MM-dd') <= #{rechargeDate2}
]]>
</if>
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(acceptDate1)">
<![CDATA[
and to_char(k2.APP_TIME,'yyyy-MM-dd') >= #{acceptDate1}
]]>
</if>
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(acceptDate2)">
<![CDATA[
and to_char(k2.APP_TIME,'yyyy-MM-dd') <= #{acceptDate2}
]]>
</if>
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(userCode)">
and upper(u.USERNAME) like upper(#{userCode}||'%')
</if>
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(userName)">
and k.APP_USER_NAME like '%'||#{userName}||'%'
</if>
order by k.app_time DESC
</select>
<select id="searchAccountsAnomaly" parameterType="map" resultType="map">
select a.*,u.realname unlockUser from KC_ACCOUNT_ANOMALY a left join
apdp_user u on a.unlock_user=u.id where
upper(a.user_accounts)=upper(#{accounts}) order by a.anomaly_time DESC
</select>
<select id="isHasAccounts" parameterType="string" resultType="map">
select * from apdp_user u where upper(u.username)=upper(#{accounts})
</select>
<update id="lossAccountsByUserId" parameterType="string">
update apdp_user a set a.accountloss='1' where a.id=#{userId}
</update>
<update id="accountsRecover" parameterType="map">
update kc_account_anomaly a set a.unlock_time=SYSDATE,a.unlock_user=#{userId},a.status=0 where a.id=#{id}
</update>
</mapper>
建议你去百度文库直接搜mybatis的教程资料,有更加详细的解释和例子
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询