1. 程式人生 > >spring jdbcTemplate 使用佔位符(?)的query方法進行多表查詢

spring jdbcTemplate 使用佔位符(?)的query方法進行多表查詢

1 在spring 的配置檔案中applicationContext.xml中,配置service,dao.(前臺使用的是flex,把flex也配置上了。)

<bean id="busSuperCapityAnalyDao" class="com.tm.dao.impl.sjwj.BusSuperCapityAnalyDaoImpl"></bean>
 <bean id="busSuperCapityAnalyService" class="com.tm.service.impl.sjwj.BusSuperCapityAnalyServiceImpl">
  <flex:remoting-destination />
 </bean>


2 寫service介面

/**
 * 車輛超級電容資料分析service
 * @author hanshibo
 */
public interface BusSuperCapityAnalyService {
	public List<BusSuperCapityDataModel>  queryBusSuperCapityData(String busno ,String startTime,String endTime);
}

3 寫service實現

/**
 * 車輛超級電容分析ServiceImpl
 * @author hanshibo
 *
 */
public class BusSuperCapityAnalyServiceImpl implements BusSuperCapityAnalyService{
	@Resource 
	private BusSuperCapityAnalyDao busSuperCapityAnalyDao;
	public List<BusSuperCapityDataModel> queryBusSuperCapityData(String busno,String startTime ,String endTime) {
		
		return busSuperCapityAnalyDao.queryBusSuperCapityData(busno, startTime, endTime);
	}

}

4  寫dao層介面

/**
 * 超級電容資料分析Dao
 * @author hanshibo
 *
 */
public interface BusSuperCapityAnalyDao {

	public List<BusSuperCapityDataModel> queryBusSuperCapityData(String busno,String startTime ,String endTime);
}

5 寫dao層實現。使用spring jdbcTemplate 的query方法,用佔位符(?)查詢。

/**
 * 車輛超級電容資料分析daoImpl
 * @author hanshibo
 *
 */
public class BusSuperCapityAnalyDaoImpl implements BusSuperCapityAnalyDao{
         @Resource
	private JdbcTemplate jdbcTemplate;
	
	private String partParam ;

public List<BusSuperCapityDataModel> queryBusSuperCapityData(String busno,
			String startTime, String endTime) {
	List<BusSuperCapityDataModel> busList = null;
		
	busList = new ArrayList<BusSuperCapityDataModel>();
         String params[]=new String[]{busno,startTime,endTime}; 
         int[] types = new int[]{Types.VARCHAR,Types.VARCHAR,Types.VARCHAR};  
         busList=jdbcTemplate.query(getcurSql(),params,types, new SuperCapityDataMapper());
         return busList;
}

private String getcurSql(){
String sqlStr=" select t.bus_job_no ,to_char(l.upload_time, 'YYYY-MM-DD HH24:MI:SS')      uploadTime,l.SINGLECAPAMAXVOL,l.CAPAMAXVOLTAGENO,l.SINGLECAPAMINVOL,l.CAPAMINVOLTAGENO,l.SINGLECAPAMAXTEM,l.SINGLECAPAMAXTEMNO,l.SINGLECAPAMINTEM, l.SINGLECAPAMINTEMNO" +
" from tm_engine_basic_log l,tm_newenergy_bus_info t"+
" where t.bus_no =?"+
" and   l.upload_time between to_date(?, 'yyyy-mm-dd hh24:mi:ss') " +
" and   to_date(?, 'yyyy-mm-dd hh24:mi:ss')" +
" and l.bus_no = t.bus_no";
	return sqlStr ; 
}

        /**
	 *RowMapper 取值
	 */
	class SuperCapityDataMapper implements RowMapper<BusSuperCapityDataModel>{

		public BusSuperCapityDataModel mapRow(ResultSet rs, int rowID)
				throws SQLException {
			BusSuperCapityDataModel  bm = new BusSuperCapityDataModel();
			bm.setBusjobno(rs.getString("bus_job_no"));
			bm.setUploadTime(rs.getString("uploadTime"));
			bm.setCapaMaxVolTageNo(rs.getInt("CAPAMAXVOLTAGENO"));
			bm.setCapaminVolTageNo(rs.getInt("CAPAMINVOLTAGENO"));
			bm.setSingleCapaMaxtem(rs.getInt("SINGLECAPAMAXTEM"));
			bm.setSingleCapaMaxVol(rs.getInt("SINGLECAPAMAXVOL"));
			bm.setSingleCapaMinTemNo(rs.getInt("SINGLECAPAMINTEMNO"));
			bm.setSingleCapaMinVol(rs.getInt("SINGLECAPAMINVOL"));
			bm.setSingleCpaMaxTemNo(rs.getInt("SINGLECAPAMAXTEMNO"));
			bm.setSingleCpaMinTem(rs.getInt("SINGLECAPAMINTEM"));
			return bm;
		}
	}



}

如下注意:

String params[]=new String[]{busno,startTime,endTime};   
int[] types = new int[]{Types.VARCHAR,Types.VARCHAR,Types.VARCHAR};  
busList=jdbcTemplate.query(getcurSql(),params,types, new SuperCapityDataMapper());

1     params引數,這是一個String 陣列,這個數組裡面的引數,都是要使用佔用符(?)的傳進來的引數(形式引數)。這裡就三個busno,startTime,endTime,這3個引數,都是sql語句裡面,where後面要使用的引數條件。

2    types是一個int 型別的陣列,這個陣列主要對應params引數裡面的引數資訊。types陣列主要存放一些引數資料型別。這裡是Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,因為params引數裡面busno,startTime,endTime 這三個引數,都是字串。

3    new SuperCapityDataMapper 。使用Mapper取到值,並且存到Model實體資訊裡面。

4    使用jdbcTemplate.query(getcurSql(),params,types, new SuperCapityDataMapper());  查詢得到一個list 。