日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Ibatis 的簡單應用

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

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

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


下面開始 簡單應用 (CRUD)
1.使用的jar?
? ibatis-2.3.4.726.jar (ibatis就用這個jar就可以了)
? ojdbc6.jar (oracle 的驅動)


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>
    
    <!-- 這個里面的 占位符 就不能亂寫了 因為會調用 Student 的 getSid ...getSname() ..  -->
    <insert id="addStudent" parameterClass="Student">
    	insert into student(sid,sname,major,birth,score) 
    	values    (#sid#,#sname#,#major#,#birth#,#score#)
    </insert>
      
    <!-- #sid# 這個 只是一個占位符 可以更改的 -->
    <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ù)  要拼接成一個表達式 就要將#  換成  $ -->
    <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);

	//使用自動增長 主鍵 
	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) 評論(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> 
調用 
            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> 
調用 
            TestIn ti = new TestIn(); 
            ti.Count = 1; 
            ti.ArrValue = strValue; 
            return Reader.QueryForList<EmailInfoModel>("WebApp_Ibatisnet.dao.GetEmailList_Test3", ti); 
實體類: 
   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> 
調用 
                Reader.QueryForList<EmailInfoModel>("WebApp_Ibatisnet.dao.GetEmailList_Test2", "1,2,3"); 

<!--
其他信息: 
Iterate的屬性: 
prepend -可被覆蓋的SQL語句組成部分,添加在語句的前面(可選) 
property -類型為java.util.List的用于遍歷的元素(必選) 
open -整個遍歷內容體開始的字符串,用于定義括號(可選) 
close -整個遍歷內容體結束的字符串,用于定義括號(可選) 
conjunction -每次遍歷內容之間的字符串,用于定義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 的簡單應用


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 自贡市| 海南省| 酒泉市| 四川省| 安龙县| 南乐县| 隆化县| 通城县| 定结县| 固阳县| 仙游县| 嘉义县| 吉水县| 龙岩市| 韶山市| 应用必备| 谷城县| 新津县| 凤城市| 赤峰市| 华池县| 祁东县| 乌拉特后旗| 濮阳市| 萝北县| 白银市| 仲巴县| 宣武区| 怀宁县| 前郭尔| 全南县| 曲麻莱县| 洛宁县| 平安县| 江西省| 蕲春县| 宣恩县| 玉林市| 息烽县| 岫岩| 山西省|