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();
    		}
    	}
    
    }
    

    参考网络资源。

  • 相关阅读:
    vue 监听变量或对象
    同行span标签设置display:inline-block;overflow:hidden垂直对齐问题
    vue style 的scoped 使用
    判断两个对象是否相等
    js call 和 apply方法记录
    接口联调遇到的坑 总结
    onclick或者其他事件在部分移动端无效的问题
    js 迭代方法
    原生js实现类的添加和删除,以及对数据的add和update、view ,ajax请求 ,页面离开的操作,获取url中参数等方法
    input 的radio checkbox 和 select 相关操作
  • 原文地址:https://www.cnblogs.com/wucg/p/2078246.html
Copyright © 2011-2022 走看看