Spring和Ibatis整合的项目,详细篇

来源:互联网 发布:疯狂联盟龙数据 编辑:程序博客网 时间:2024/06/05 15:10

用到的配置文件,基本如下:

SqlMapConfig.xml,Bean.xml

Spring.xml

web.xml

以上为所使用的xml文件,当然还可以在xml里载入外部的properties文件了

既然和Spring整合了,Ibatis的SqlMapConfig.xml文件中仅有的内容就是加载Bean.xml了,所有的关于数据库的东西全部交给Spring.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="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:db.properties</value></list></property></bean><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="${driver}" /><property name="url" value="${url}" /><property name="username" value="${username}" /><property name="password" value="${password}" /><property name="maxActive" value="${maxActive}" /><property name="maxIdle" value="${maxIdle}" /><property name="maxWait" value="${maxWait}" /><property name="defaultAutoCommit" value="${defaultAutoCommit}" /><property name="logAbandoned" value="${logAbandoned}"></property></bean><!-- SqlMap setup for iBATIS Database Layer --><bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"><property name="configLocation"><value>classpath:SqlMapConfig.xml</value></property><property name="dataSource" ref="dataSource" /></bean><bean id="mail" class="cn.cisco.com.mail.Mail" /><bean id="monDao" class="cn.cisco.com.dao.impl.MonDAOImpl"><property name="sqlMapClient" ref="sqlMapClient" /><property name="mail" ref="mail" /></bean><bean id="monService" class="cn.cisco.com.service.impl.MonServiceImpl"><property name="monDao" ref="monDao" /><property name="transactionManager"><ref bean="transactionManager" /></property></bean><bean id="monitor" class="cn.cisco.com.main.Monitor"><property name="monService" ref="monService" /></bean><bean id="startListener" class="cn.cisco.com.main.StartListener"><property name="monitor" ref="monitor" /></bean><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource"><ref bean="dataSource" /></property></bean></beans>

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMapConfigPUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN""http://www.ibatis.com/dtd/sql-map-config-2.dtd"><sqlMapConfig><sqlMap resource="cn/cisco/com/bean/monitor.xml" /></sqlMapConfig>

Bean.xml文件就是全部的sql语句了,不多说了


<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>monitor</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:Spring.xml</param-value>  </context-param>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  </web-app>

OK了