mybatis四(动态sql)

<1>
<select id="selectUserByConditions" parameterType="user" resultType="user"> SELECT * FROM USER WHERE 1=1 <if test="id != null and id!=''"> and id =#{id} if> <if test="id != null and id!=''"> id =#{id} if> <if test="email != null and email !=''"> and email =#{email} if> <if test="lastName != null and lastName !=''"> and last_name =#{lastName} if> <if test="gender == 1 or gender == 0"> and gender =#{gender} if> select>

<2>

<select id="selectUserByConditions" parameterType="user" resultType="user">
        SELECT * FROM USER
        
        <if test="gender == 1 or gender == 0">
             gender =#{gender}
        if>

 


    --> <where> <if test="id != null and id!=''"> id =#{id} if> <if test="email != null and email !=''"> and email =#{email} if> <if test="lastName != null and lastName !=''"> and last_name =#{lastName} if> <if test="gender == 1 or gender == 0"> and gender =#{gender} if> where> select>

<3>前后缀解决

后缀解决
<select id="selectUserByConditions" parameterType="user" resultType="user"> SELECT * FROM USER "where" suffixOverrides="and"> <if test="id != null and id!=''"> id =#{id} and if> <if test="email != null and email !=''"> email =#{email} and if> <if test="lastName != null and lastName !=''"> last_name =#{lastName} and if> <if test="gender == 1 or gender == 0"> gender =#{gender} if> select>

 

  前缀解决
<select id="selectUserByCons" parameterType="user" resultType="user"> SELECT * FROM USER "where" prefixOverrides="and"> <if test="id != null and id!=''"> and id =#{id} if> <if test="email != null and email !=''"> and email =#{email} if> <if test="lastName != null and lastName !=''"> and last_name =#{lastName} if> <if test="gender == 1 or gender == 0"> and gender =#{gender} if> select>

<4>

<select id="selectUserByCons" parameterType="user"
        resultType="user">
        SELECT * FROM USER
        <where>
            
            <choose>
            <when test="id != null and id!=''">
                id =#{id}
            
            "email != null and email !=''">
                and email =#{email}
            
            "lastName != null and lastName !=''">
                and last_name =#{lastName}
            
                <otherwise>
                    1=1
                
            
        where>
    select>

<5>更新

"updateUserById" parameterType="model.User">
        UPDATE user 
        <set>
            <if test="email != null and email !=''">
                 email =#{email},
            if>
            <if test="lastName != null and lastName !=''">
                 last_name =#{lastName},
            if> 
            
            <if test="gender == 1 or gender == 0">
                 gender =#{gender}
            if> 
        set>
        WHERE id=#{id}
    

<6>批量查询

<select id="selectUserByIds" resultType="user">
        SELECT * FROM USER where id 
        in
        
       public List selectUserByIds(@Param("ids")List ids);对应collection="ids" <foreach collection="list" item="item_id" open="(" separator="," close=")"> #{item_id} foreach> select>

<7>批量保存

mysql下

 //推荐使用
     "addEmpsBatch">
      INSERT INTO emp(ename,gender,email,did)
      VALUES
      <foreach collection="emps" item="emp" separator=",">
      (#{emp.eName},#{emp.gender},#{emp.email},#{emp.dept.id})
      foreach>
     
  //在jdbc.url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true
    

  批量batch保存mybatis ExecutorType.BATCH

Mybatis内置的ExecutorType有3种,默认为simple,该模式下它为每个语句的执行创建一个新的预处理语句,单条提交sql;而batch模式重复使用已经预处理的语句,并且批量执行所有更新语句,

显然batch性能将更优; 但batch模式也有自己的问题,比如在Insert操作时,在事务没有提交之前,是没有办法获取到自增的id,这在某型情形下是不符合业务要求的

Oracle下批量保存   

  方法1,将多个insert放在begin和end之间执行 
  begin
  insert into user(userno,username,email) values (seq_user.nextval,
'001','aaaaa');   insert into user(userno,username,email) values (seq_user.nextval,'002','bbbbbb'); end;
 方法2利用中间表   insert into user(userno,username,email)         
select seq_user.nextval,username,email from(             select '003' username ,'cccccc' email from dual             union             select '004' username,'ddddddddddd' email from dual             union             select '005' username,'eeeeeee' email from dual          ) -->

     
  <insert id = "addUsers"databaseId="oracle"  parameterType="user">   
        
  <foreach collection = "users" item="user" open="begin" close="end;">           insert into user(userno,username,email)          values (seq_user.nextval,#{user.username},#{user.email}); foreach>

insert into user(userno,username,email)       select seq_user.nextval,username,email from(           <foreach collection="users" item="user" separator="union" >                    select #{user.username} username ,#{user.email} email from dual             foreach>         )
  -->

 <8>两个内置参数  

    
    <select id="selectUserByDataBaseId" resultType="user">
    <bind name="nameLike" value="'%' + lastName + '%'"/> <if test="_databaseId=='mysql'"> select * from user <if test="_parameter!=null"> where last_name=#{_parameter.lastName} if> if> <if test="_databaseId=='oracle'"> select * from user where last_name = #{nameLike} if> select>

<9>sql片段

"userColumn">
        id,email,last_name,gender
    
    <select id="selectUser" resultType="user">
        select 
        "userColumn"/>
        from user where id=#{id}
    select>

2.

"userColumn">
        id,email,last_name,gender,${AA}
    
    <select id="selectUser" resultType="user">
        select 
        "userColumn">
            "AA" value="depart_id"/>
        
        from user where id=#{id}
    select>

 

你可能感兴趣的:(mybatis四(动态sql))