mybatis注解開發-動態SQL


mybatis注解開發-動態SQL

實體類以及表結構

在mybatis-config.xml中注冊mapper接口

--------------------------

動態查詢@SelectProvider

EmployeeMapper接口

復制代碼
package Intefaceproxy.Dyno;

import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.SelectProvider;
import model.Employee;

public interface EmployeeMapper {
    //動態查詢  type:指定一個類    method:使用這個類中的selectWhitParamSql方法返回的sql字符串  作為查詢的語句
    @SelectProvider(type=Intefaceproxy.Dyno.EmployeeDynaSqlProvider.class,method="selectWhitParamSql")
    List<Employee> selectWithParam(Map<String,Object> param);
}
復制代碼

返回sql語句的類

復制代碼
package Intefaceproxy.Dyno;

import java.util.Map;

import org.apache.ibatis.jdbc.SQL;

public class EmployeeDynaSqlProvider {
    //方法中的關鍵字是區分大小寫的  SQL SELECT WHERE
    //該方法會根據傳遞過來的map中的參數內容  動態構建sql語句
    public String selectWhitParamSql(Map<String, Object> param) {
        return new SQL() {
            {
                SELECT("*");
                FROM("tb_employee");
                if (param.get("id")!=null) {
                    WHERE("id=#{id}");
                }
                if(param.get("loginname")!=null) {
                    WHERE("loginname=#{loginname}");
                }
                if(param.get("password")!=null) {
                    WHERE("password=#{password}");
                }
                if(param.get("name")!=null) {
                    WHERE("name=#{name}");
                }
                if(param.get("sex")!=null) {
                    WHERE("sex=#{sex}");
                }
                if(param.get("age")!=null) {
                    WHERE("age=#{age}");
                }
                if(param.get("phone")!=null) {
                    WHERE("phone=#{phone}");
                }
                if(param.get("sal")!=null) {
                    WHERE("sal=#{sal}");
                }
                if(param.get("state")!=null) {
                    WHERE("state=#{state}");
                }
            }
            
        }.toString();
    }
}
復制代碼

測試:

當然也可以傳遞employee對象

接口:

//傳遞employee對象
@SelectProvider (type=Intefaceproxy.Dyno.EmployeeDynaSqlProvider. class ,method= "selectWhitEmployeeSql" )
List<Employee>selectWithEmployee(Employee employee);

返回sql的類

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//selectWhitEmployeeSql
     public  String selectWhitEmployeeSql(Employee employee) {
         return  new  SQL() {
             {
                 SELECT( "*" );
                 FROM( "tb_employee" );
                 if  (employee.getId()!= null ) {
                     WHERE( "id=#{id}" );
                 }
                 if (employee.getLoginname()!= null ) {
                     WHERE( "loginname=#{loginname}" );
                 }
                 if (employee.getPassword()!= null ) {
                     WHERE( "password=#{password}" );
                 }
                 if (employee.getName()!= null ) {
                     WHERE( "name=#{name}" );
                 }
                 if (employee.getSex()!= null ) {
                     WHERE( "sex=#{sex}" );
                 }
             }
         }.toString();
     }

測試:

------------------------------

動態插入@InsertProvider

 

1
2
3
4
//動態插入
     @InsertProvider (type=Intefaceproxy.Dyno.EmployeeDynaSqlProvider. class ,method= "insertEmployeeSql" )
     @Options (useGeneratedKeys= true ,keyProperty= "id" )
     int  insertEmployee(Employee employee);

 

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//insertEmployeeSql
     public  String insertEmployeeSql(Employee employee) {
         return  new  SQL() {
             {
                 INSERT_INTO( "tb_employee" );
                 if (employee.getLoginname()!= null ) {
                     VALUES( "loginname" , "#{loginname}" );
                 }
                 if (employee.getPassword()!= null ) {
                     VALUES( "password" "#{password}" );
                 }
                 if (employee.getName()!= null ) {
                     VALUES( "name" "#{name}" );
                 }
                 if (employee.getSex()!= null ) {
                     VALUES( "sex" "#{sex}" );
                 }
                 if (employee.getAge()!= null ) {
                     VALUES( "age" "#{age}" );
                 }
                 if (employee.getPhone()!= null ) {
                     VALUES( "phone" "#{phone}" );
                 }
                 if (employee.getSal()!= null ) {
                     VALUES( "sal" "#{sal}" );
                 }
                 if (employee.getState()!= null ) {
                     VALUES( "state" "#{state}" );
                 }
             }
         }.toString();
     }

測試:

-------------------------

@UpdateProvider

 

1
2
3
//動態更新
     @UpdateProvider (type=Intefaceproxy.Dyno.EmployeeDynaSqlProvider. class ,method= "updateEmployeeSql" )
     void  updateEmployee(Employee employee);

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//updateEmployeeSql
     public  String updateEmployeeSql(Employee employee) {
         return  new  SQL() {
             {
                 UPDATE( "tb_employee" );
                 if (employee.getLoginname()!= null ) {
                     SET( "loginname=#{loginname}" );
                 }
                 if (employee.getPassword()!= null ) {
                     SET( "password=#{password}" );
                 }
                 if (employee.getName()!= null ) {
                     SET( "name=#{name}" );
                 }
                 if (employee.getSex()!= null ) {
                     SET( "sex=#{sex}" );
                 }
                 if (employee.getAge()!= null ) {
                     SET( "age=#{age}" );
                 }
                 if (employee.getPhone()!= null ) {
                     SET( "phone=#{phone}" );
                 }
                 if (employee.getSal()!= null ) {
                     SET( "sal=#{sal}" );
                 }
                 if (employee.getState()!= null ) {
                     SET( "state=#{state}" );
                 }
                 WHERE( "id=#{id}" );
             }
         }.toString();
     }

測試:

----------------------------

@DeleteProvider

1
2
3
//動態刪除
@DeleteProvider (type=Intefaceproxy.Dyno.EmployeeDynaSqlProvider. class ,method= "deleteEmployeeSql" )
void  deleteEmployee(Employee employee);

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//deleteEmployeeSql
public  String deleteEmployeeSql(Employee employee) {
     return  new  SQL() {
         {
             DELETE_FROM( "tb_employee" );
             if (employee.getLoginname()!= null ) {
                 WHERE( "loginname=#{loginname}" );
             }
             if (employee.getPassword()!= null ) {
                 WHERE( "password=#{password}" );
             }
             if (employee.getName()!= null ) {
                 WHERE( "name=#{name}" );
             }
         }
     }.toString();
}

 測試:

 

原文地址:https://www.cnblogs.com/Joke-Jay/p/8524722.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM