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

ibatis2小例子(转)
这个是ibatis v2的一个小例子,刚学习,从网上找的,忘记原来的网址了,不过对于乐于分享的人很是感激,感觉还不错,上手应该挺快的,灵活的SQL构建。
感觉主要还是那个配置sql的文件里面的那些配置文件有一些难度,所以附上一份文档参考参考。


下面是实体类:
package com.tonyj.pojo;

public class Contact {
	private String firstName;
	private String lastName;
	private String email;
	private int id;
	//相应的getter和setter方法,构造方法
	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 String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Contact(String firstName, String lastName, String email) {
		super();
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
	}
	public Contact() {
		super();
		// TODO Auto-generated constructor stub
	}
}


接下来是:配置文件 SqlMapConfig.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>
	<properties resource="jdbc.properties"/>
	<settings 
		cacheModelsEnabled="true"
		enhancementEnabled="true"
		lazyLoadingEnabled="true"
		maxRequests="32"
		maxSessions="10"
		maxTransactions="5"
		useStatementNamespaces="true"/> 
	<transactionManager type="JDBC">
		<dataSource type="SIMPLE">
		     <property name="JDBC.Driver" value="${jdbc.driverClassName}"/>
			 <property name="JDBC.ConnectionURL" value="${jdbc.url}"/>
			 <property name="JDBC.Username" value="${jdbc.userName}"/>
			 <property name="JDBC.Password" value="${jdbc.password}"/>
		</dataSource>
	</transactionManager>
	<sqlMap resource="Contact.xml"/>
</sqlMapConfig>



配置文件:jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/vin
jdbc.userName=root
jdbc.password=root



配置文件:Contact.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>
	<typeAlias alias="Contact" type="com.tonyj.pojo.Contact"/>
	<!-- ibatis查询操作 -->
	<select id="getAll" resultClass="Contact">
		select * 
		from contact
	</select>
	<!-- ibatis插入1 -->
	<insert id="insert" parameterClass="Contact">
		insert into contact(firstName,lastName,email)
		values(#firstName#,#lastName#,#email#)
		<selectKey resultClass="int" keyProperty="id">
			select last_insert_id() as id
		</selectKey>
	</insert>
	<!-- ibatis插入操作2 -->
	<parameterMap class="Contact" id="insert-contact-parameter">
		<parameter property="firstName"/>
		<parameter property="lastName"/>
		<parameter property="email"/>
	</parameterMap>
	<statement id="insertContact" parameterMap="insert-contact-parameter">
		insert into contact(firstName,lastName,email) values (?,?,?);
	</statement>
	<!-- ibatis删除操作 -->
	<delete id="deleteContactById" parameterClass="int">
		delete from Contact where id=#id#
	</delete>
	<!-- ibatis更新操作 -->
	<update id="updateById" parameterClass="int">
		update Contact set lastName="tonyJ"
		where id=#id#
	</update>
	<!-- ibatis根据id查询 -->
	<resultMap class="Contact" id="result">
		<result property="id" column="id"/>
		<result property="firstName" column="firstName"/>
		<result property="lastName" column="lastName"/>
		<result property="email" column="email"/>
	<