java web中IBATIS的工作原理(1)

14 篇文章 0 订阅
2 篇文章 0 订阅

这段时间闲来无事重新研究下SSH和SSI的模型,以前写过一篇关于Hibernate对JDBC的封装的文章,这不就再来写一篇Ibatis的。

相对于Hibernate的一站式ORM解决方案而言,Ibatis则是一种半自动化的ORM的实现方案了,怎么说呢,大家看Hibernate是对数据库结构提供了较为完整的封装,提供了POJO到数据库表的全套映射机制,我们要做的就是配置好配置文件和POJO以及他们之间的映射关系即可,即便是你不懂SQL语句,也能进行开发使用。可是往往这种情况下就稍微的有点儿局限性,对于有些需求来说不容易满足,并且不利于sql的优化等。这时我们就更多的考虑到了轻量级的Ibatis这种半自动的实现形式了。要知道Ibatis的优点在于,它仅仅实现的是POJO与sql之间的映射关系,具体的sql语句需要我们自己来写才行。然后通过Ibatis的配置文件将sql所需的参数,以及返回的结果字段映射到指定的POJO。从而达到ORM效果.下面让我们来一一介绍Ibatis的配置文件以及核心函数:

1.Ibatis的实例配置文件及jdbc的配置:

 <sqlMapConfig>
    	<properties resource="SqlMap.properties"/>这里是jdbc的配置文件
    	<transactionManager type="JDBC">
    		<dataSource type="SIMPLE">
    		 <property value="${driver}" name="JDBC.Driver"/>
    		 <property value="${url}" name="JDBC.ConnectionURL"/>
    		 <property value="${username}" name="JDBC.Username"/>
    		 <property value="${password}" name="JDBC.Password"/>
    		
    		</dataSource>
    	
    	</transactionManager>
        <sqlMap resource="ncut/com/map/Student.xml"/>这里是POJO与sql语句的映射文件
    
    </sqlMapConfig>
2.POJO的基础类文件:

public class Student {
	private int id;
	private String username;
	private String password;
	private int age;
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
3.sql的映射文件:

<sqlMap>
	<typeAlias alias="Student" type="ncut.com.bean.Student"/><!-- 其实就是将bean的替换成一个短的名字 -->
	<select id="selectAllStudent" resultClass="Student">
		select * from student1
	</select>
	<!-- parameterClass  和 parameterMap都是参数类型
	     resultClass resultMap都是返回值类型  -->
	<select id="selectStudentById" parameterClass="int" resultClass="Student">
		select * from student1
		where id=#id#
	</select>
	
	<insert id="addStudent" parameterClass="Student">
		insert student1(id,username,password,age)
		values  (#id#,#username#,#password#,#age#)
	</insert>
</sqlMap>
4.SqlMapClient是Ibatis运作的核心,所有的数据库操作都是通过SqlMapClient实例来完成的。

XmlSqlMapClientBuilder是ibatis的组件,是用来根据配置文件创建SqlMapClient实例的:

private static SqlMapClient sqlMapClient;
	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();
		}
	}
然后就可以直接调用 SqlMapClient中的方法,进行数据库的CRUD了。

其中还有很多的细节,值得自己去慢慢研究。





// JBuilder API Decompiler stub source generated from class file // 2010-1-15 // -- implementation of methods is not available package com.ibatis.common.jdbc; // Imports import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.SQLWarning; import java.sql.Savepoint; import java.sql.Statement; import java.util.Map; public class SimplePooledConnection implements InvocationHandler { // Fields private static final String CLOSE = "close"; private static final Class[] IFACES; private int hashCode; private SimpleDataSource dataSource; private Connection realConnection; private Connection proxyConnection; private long checkoutTimestamp; private long createdTimestamp; private long lastUsedTimestamp; private int connectionTypeCode; private boolean valid; // Constructors public SimplePooledConnection(Connection connection, SimpleDataSource dataSource) { } // Methods public void invalidate() { } public boolean isValid() { return false;} public Connection getRealConnection() { return null;} public Connection getProxyConnection() { return null;} public int getRealHashCode() { return 0;} public int getConnectionTypeCode() { return 0;} public void setConnectionTypeCode(int connectionTypeCode) { } public long getCreatedTimestamp() { return 0L;} public void setCreatedTimestamp(long createdTimestamp) { } public long getLastUsedTimestamp() { return 0L;} public void setLastUsedTimestamp(long lastUsedTimestamp) { } public long getTimeElapsedSinceLastUse() { return 0L;} public long getAge() { return 0L;} public long getCheckoutTimestamp() { return 0L;} public void setCheckoutTimestamp(long timestamp) { } public long getCheckoutTime() { return 0L;} private Connection getValidConnection() { return null;} public int hashCode() { return 0;} public boolean equals(Object obj) { return false;} public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return null;} public Statement createStatement() throws SQLException { return null;} public PreparedStatement prepareStatement(String sql) throws SQLException { return null;} public CallableStatement prepareCall(String sql) throws SQLException { return null;} public String nativeSQL(String sql) throws SQLException { return null;} public void setAutoCommit(boolean autoCommit) throws SQLException { } public boolean getAutoCommit() throws SQLException { return false;} public void commit() throws SQLException { } public void rollback() throws SQLException { } public void close() throws SQLException { } public boolean isClosed() throws SQLException { return false;} public DatabaseMetaData getMetaData() throws SQLException { return null;} public void setReadOnly(boolean readOnly) throws SQLException { } public boolean isReadOnly() throws SQLException { return false;} public void setCatalog(String catalog) throws SQLException { } public String getCatalog() throws SQLException { return null;} public void setTransactionIsolation(int level) throws SQLException { } public int getTransactionIsolation() throws SQLException { return 0;} public SQLWarning getWarnings() throws SQLException { return null;} public void clearWarnings() throws SQLException { } public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { return null;} public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { return null;} public Map getTypeMap() throws SQLException { return null;} public void setTypeMap(Map map) throws SQLException { } public void setHoldability(int holdability) throws SQLException { } public int getHoldability() throws SQLException { return 0;} public Savepoint setSavepoint() throws SQLException { return null;} public Savepoint setSavepoint(String name) throws SQLException { return null;} public void rollback(Savepoint savepoint) throws SQLException { } public void releaseSavepoint(Savepoint savepoint) throws SQLException { } public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return null;} public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { return null;} }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值