First iBatis 入門樣本

來源:互聯網
上載者:User

環境: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.SQLServerDriverurl=jdbc:sqlserver://192.168.10.144;databaseName=Test;user=sapassword=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">SELECTid_1 as id,firstName_1 as firstName,lastName_1 as lastName,weightInKilograms_1 as weightInKilograms,heightInMeters_1 asheightInMetersFROM PERSONWHERE 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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.