设为首页 收藏本站
查看: 998|回复: 0

[经验分享] mybatis-杂记

[复制链接]

尚未签到

发表于 2016-11-24 06:09:26 | 显示全部楼层 |阅读模式
   1、mybatis在xml文件中处理大于号小于号的方法
           第一种方法:
                用了转义字符把>和<替换掉,然后就没有问题了。
             SELECT * FROM test WHERE 1 = 1 AND start_date  &lt;= CURRENT_DATE AND end_date &gt;= CURRENT_DATE
      附:XML转义字符


&lt;





小于号



&gt;






大于号



&amp;



&






&apos;



'



单引号



&quot;



"



双引号






  第二种方法: 
             因为这个是xml格式的,所以不允许出现类似“>”这样的字符,但是都可以使用<![CDATA[ ]]>符号进行说明,将此类符号不进行解析 你的可以写成这个,mapper文件示例代码:
   

<![CDATA[ when min(starttime)<='12:00' and max(endtime)<='12:00' ]]>
  2、SQL语句中“resultMap”和"parameterMap"的设置
       在配置文件先申明resultMap和parameterMap

<resultMap id="BaseResultMap" type="com.etwin.order.model.UserNew" >
<id column="ID" property="id" jdbcType="INTEGER" />
<result column="USER_ID" property="userId" jdbcType="VARCHAR" />
<result column="Name" property="name" jdbcType="VARCHAR" />
<result column="PassWord" property="password" jdbcType="VARCHAR" />
<result column="Sex" property="sex" jdbcType="INTEGER" />
<result column="Phone" property="phone" jdbcType="VARCHAR" />
<result column="IDCard" property="idcard" jdbcType="VARCHAR" />
<result column="Email" property="email" jdbcType="VARCHAR" />
<result column="Add_Person" property="addPerson" jdbcType="VARCHAR" />
<result column="Add_Time" property="addTime" jdbcType="TIMESTAMP" />
<result column="Role_ID" property="roleId" jdbcType="VARCHAR" />
<result column="status" property="status" jdbcType="INTEGER" />
<result column="loginTime" property="logintime" jdbcType="TIMESTAMP" />
<result column="Cash_Password" property="cashPassword" jdbcType="VARCHAR" />
<result column="Agent_ID" property="agentId" jdbcType="INTEGER" />
<result column="Is_Enable" property="isEnable" jdbcType="VARCHAR" />
<result column="Agent_Type" property="agentType" jdbcType="VARCHAR" />
</resultMap>

   说明:
      resultMap:返回结果集
      parameterMap:查询参数集
        BaseResultMap:可以为返回结果集也可以为查询参数集,jdbcType可写可不写。
      
        SQL查询例子

<select id="selectUserOldJiLian" resultType="com.etwin.order.model.UserOld" parameterType="com.etwin.order.model.UserOld">
select distinct u.User_ID userId, u.Agent_ID agentId, u.User_Name userName, u.Password Password, u.User_Type userType, u.Create_Date createDate,
u.Create_By createBy,u.Modify_Date modifyDate, u.Modify_Pass_Date modifyPassDate, u.Modify_PayPass_Date modifyPaypassDate, u.Modify_By modifyBy,
u.Active_Flag activeFlag,u.Employee_Name employeeName, u.Email email, u.Tel tel, u.Mobile_Tel mobileTel, u.Stop_CMD stopCmd, u.Is_Admin isAdmin,
u.Pay_Password payPassword, u.Parent_Agent_Id parentAgentId, u.Question question, u.Answer answer, u.User_Security userSecurity, r.Role_ID RoleId
from  om_user u,om_userrole ur,om_role r
<where>
1=1 and u.user_id = ur.User_id and ur.Role_Code = r.Role_Code
<if test="agentId != null">
AND Agent_ID = #{agentId}
</if>
</where>
</select>

   说明:
        resultType="com.etwin.order.model.UserOld",返回结果为对象,可以换成上面配置好的resultMap="BaseResultMap"
        parameterType="com.etwin.order.model.UserOld",查询差数为对象,可以换成上面配置好的parameterMap="BaseResultMap"
  3、mybatis批量操作
  xml文件配置      

<!-- 批量插入 -->
<insert id="insertRoleNewList" parameterType="java.util.List">
insert into sys_role(ID,Role_ID,Name)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.id}, #{item.roleId},#{item.name})
</foreach>
</insert>

 注释:list为道中传入的参数(集合)


    dao调用
int insertRoleNewList(List list);
 注释:list为向xml文件中传递的参数



4、迭代写法    


     xml文件写法

<select id="findUserIdRole" resultMap="roleResultMap" parameterType="java.util.Map">
select * from t_role
<trim prefix="where" prefixOverrides="and" >
<if test="userRoleIdList!=null">
and id in
<foreach item="item" index="index" collection="userRoleIdList" open="(" separator="," close=")">  
#{item}  
</foreach>   
</if>
</trim>     
</select>



  注释:collection="userRoleIdList"中userRoleIdList为集合

    调用类写法
package com.gamexun.support.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.gamexun.support.model.Role;
import com.gamexun.support.service.RoleService;
public class Test {
public static void main(String[] args) {         
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("gx-support-common-context-test.xml");
RoleService roleService = (RoleService)context.getBean("roleService");     
try {
Map<String,Object> params = new HashMap<String,Object>();
List userRoleIdList = new ArrayList();
userRoleIdList.add(1);
userRoleIdList.add(2);
userRoleIdList.add(3);
params.put("userRoleIdList",userRoleIdList);
List<Role> list = roleService.findUserIdRole(params);
System.out.println("---------------------------------------------");
System.out.println(list.size());
} catch (Exception e) {
e.printStackTrace();
}      
}
}


5、在mybatis中使用模糊查询

<!-- 查询用户 -->
<select id="getUser" resultMap="usertMap" parameterType="java.util.Map">
select * from user
<where>
1=1
<if test="account!=null"><![CDATA[ and account like '${account}%' ]]></if>
<if test="name!=null"><![CDATA[ and name like '${name}%' ]]></if>
<if test="tel!=null"><![CDATA[ and phone like '${tel}%' ]]></if>
<if test="beginNum!=null and count!=null">
order by id limit #{beginNum},#{count}
</if>
</where>
</select>
 说明:like后面必须用$;

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-304554-1-1.html 上篇帖子: spring+mybatis 下篇帖子: Mybatis 学习笔记
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表