`
liran_java
  • 浏览: 66830 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

ibatis与spring整合实例(附源码)

阅读更多

使用 SQL Map ,能够大大减少访问关系数据库的代码。 SQL Map 使用简单的 XML 配置文件将 Java Bean 映射成 SQL 语句,对比其他的数据库持续层和 ORM 框架(如 JDO 的实现, Hibernate 等), SQL Map 即有 ORM 的功能又具备 SQL 的灵活性和高效性。是一个非常优秀的 JDBC 的替代品。

SQL MAP 最大的优点在于它简单易学, 只要熟悉 Java Bean XML SQL ,就能使您充分发挥 SQL 语句的能力。同时 SQL MAP 得到了 SPRING DAO 支持,让我们的持久层工作更加简单,高效。下面介绍 IBATIS SPRING 结合的详细过程。

加入 SQL MAP 功能

数据库表 T_PERSON

CREATE TABLE `person` (
  `id` int(11) NOT NULL default '0',
  `name` varchar(255) default NULL,
  `info` varchar(255) default NULL,
  `info_blob` blob,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 

映射对象 PERSON

怎样将数据库表映射为对象呢? SQL Map O/R 之间的关系没有限制,一个数据库表可以映射成多个对象,反过来也是成立的。下面建立一对一映射对象 PERSON

public class Person implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private long id ;
	private String name;
	private String info;
	private byte[] info_blob ;
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	public byte[] getInfo_blob() {
		return info_blob;
	}
	public void setInfo_blob(byte[] info_blob) {
		this.info_blob = info_blob;
	}
 

配置 SQL_MAP 配置文件

怎样把数据库对象与 JAVA 对象联系起来,当然是通过配置文件了:

SQL_MAP 映射文件 SQLMAP_PERSON

<sqlMap namespace="Person">
	
	<typeAlias alias="Person" type="org.lr.ibatis.bean.Person" />
	<cacheModel id="person-cache" type="OSCACHE">
		<flushInterval hours="24" />
		<flushOnExecute statement="queryAll" />
		<property name="size" value="1000" />
	</cacheModel>
	<select id="queryAll" resultClass="Person" cacheModel="person-cache">
		select * from person
	</select>

	<select id="queryById" parameterClass="java.lang.String"
		resultClass="Person">
		select * from person where id=#id#
	</select>
	
	<update id="updateBlob" parameterClass="org.lr.ibatis.bean.Person">
		update person set info_blob = #info_blob#
	</update>

</sqlMap>

 

SQL_MAP 配置文件 IBATIS-MAP-CONFIG

<sqlMapConfig>
	<settings maxRequests="256" maxSessions="64" maxTransactions="16"
		enhancementEnabled="true" />
	<sqlMap resource="org/lr/ibatis/bean/sqlmap_person.xml"></sqlMap>
</sqlMapConfig>
 

PERSON类操作类

SRPING为ibatis dao提供了一个的工具类SqlMapClientDaoSupport,它提供了setSqlMapClient(SqlMapClient)方法和getSqlMapClientTemplate()做为ibatis的支持,我们的dao类之需要继承这个类就可以了

public class PersonDaoImp extends SqlMapClientDaoSupport implements PersonDao {

	@SuppressWarnings("unchecked")
	public List<Person> getAllPerson() {
		// TODO Auto-generated method stub
		return getSqlMapClientTemplate().queryForList("queryAll");
	}

	public Person getPersonById(String id ) {
		// TODO Auto-generated method stub
		return (Person)getSqlMapClientTemplate().queryForObject("queryById",id);
	}

	public void updateBlob(Person person) {
		// TODO Auto-generated method stub
		getSqlMapClientTemplate().update("updateBlob",person);
	}

}

 personService

ublic class PersonServiceImp implements PersonService {
	
	private PersonDao personDao;
	
	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	public List<Person> getAllPerson() {
		// TODO Auto-generated method stub
		return personDao.getAllPerson();
	}

	public Person getPersonById(String id) {
		// TODO Auto-generated method stub
		return personDao.getPersonById(id);
	}

	public void updateBlob(Person person) {
		// TODO Auto-generated method stub
		personDao.updateBlob(person);
	}

  SRPING 集成

配置spring 管理

 

	<!-- sql map client 配置 -->
	<bean id="sqlMapClient"
		class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation"
			value="ibatis-sql-map-config.xml" />
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- ************************************************************************-->
	<!-- //////////////////////ibatis事务代理配置///////////////////////////////-->
	<!-- ************************************************************************-->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<bean id="txProxyTemplate" abstract="true"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager">
			<ref bean="transactionManager" />
		</property>
		<property name="transactionAttributes">
			<props>
				<prop key="create*">PROPAGATION_REQUIRED</prop>
				<prop key="save*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
			</props>
		</property>
	</bean>

	<!-- ************************************************************************-->
	<!-- //////////////////////dao配置///////////////////////////////////////////-->
	<!-- ************************************************************************-->

	<bean id="personDao"
		class="org.lr.ibatis.dao.imp.PersonDaoImp">
		<property name="sqlMapClient">
			<ref bean="sqlMapClient" />
		</property>
	</bean>
	<!-- ************************************************************************-->
	<!-- ////////////////////////////////SERVICE代理配置//////////////////////////-->
	<!-- ************************************************************************-->
	<bean id="personServiceTarget"
		class="org.lr.ibatis.service.imp.PersonServiceImp">
		<property name="personDao" ref="personDao"></property>
	</bean>
	<!-- ************************************************************************-->
	<!-- ///////////////////////////SERVICE配置///////////////////////////////////-->
	<!-- ************************************************************************-->
	<bean id="personService" parent="txProxyTemplate">
		<property name="target" ref="personServiceTarget"></property>
	</bean>

    测试

 

	public void testPersonService() {

		long start = System.currentTimeMillis();
		PersonService personService = (PersonService) context
				.getBean("personService");
		List<Person> list = new LinkedList<Person>();
		list = personService.getAllPerson();
		long end = System.currentTimeMillis();
		System.out.println("执行时间------" + (end - start));
		for (Person p : list) {
			System.out.println(p.getId());
			System.out.println(p.getName());
			System.out.println(p.getInfo());
			System.out.println(new String(p.getInfo_blob()));

		}
	}

	public void testPersonUpdate() throws Exception{
		FileInputStream fis = new FileInputStream(new File("D:/savePath/pro.txt"));
		byte[] temp = new byte[(int)fis.available()];
		fis.read(temp);
		PersonService personService = (PersonService) context.getBean("personService");
		Person person = new Person();
		person.setId(3);
		person.setName("zhangsan");
		person.setInfo("张三");
		person.setInfo_blob(temp);
		personService.updateBlob(person);
	}

 结束

 

分享到:
评论
1 楼 streamfly 2010-07-20  
感谢啊 ,兄弟!我一直在找这样的资料呢!!!

相关推荐

Global site tag (gtag.js) - Google Analytics