zoukankan      html  css  js  c++  java
  • First iBatis 入门示例

    环境:windows Server2003, Eclipse helios, MS SqlServer 2008

    step 1: SQL:

    Create table Person
    (
    id_1 bigint identity primary key,
    firstName_1 varchar(20),
    lastName_1 varchar(20),
    weightInKilograms_1 decimal,
    heightInMeters_1 decimal
    )
    

    step 2: dbConfig.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sqlMapConfig
     PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
     "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
    <sqlMapConfig>
    	<properties resource="db.properties" />
    	<settings cacheModelsEnabled="true" enhancementEnabled="true"
    		lazyLoadingEnabled="true" maxRequests="32" maxSessions="10"
    		maxTransactions="5" useStatementNamespaces="false" />
    
    	<transactionManager type="JDBC">
    		<dataSource type="SIMPLE">
    			<property name="JDBC.Driver" value="${driver}" />
    			<property name="JDBC.ConnectionURL" value="${url}" />
    			<property name="JDBC.Username" value="${user}" />
    			<property name="JDBC.Password" value="${password}" />
    		</dataSource>
    	</transactionManager>
    	<sqlMap resource="com/ibatis/Person.xml" />
    </sqlMapConfig>
    

    step 3: db.properties

    driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
    url=jdbc:sqlserver://192.168.10.144;databaseName=Test;
    user=sa
    password=xxxxxx
    

    step 4: Person.java

    package com.ibatis;
    
    public class Person {
    	private int id;
    	 private String firstName;
    	 private String lastName;
    	 private double weightInKilograms;
    	private double heightInMeters;
    
    	public int getId() {
    		return id;
    	}
    
    	public void setId(int id) {
    		this.id = id;
    	}
    
    	public String getFirstName() {
    		return firstName;
    	}
    
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
    
    	public String getLastName() {
    		return lastName;
    	}
    
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
    
    	public double getWeightInKilograms() {
    		return weightInKilograms;
    	}
    
    	public void setWeightInKilograms(double weightInKilograms) {
    		this.weightInKilograms = weightInKilograms;
    	}
    
    	public double getHeightInMeters() {
    		return heightInMeters;
    	}
    
    	public void setHeightInMeters(double heightInMeters) {
    		this.heightInMeters = heightInMeters;
    	}
    }
    

    step 5: Person.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sqlMap
     PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
     "http://www.ibatis.com/dtd/sql-map-2.dtd">
    <sqlMap namespace="Person">
    
    	<!-- Use primitive wrapper type (e.g. Integer) as parameter and allow results 
    		to be auto-mapped results to Person object (Java Bean) properties -->
    	<select id="getPerson" parameterClass="int" resultClass="com.ibatis.Person">
    		SELECT
    		id_1 as id,
    		firstName_1 as firstName,
    		lastName_1 as lastName,
    		weightInKilograms_1 as weightInKilograms,
    		heightInMeters_1 as
    		heightInMeters
    		FROM PERSON
    		WHERE id_1 = #id#
     </select>
    
    
    </sqlMap>
    

    step 6: Test

    package com.test;
    
    import java.io.IOException;
    import java.io.Reader;
    import java.sql.SQLException;
    
    import com.ibatis.Person;
    import com.ibatis.common.resources.Resources;
    import com.ibatis.sqlmap.client.SqlMapClientBuilder;
    
    public class TestPerson {
    
    	private static final String resource = "dbConfig.xml";
    
    	public static void main(String[] args) {
    		try {
    			com.ibatis.sqlmap.client.SqlMapClient sqlMap = null;
    			Reader reader = Resources.getResourceAsReader(resource);
    			sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
    
    			Person person = (Person) sqlMap.queryForObject("getPerson",
    					new Integer(1));
    			System.out.println(person.getFirstName()); // 打印
    			System.out.println(person.getLastName()); // 打印
    			System.out.println(person.getHeightInMeters()); // 打印
    			System.out.println(person.getWeightInKilograms()); // 打印
    
    		} catch (IOException e) {
    			e.printStackTrace();
    		} catch (SQLException e1) {
    			e1.printStackTrace();
    		}
    	}
    
    }
    

    参考网络资源。

  • 相关阅读:
    十问5.12汶川大地震
    JZ035数组中的逆序对
    JZ037数字在排序数组中出现的次数
    JZ039平衡二叉树
    JZ033丑数
    JZ040数组中只出现一次的数字
    JZ032把数组排成最小的数
    JZ036两个链表的第一个公共结点
    JZ034第一个只出现一次的字符位置
    JZ031从 1 到 n 整数中 1 出现的次数
  • 原文地址:https://www.cnblogs.com/wucg/p/2078246.html
Copyright © 2011-2022 走看看