首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

ibtis跟spring整合的例子

2012-11-20 
ibtis和spring整合的例子spring其实已经提供了支持ibatis的模板,我们只需要在配置中配置好这些文件,就可以

ibtis和spring整合的例子
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" 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 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" resultresultparameterproperty="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" parameterkeyProperty="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" parameterprepend="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

热点排行