ibatis和spring整合开发 例子

与卿画眉共浮生 2011-06-25

spring其实已经提供了支持ibatis的模板,我们只需要在配置中配置好这些文件,就可以进行相关的开发了。

下面是我写的一个例子,其中参考了:http://static.springsource.org/spring/docs/2.0.0/reference/orm.html的一些内容,有兴趣可以登录该网页学习:

首先我们看一下spring的相关配置application.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource">
			<ref bean="dataSource"/>
		</property>
	</bean>
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://127.0.0.1/ibatis</value>
		</property>
		<property name="username">
			<value>root</value>
		</property>
		<property name="password">
			<value>123</value>
		</property>
	</bean>
	
	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation">
			<value>classpath:sqlMapConfig.xml</value>
		</property>
		<property name="dataSource">
			<ref bean="dataSource"/>
		</property>
	</bean>
	
	<bean id="userInfoDao" class="com.openv.ibatis.dao.impl.UserInfoDaoImpl">
		<property name="sqlMapClient">
			<ref bean="sqlMapClient"/>
		</property>
	</bean>

</beans>

 在这个里面我们配置了数据源(dataSource)其实就是连接数据库哈。txManager为事务管理器,不过这里我没有使用哈,暂且让它放在那里吧。。。。sqlMapClient:org.springframework.orm.ibatis.SqlMapClientFactoryBean该类继承了spring中的FactoryBean和InitializingBean,该类是用于创建一个iBatis的SqlMapClient,在spring的应用配置宏我们就创建好了一个共享的iBatis的SqmMapClient,它可以通过依赖注入的方式传到iBatis-based DAOs。还有一个配置就是userInfoDao了,这就是一个用户自定义接口,重点在于将sqlMapClient注入到它的实现类中

下一个配置文件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>

	<settings lazyLoadingEnabled="true" useStatementNamespaces="true"/>
	
	<sqlMap resource="com/openv/ibatis/vo/UserInfo.xml"/>

</sqlMapConfig>

 在这里我只用一个操作的数据库表,配置的路径在com/openv/ibatis/vo/UserInfo.xml中

下面查看一下UserInfo.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="UserInfo">
	<typeAlias alias="UserInfo" type="com.openv.ibatis.vo.UserInfo"/>
	
	<resultMap class="UserInfo" id="UserInfoResult">
		<result property="id" column="id"/>
		<result property="firstName" column="first_name"/>
		<result property="lastName" column="last_name"/>
		<result property="email" column="email"/>
	</resultMap>
	<select id="UserInfo.select" resultClass="UserInfo">
		select * from user_info
	</select>
	<select id="UserInfo.selectById" resultClass="UserInfo" parameterClass="UserInfo">
		select * from user_info
		<dynamic prepend="where">
			<isNotEmpty prepend="AND" property="id">(id=#id#)</isNotEmpty>
			<isNotEmpty prepend="AND" property="firstName">(first_name=#firstName#)</isNotEmpty>
			<isNotEmpty prepend="AND" property="lastName">(last_name=#lastName#)</isNotEmpty>
			<isNotEmpty prepend="AND" property="email">(email=#email#)</isNotEmpty>
		</dynamic>
	</select>
	<insert id="UserInfo.insertUserInfo" parameterClass="UserInfo">
		insert into user_info(first_name,last_name,email) values(#firstName#,#lastName#,#email#)
		<selectKey resultClass="Integer" keyProperty="id">
			SELECT LAST_INSERT_ID() AS ID
		</selectKey>
	</insert>
	<update id="UserInfo.updateUserInfoById">
		update user_info
		<dynamic prepend="set">
			<isNotEmpty property="firstName" prepend=",">first_name=#firstName#</isNotEmpty>
			<isNotEmpty property="lastName" prepend=",">last_name=#lastName#</isNotEmpty>
			<isNotEmpty property="email" prepend=",">email=#email#</isNotEmpty>
		</dynamic>
		where id=#id#
	</update>
	<update id="UserInfo.updateLastName" parameterClass="java.util.Map">
		update user_info set last_name=#newLastName# where last_name=#oldLastName#
	</update>
	<delete id="UserInfo.deleteUserInfo">
		delete from user_info
		<dynamic prepend="where">
			<isNotEmpty property="id" prepend="AND">(id=#id#)</isNotEmpty>
			<isNotEmpty property="firstName" prepend="AND">(first_name=#firstName#)</isNotEmpty>
			<isNotEmpty property="lastName" prepend="AND">(last_name=#lastName#)</isNotEmpty>
			<isNotEmpty property="email" prepend="AND">(email=#email#)</isNotEmpty>
		</dynamic>
	</delete>
</sqlMap>

 该配置文件主要是实现对数据库的操作

还有一个properties文件:在该文件中配置一下就可以显示ibatis调用的sql,log4j.properties:

#------------------------------------------------------------------------------
#
#  The following properties set the logging levels and log appender.  The
#  log4j.rootCategory variable defines the default log level and one or more
#  appenders.  For the console, use 'S'.  For the daily rolling file, use 'R'.
#  For an HTML formatted log, use 'H'.
#
#  To override the default (rootCategory) log level, define a property of the
#  form (see below for available values):
#
#        log4j.logger. =
#
#    Available logger names:
#      TODO
#
#    Possible Log Levels:
#      FATAL, ERROR, WARN, INFO, DEBUG
#
#------------------------------------------------------------------------------
log4j.rootCategory=INFO, S

log4j.logger.com.dappit.Dapper.parser=ERROR
log4j.logger.org.w3c.tidy=FATAL

#------------------------------------------------------------------------------
#
#  The following properties configure the console (stdout) appender.
#  See http://logging.apache.org/log4j/docs/api/index.html for details.
#
#------------------------------------------------------------------------------
log4j.appender.S = org.apache.log4j.ConsoleAppender
log4j.appender.S.layout = org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n

#show sql in ibatis
log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
#------------------------------------------------------------------------------
#
#  The following properties configure the Daily Rolling File appender.
#  See http://logging.apache.org/log4j/docs/api/index.html for details.
#
#------------------------------------------------------------------------------
log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File = logs/bensApps.log
log4j.appender.R.Append = true
log4j.appender.R.DatePattern = '.'yyy-MM-dd
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n

#------------------------------------------------------------------------------
#
#  The following properties configure the Rolling File appender in HTML.
#  See http://logging.apache.org/log4j/docs/api/index.html for details.
#
#------------------------------------------------------------------------------
log4j.appender.H = org.apache.log4j.RollingFileAppender
log4j.appender.H.File = logs/bensApps.html
log4j.appender.H.MaxFileSize = 100KB
log4j.appender.H.Append = false
log4j.appender.H.layout = org.apache.log4j.HTMLLayout

 其他的操作可以下载文件。

相关推荐

兔子压倒窝边草 / 0评论 2013-07-26
蜀川居 / 0评论 2014-07-16