1. 程式人生 > >ibatis主鍵生成方式 用序列實現自動增長 查詢方式

ibatis主鍵生成方式 用序列實現自動增長 查詢方式

select seqname.currval from dual   查詢序列當前值


select seqname.nextval from dual   查詢序列下一值


主鍵用序列來建立   


   1.先新建一個序列SEQ_RD_CI_APPSUBSYSTEM_ID
  <selectKey resultClass="Integer" keyProperty="id">      //將值賦值給id
select SEQ_RD_CI_APPSUBSYSTEM_ID.currval from dual
</selectKey>


 < selectKey> 來支援主鍵自動生成


keyProperty="id" 定義了主鍵名稱   resultClass="Integer" 定義返回型別 


在此處也可以將#sid#改為studentPKSequence.nextVal來實現同樣的效果

<sqlMap namespace="changeReport">
<!-- 代表的是resultclass的簡寫 -->
<typeAlias alias="ChgDto" type="com.readysoft.report.change.model.ChgDto" />


<!-- 變更狀態報表 

Map<String, Object> condition = new HashMap<String, Object>();
condition.put("mt1", mt1);
condition.put("mt2", mt2);
  List<ChgDto>  chg_list=changeReportService.ChangeStatus(condition); 需要num和status   -->     

<select id="changeStatus" resultClass="ChgDto"  parameterClass="Map">
select  count(chg.id)num, sta.sym status 
from  RD_CHG chg,RD_CHGSTAT sta  
where chg.status=sta.code 
and chg.OPEN_DATE  between #mt1# and #mt2#
group by sta.sym 
</select>

<!--
OPEN_DATE
 因為chg的open_date都為空     所以改為LAST_MOD_DT,
  因為後者有時間  LAST_MOD_DT有時間-->
  
  
<!-- 關閉變更    ChgDto.setMt1(mt1); ChgDto.setMt2(mt2);-->
<select id="changeClosed" resultClass="Integer"  parameterClass="ChgDto">
select  count(chg.id) num 
from  RD_CHG chg,RD_CHGSTAT sta  
where chg.status=sta.code 
and  chg.status='Closed'
and chg.CLOSE_DATE between #mt1# and #mt2#
</select>

package com.car.dao;

import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.car.dto.CarInfoDTO;
import com.car.dto.CarValueDTO;
import com.car.jdbc.DB;
import com.ibatis.sqlmap.client.SqlMapClient;

public class CarValueDAOimpl_ibatis implements CarValueDAO {

	public static SqlMapClient  sqlMapClient=null;
	static{
		Reader reader;
		try {
			reader = com.ibatis.common.resources.Resources.getResourceAsReader("SqlMapConfig.xml");
			sqlMapClient=com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader);
			reader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void add(CarValueDTO carvaluedto) {
		
	}

	public void delete(int num) {
		

	}

	public List<CarValueDTO> findAll() {
		List<CarValueDTO> 	carList=null;
		try {
		carList=sqlMapClient.queryForList("car.selectAllCar");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return carList;
	}

	public CarValueDTO findBynum(int num) {
//		List<CarValueDTO> 	carList=null;
		CarValueDTO carValueDTO=null;
		try {
			carValueDTO=(CarValueDTO) sqlMapClient.queryForObject("car.selectcarBynum", num);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return carValueDTO;
	}

	public void update(CarValueDTO carvaluedto) {
		
	}

	public static void main(String[] args) {
	    List<CarValueDTO> car_list=	new CarValueDAOimpl_ibatis().findAll();
	/*	for (int i = 0; i < car_list.size(); i++) {
			System.out.println(car_list.get(i).getCarnum());
		}
		
		for(CarValueDTO  carValueDTO:new CarValueDAOimpl_ibatis().findAll()){
			System.out.println(carValueDTO.getCarnum());
			
		}*/
	    
	CarValueDTO  carValueDTO = new CarValueDAOimpl_ibatis().findBynum(10);
	System.out.println(carValueDTO.getCarnum()+"   "+carValueDTO.getCost());
	}
	
}


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >


<sqlMap namespace="car">
<!-- typeAlias 給類取別名 -->
<typeAlias alias="Car" type="com.car.dto.CarValueDTO" />


<!-- 返回物件型別的資料 -->
	<select id="selectAllCar"  resultClass="Car">
	 select * from carvalue
	</select>


   <select id="selectcarBynum" parameterClass="Integer"  resultClass="Car">
   select * from  carvalue  where carnum=#carnum#
   </select>
   
   <insert id="">
   </insert>
   
</sqlMap>  

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="changeReport">
<!-- 代表的是resultclass的簡寫 -->
<typeAlias alias="ChgDto" type="com.readysoft.report.change.model.ChgDto" />


<!-- 變更狀態報表 
    需要num和status   -->     
<select id="changeStatus" resultClass="ChgDto"  parameterClass="Map">
select  count(chg.id)num, sta.sym status 
from  RD_CHG chg,RD_CHGSTAT sta  
where chg.status=sta.code 
and chg.OPEN_DATE  between #mt1# and #mt2#
group by sta.sym 
</select>

<!--
OPEN_DATE
 因為chg的open_date都為空     所以改為LAST_MOD_DT,
  因為後者有時間  LAST_MOD_DT有時間-->
  
  
<!-- 關閉變更 -->
<select id="changeClosed" resultClass="Integer"  parameterClass="ChgDto">
select  count(chg.id) num 
from  RD_CHG chg,RD_CHGSTAT sta  
where chg.status=sta.code 
and  chg.status='Closed'
and chg.CLOSE_DATE between #mt1# and #mt2#
</select>


 <!-- 新建變更 -->
    <select id="changeCreated" resultClass="Integer" parameterClass="ChgDto">
select  count(chg.id) num1 
from   RD_CHG chg
where   chg.OPEN_DATE between #mt1# and #mt2#
</select>


<!--變更分類  所有的 -->
<select id="changeType" resultClass="ChgDto" >
select  count(chg.id) num,ctg.SYM
from   RD_CHG chg,RD_CHG_CATEGORY ctg 
where chg.Z_CHG_CATEGORY=ctg.ID
group by ctg.SYM 
</select>






<!-- 變更分類 -->
<!-- 網路裝置 -->
<select id="changeType1" resultClass="Integer" parameterClass="ChgDto">
select  count(chg.id) num
from   RD_CHG chg,RD_CHG_CATEGORY ctg 
where chg.Z_CHG_CATEGORY=ctg.ID
where ctg.ID=61
and chg.OPEN_DATE between #mt1# and #mt2# 
group by ctg.SYM 
</select>

<!-- 軟體產品 -->
<select id="changeType2" resultClass="Integer" parameterClass="ChgDto">
select  count(chg.id) num
from   RD_CHG chg,RD_CHG_CATEGORY ctg 
where chg.Z_CHG_CATEGORY=ctg.ID
where ctg.ID=163
and chg.OPEN_DATE between #mt1# and #mt2# 
group by ctg.SYM 
</select>

<!-- 主機裝置 -->
<select id="changeType3" resultClass="Integer" parameterClass="ChgDto">
select  count(chg.id) num
from   RD_CHG chg,RD_CHG_CATEGORY ctg 
where chg.Z_CHG_CATEGORY=ctg.ID
where ctg.ID=165
and chg.OPEN_DATE between #mt1# and #mt2# 
group by ctg.SYM 
</select>


  
   




</sqlMap>