这里是引用
一、主要流程
1、通过解析两个xml文件,将他们共同变成一个configuration对象。不同的mapper.xml生成不同的configuration对象
2、通过configuration创建sessionFactory对象
3、再由sessionFactory生成sqlsession对象。它包含一组statement,可以理解为一个方法就是一个会话
4、statement是核心,一次sql语句的执行就是一个statement
5、通过两个handler将参数和结果封装进statement
二、高阶用法
(一)动态sql
主要利用@SelectProvider等注解
1、Dao类
java
package com.anmining.truck.dispatch.modules.report.dao;
import com.anmining.truck.dispatch.modules.report.entity.TruckReportData;
import com.anmining.truck.dispatch.modules.report.utils.MapperSql;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
@Mapper
public interface ReportDao {
@SelectProvider(type = MapperSql.class, method = "getUnloadAndExcavator")
List<Map<String, Object>> getUnloadAndExcavator(Map s);
}
2、MapperSql类
java
package com.anmining.truck.dispatch.modules.report.utils;
import java.util.List;
import java.util.Map;
public class MapperSql {
public String getUnloadAndExcavator(Map map) throws IllegalAccessException {
StringBuilder sql = new StringBuilder();
List<Map<String,String>> unloadList = (List) map.get("allUnloadName");
String startTime = map.get("startTime").toString();
String endTime = map.get("endTime").toString();
sql.append("select tb.excavator_name,tb.excavator_code,");
for (int i=0;i<unloadList.size();i++){
Map unload = unloadList.get(i);
sql.append("MAX ( CASE tb.unloading_name WHEN '"+unload.get("unloading_name")+"' THEN tb.yield ELSE 0 END ) \""+unload.get("unloading_name")+"\",");
if (i==unloadList.size()-1){
//去掉最后的,
sql.deleteCharAt(sql.length()-1);
}
}
sql.append(" FROM\n" +
"\t(\n" +
"\tSELECT\n" +
"\t\te.excavator_name,\n" +
"\t\tu.unloading_name,e.excavator_code,\n" +
"\t\tcount ( w.yield ) yield \n" +
"\tFROM\n" +
"\t\tp_workflow_statistical_table w\n" +
"\t\tLEFT JOIN b_excavator_base_info e ON w.excavator_code = e.excavator_code\n" +
"\t\tLEFT JOIN b_unloading_base_info u ON w.unloading_code = u.unloading_code\n" +
"\t\tLEFT JOIN b_truck_base_info T ON w.truck_code = T.truck_code \n" +
"\tWHERE\n" +
"\t\tcreate_time >= '" +startTime+"' "+
"\t\tAND create_time <= '" +endTime+"' "+
"\tGROUP BY\n" +
"\t\te.excavator_name,\n" +
"\t\tu.unloading_name,e.excavator_code \n" +
"\t) tb \n" +
"GROUP BY\n" +
"\ttb.excavator_name,tb.excavator_code ORDER BY tb.excavator_name");
return sql.toString();
}
}