亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Ibatis 的簡(jiǎn)單應(yīng)用

系統(tǒng) 1671 0
???? 可能有些人 都用上了Mybatis, 但是有的公司 可能還在用ibatis.
Ibatis-Home(官網(wǎng)) 想了解更多的 就看看.

myeclipse 插件地址
http://ibatis.apache.org/tools/abator

Ibatis的優(yōu)點(diǎn)(與JDBC相比)
1.減少了約61%代碼量
2.配置 使用簡(jiǎn)單
3.架構(gòu)性能增強(qiáng)
4.SQL 語句和程序代碼分離
5.簡(jiǎn)化項(xiàng)目中的分工
6.增強(qiáng)移植性


下面開始 簡(jiǎn)單應(yīng)用 (CRUD)
1.使用的jar?
? ibatis-2.3.4.726.jar (ibatis就用這個(gè)jar就可以了)
? ojdbc6.jar (oracle 的驅(qū)動(dòng))


SqlMapConfig.properties
    
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username=test
password=test

  


SqlMapConfig.xml
    
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>

  <!-- Configure a built-in transaction manager.  If you're using an 
       app server, you probably want to use its transaction manager 
       and a managed datasource -->
  <properties resource="SqlMapConfig.properties"/>
  <transactionManager type="JDBC" commitRequired="false">
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver" value="${driver}"/>
      <property name="JDBC.ConnectionURL" value="${url}"/>
      <property name="JDBC.Username" value="${username}"/>
      <property name="JDBC.Password" value="${password}"/>
    </dataSource>
  </transactionManager>

  <!-- List the SQL Map XML files. They can be loaded from the 
       classpath, as they are here (com.domain.data...) -->
  <sqlMap resource="com/ibatis/student/Student.xml"/>
  <!-- List more here...
  <sqlMap resource="com/mydomain/data/Order.xml"/>
  <sqlMap resource="com/mydomain/data/Documents.xml"/>
  -->

</sqlMapConfig>



  


Student.xml
    
<?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>
    <typeAlias alias="Student" type="com.ibatis.student.Student"/>
    
    <select id="queryAllStudent" resultClass="Student">
    	select * from student
    </select>
    
    <select id="queryStudentById" parameterClass="int" resultClass="Student">
		select * from student where sid=#sid#    
    </select>
    
    <!-- 這個(gè)里面的 占位符 就不能亂寫了 因?yàn)闀?huì)調(diào)用 Student 的 getSid ...getSname() ..  -->
    <insert id="addStudent" parameterClass="Student">
    	insert into student(sid,sname,major,birth,score) 
    	values    (#sid#,#sname#,#major#,#birth#,#score#)
    </insert>
      
    <!-- #sid# 這個(gè) 只是一個(gè)占位符 可以更改的 -->
    <delete id="deleteStudentById" parameterClass="int">
    	delete from student where sid=#sid#
    </delete>
    
    
    <update id="updateStudent" parameterClass="Student">
    	update student
    	set 
    		sname=#sname#,
    		major=#major#,
    	    birth=#birth#,
    	    score=#score#
    	 where sid=#sid#
    </update>
    
    <!-- 如果參數(shù)  要拼接成一個(gè)表達(dá)式 就要將#  換成  $ -->
    <select id="queryStudentByName" parameterClass="String" resultClass="Student">
    	select sid,sname,major,birth,score from student where sname like '$sname$'	
    </select>  
    
    <!-- Student  不區(qū)分大小寫的   -->
    <insert id="insertStudentBySequence" parameterClass="Student">
    	<selectKey resultClass="int" keyProperty="sid">
    		select STUDENT_SEQ.nextVal from dual
    	</selectKey>
    	insert into student(sid,sname,major,birth,score)
    	values (#sid#,#sname#,#major#,#birth#,#score#)
    </insert>


    	
</sqlMap>


  


IStudentDAO.java
    
package com.ibatis.student;

import java.util.List;


public interface IStudentDAO {
	public void addStudent(Student student);

	//使用自動(dòng)增長(zhǎng) 主鍵 
	public void addStudentBySequence(Student student);

	public void delStudentById(int id);

	public void updStudentById(Student student);

	
	public List<Student> queryAllStudent();

	//使用模糊查詢
	public List<Student> queryStudentByName(String name);

	public Student queryStudentById(int id);
}


  


IStudentDAOImpl.java
    
package com.ibatis.student;


import java.io.IOException;
import java.io.Reader;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

public class IStudentDAOImpl implements IStudentDAO {

	private static SqlMapClient sqlMapClient=null;
	
	static{
		try {
			Reader 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 addStudent(Student student) {
		// TODO Auto-generated method stub
		try {
			sqlMapClient.insert("addStudent", student);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void addStudentBySequence(Student student) {
		// TODO Auto-generated method stub
		try {
			//1.從數(shù)據(jù)庫徐磊中獲取主鍵值
			//2.往 student表中插入記錄
			sqlMapClient.insert("insertStudentBySequence", student);
			System.err.println(student.getSid());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}

	public void delStudentById(int id) {
		// TODO Auto-generated method stub
		try {
			System.out.println(sqlMapClient.delete("deleteStudentById", id));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List<Student> queryAllStudent() {
		// TODO Auto-generated method stub
		List<Student>  studentList=null;
		try {
			studentList=sqlMapClient.queryForList("queryAllStudent");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return studentList;
	}

	public Student queryStudentById(int id) {
		Student student=null;
		try {
			student=(Student)sqlMapClient.queryForObject("queryStudentById",id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return student;
	}

	public List<Student> queryStudentByName(String name) {
		// TODO Auto-generated method stub
		List<Student> studentList=null;
		try {
			studentList=sqlMapClient.queryForList("queryStudentByName", name);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return studentList;
	}

	public void updStudentById(Student student) {
		// TODO Auto-generated method stub
		try {
			System.out.println(sqlMapClient.update("updateStudent", student));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public  static void main(String [] args){
		IStudentDAO dao=new IStudentDAOImpl();
		//1.
		
//		for (Student student : dao.queryAllStudent()) {
//			System.out.println(student);
//		}
		
		//2.
//		System.out.println(dao.queryStudentById(1));
		
		//3.
//		Student student=new Student();
//		student.setSid(5);
//		student.setSname("admin");
//		student.setScore(100);
//		student.setMajor("Games");
//		student.setBirth(Date.valueOf("2008-08-08"));
//		
//		dao.addStudent(student);
		
		//4.
//		dao.delStudentById(1);
		
		//5.
//		Student student=new Student();
//		student.setSid(2);
//		student.setSname("luob");
//		student.setScore(50);
//		student.setMajor("Games");
//		student.setBirth(Date.valueOf("2008-08-08"));
//		dao.updStudentById(student);
		
		//6.
//		for (Student student : dao.queryStudentByName("l%")) {
//			System.out.println(student);
//		}
		
		//7.
		Student student=new Student();
		student.setSid(2);
		student.setSname("SMITH");
		student.setScore(50);
		student.setMajor("Games");
		student.setBirth(Date.valueOf("2008-08-08"));
		
		dao.addStudentBySequence(student);
	}


}



  


    
ibatis in語句參數(shù)傳入方法
Posted on 2012-02-03 10:11 yuhaibo736 閱讀(2925) 評(píng)論(1)  編輯  收藏  
 第一種:傳入?yún)?shù)僅有數(shù)組 
       <select id="GetEmailList_Test"  resultClass="EmailInfo_"> 
            select * 
            from MailInfo with (nolock) 
            where ID in 
                <iterate open="(" close=")" conjunction="," > 
                    #[]# 
                </iterate> 
        </select> 
調(diào)用 
            string[] strValue = new string[] { "1", "2", "3" }; 
            Reader.QueryForList<EmailInfoModel>("WebApp_Ibatisnet.dao.GetEmailList_Test", strValue ); 

       第二種:傳入?yún)?shù)有數(shù)組,且有其他數(shù)據(jù) 
        <select id="GetEmailList_Test3" parameterClass="TestIn" resultClass="EmailInfo_"> 
            select  top(#Count#)* 
            from MailInfo with (nolock) 
            where ID in 
            <iterate open="(" close=")" conjunction="," property="ArrValue" > 
                #ArrValue[]# 
            </iterate> 
        </select> 
調(diào)用 
            TestIn ti = new TestIn(); 
            ti.Count = 1; 
            ti.ArrValue = strValue; 
            return Reader.QueryForList<EmailInfoModel>("WebApp_Ibatisnet.dao.GetEmailList_Test3", ti); 
實(shí)體類: 
   public class TestIn 
    { 
        private int count; 
        public int Count 
        { 
            get { return count; } 
            set { count = value; } 
        } 
        private string[] arrValue; 
        public string[] ArrValue 
        { 
            get { return arrValue; } 
            set { arrValue = value; } 
        } 
    } 

       第三種:in后面的數(shù)據(jù)確定,使用string傳入 
        <select id="GetEmailList_Test2" parameterClass="TestIn" resultClass="EmailInfo_"> 
            select * 
            from MailInfo with (nolock) 
            where ID in 
            ($StrValue$) 
        </select> 
調(diào)用 
                Reader.QueryForList<EmailInfoModel>("WebApp_Ibatisnet.dao.GetEmailList_Test2", "1,2,3"); 

<!--
其他信息: 
Iterate的屬性: 
prepend -可被覆蓋的SQL語句組成部分,添加在語句的前面(可選) 
property -類型為java.util.List的用于遍歷的元素(必選) 
open -整個(gè)遍歷內(nèi)容體開始的字符串,用于定義括號(hào)(可選) 
close -整個(gè)遍歷內(nèi)容體結(jié)束的字符串,用于定義括號(hào)(可選) 
conjunction -每次遍歷內(nèi)容之間的字符串,用于定義AND或OR(可選) 
<iterate>遍歷類型為java.util.List的元素。-->

<!--like-->
<select id="selectAccount" resultMap="AccountResult" parameterClass="Account">
select * from ACCOUNT
<dynamic prepend="where">
<isNotNull property="id" prepend="and" open="(" close=")">
id = #id#
</isNotNull>
<isNotEmpty property="name" prepend="and">
name like '%$name$%'
</isNotEmpty>
</dynamic>
</select>


  


Ibatis 的簡(jiǎn)單應(yīng)用


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。?!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 婷婷视频在线 | 亚洲成人一级 | 免费看国产精品久久久久 | 尤物视频在线播放 | 久久久久久全国免费观看 | jizz成熟丰满老女人 | 老司机亚洲精品影视www | 日本a∨在线 | 在线观看福利影院 | 亚洲成 人a影院青久在线观看 | 精品国产视频在线观看 | 免费视频亚洲 | 狠狠综合久久久久综 | 久久精品亚洲乱码伦伦中文 | 欧美色视频日本片高清在线观看 | 精品视自拍视频在线观看 | 四虎永久在线精品 | 久久久久中文 | 欧美19p| 在线观看亚洲免费视频 | 欧美在线成人免费国产 | 99热久久国产综合精品久久国产 | 天天操狠狠 | 国产91精品一区二区视色 | 国产精品视频免费视频 | 四虎永久免费地ww4hu57 | 欧美理论片在线观看 | 色综合视频一区二区观看 | 国产成人禁片免费观看视频 | 久久99精品久久久久子伦小说 | 亚洲欧美一级久久精品 | 国产成人精品久久亚洲高清不卡 | 久久最新免费视频 | 国产大片91精品免费观看不卡 | 久久精品成人一区二区三区 | 日韩视频一 | 欧美色视频日本片高清在线观看 | 国产高清看片日韩欧美久久 | 91拍拍在线观看 | 伊人中文字幕在线 | 妖精www视频在线观看高清 |