日期:2014-05-16  浏览次数:20570 次

ibatis 配置 连上 h2 数据库
先说说ibatis配置吧
新建一个项目
我的项目名称是ibatis

1,导入ibatis所需要的包,因为我使用了日志log4j,所以也把log4j的依赖包也导进来

ibatis-2.3.4.726.jar
commons-logging-api.jar
commons-logging-1.1.jar
commons-dbcp-1.2.2.jar
commons-pool.jar
h2-1.3.154.jar (h2数据库所需要jar包)

2,在src目录下创建一个xml文件 sqlmap-config.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>
	<settings cacheModelsEnabled="true" enhancementEnabled="true"
		lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"
		maxSessions="10" maxTransactions="5" useStatementNamespaces="false" />
	<transactionManager type="JDBC">
		<dataSource type="SIMPLE">
			<property name="JDBC.Driver" value="org.h2.Driver" />
			<property name="JDBC.ConnectionURL" value="jdbc:h2:database/h2db" />
			<property name="JDBC.Username" value="root" />
			<property name="JDBC.Password" value="" />
			<property name="Pool.MaximumActiveConnections" value="10" />
			<property name="Pool.MaximumIdleConnections" value="5" />
			<property name="Pool.MaximumCheckoutTime" value="120000" />
			<property name="Pool.TimeToWait" value="500" />
			<property name="Pool.PingQuery" value="select 1 from sample" />
			<property name="Pool.PingEnabled" value="false" />
			<property name="Pool.PingConnectionsOlderThan" value="1" />
			<property name="Pool.PingConnectionsNotUsedFor" value="1" />
		</dataSource>
	</transactionManager>
	<!-- 引入user操作文件 -->
	<sqlMap resource="sqlmap/user-sql.xml"/>
</sqlMapConfig>


3,在src目录下创建文件夹 sqlmap,然后在文件夹下创建xml文件user-sql.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 namespace="User">
	<typeAlias alias="user" type="model.User" />
	<select id="getUser" parameterClass="java.lang.String"
		resultClass="user">
		<![CDATA[    select  name,  sex   from t_user   where name = #name#   ]]>
	</select>
	<select id="getAllUser" resultClass="user">
		<![CDATA[   select   name,  sex  from t_user  ]]>
	</select>
	<update id="updateUser" parameterClass="user">
		<![CDATA[   UPDATE t_user  SET name=#name#, sex=#sex# WHERE id = #id# ]]>
	</update>
	<insert id="insertUser" parameterClass="user">
		INSERT INTO t_user (
		name, sex) VALUES ( #name#, #sex# )
	</insert>
	<delete id="deleteUser" parameterClass="java.lang.String">
		delete from t_user where
		id=#value#
	</delete>
</sqlMap>


4,在src目录下新建文件夹model,然后在文件夹下新建一个类 User.java
package model;

import java.io.Serializable;

public class User implements Serializable {
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String name;
	private Integer sex;

	public User() {
	}
	public Integer getId() {
		return this.id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return this.name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getSex() {
		return this.sex;
	}
	public void setSex(Integer sex) {
		this.sex = sex;
	}

}


5,在src目录下新建h2demo.java类
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

import model.User;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
/**
 * 
 * @代码功能:测试demo
 * @packageName 
 * @file_name h2demo.java
 * @type_name h2de