spring 与 Ibatis的结合开发

前几天自己利用下班的时间写了一下spring与IBatis的工程配置,整个工程分为三层:DAO, SERVICE, CONTROLLER。 相信很多人都很熟悉。下面我把我整个工程的配置文件分享给大家,作为处级搭建工程的模版吧。

下图是整个工程的结构图:



接下来我们分别介绍每个配置文件:
1. web.xml 

web.xml主要是用于配置spring MVC的分发器以及listener(监听器)

<?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>PregnantProject</display-name>
	
	<listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
            <param-name>contextConfigLocation</param-name>
			<param-value>
			WEB-INF/applicationContext.xml,
			WEB-INF/controller_config/controller-config.xml
			</param-value>
         </init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	
</web-app>



2. applicationContext.xml

在applicationContext中主要配置了spring的事务管理器, 视图解析器, 还有就是数据库连接设置信息。数据库的设置信息也可以配置在database.properties当中,在这里我省去了这一步骤。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

 <!--事务管理DataSourceTransactionManager-->
<bean id="txManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<!-- 视图解析器 -->
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView"></property>
    <property name="prefix" value="/WEB-INF/views/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>


<!-- 异常解析器 -->
<bean id="simpleMappingExceptionResolver"
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop
                key="org.springframework.web.multipart.MaxUploadSizeExceededException">common/fileerror</prop>
        </props>
    </property>
</bean>


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
	<property name="driverClassName">
		<value>com.mysql.jdbc.Driver</value>
	</property>
	<property name="username">
		<value>root</value>
	</property>
	<property name="password">
		<value>12456</value>
	</property>
	<property name="url">
		<value>jdbc:mysql://127.0.0.1:3306/pregnant</value>
	</property>	
</bean>

</beans>


sqlMapConfig.xml
这个文件很简单,就是ibatis的数据库表操作的配置。详细内容如下:
<?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>
  <sqlMap resource="com/pregnant/config/bean/UserBaseinfo.xml"/> 
</sqlMapConfig>


UserBaseInfo.xml对应的是ibatis对于数据库model的映射以及操作处理,在这里也把配置操作贴出来给大家:
UserBaseInfo.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
        PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
        "http://www.ibatis.com/dtd/sql-map-2.dtd">

	<sqlMap namespace="UserBase">
		
		<typeAlias alias="UserBaseInfo" type="com.pregnant.model.UserBaseInfo"/>
	
		<select id="getAllPerson" resultClass="UserBaseInfo">	
			SELECT * FROM user_baseInfo;
		</select>
		
		<insert id="insertOneUser" parameterClass="java.util.Map">
			insert into user_baseInfo values(#userName#,#userEmail#,#userPhone#,#userPasswd#)
		</insert>
		
		<select id="getUserByEmail" resultClass="UserBaseInfo">
		    select * from user_baseInfo where userEmail = #email#
		</select>
	
	</sqlMap>


上面也介绍完了ibatis的接触配置,那么下面我们将介绍工程各个层级之间的配置:

userbaseinfo_DAO.xml

在这里我们配置了SqlMapClientFactoryBean, 它完成了spring 与ibatis的结合。这里不多讲了,大家自己看配置文件一下就明白了。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<import resource="../applicationContext.xml"/>

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
	<property name="dataSource">
		<ref bean="dataSource"/>
	</property>
	
  <property name="configLocation">
     <value>WEB-INF/dao_config/sqlMapConfig.xml</value>
  </property>
</bean> 

<bean id="userBaseInfoDao" class="com.pregnant.daoImpl.UserBaseInfoDaoimpl">
	<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>

</beans>


配置到这里我们应该吧DAO层的一个类的代码贴出来,让大家看看如何调用spring提供的ORM来操作数据库。从配置文件中已经可以看出就是通过sqlmapclient来进行。

package com.pregnant.daoImpl;

import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.pregnant.dao.IUserBaseInfoDao;
import com.pregnant.model.UserBaseInfo;

public class UserBaseInfoDaoimpl extends SqlMapClientDaoSupport implements IUserBaseInfoDao{

	public List<UserBaseInfo> getAllUser(){
		List<UserBaseInfo> list = getSqlMapClientTemplate().queryForList("UserBase.getAllPerson");
		return list;
	}

	@Override
	public UserBaseInfo getUserByEmail(String email) {
		UserBaseInfo oneUser = (UserBaseInfo) getSqlMapClientTemplate().queryForObject(email);	
		return oneUser;
	}

	@Override
	public void insertUserbaseInfo(Map info) {
		getSqlMapClientTemplate().insert("insertOneUser", info);
		return;
	}	
}



接下来的SERVCE层和controller实际上就是利用spring的IOC来注入前一层次的类进行业务操作。这里不做过多介绍只是把代码贴给大家:


one-service.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<import resource="../dao_config/userbaseinfo_dao.xml"/>

<bean id="userBaseInfoService" class="com.pregnant.serviceImpl.UserBaseInfoServiceImpl">
	<property name="userBaseInfoDao">
		<ref bean="userBaseInfoDao"/>	
    </property>
</bean>

</beans>


controller.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<import resource="../service_config/one_service.xml"/>


<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                 <props>
                       <prop key="userBaseHandle.do">userBaseInfoController</prop>
                 </props>
            </property>
</bean>

<bean id="paraMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
           <property name="paramName" value="whichMethod"/>
</bean>

<bean id="userBaseInfoController" class="com.pregnant.controller.UserBaseInfoController">
      <property name="methodNameResolver">
            <ref bean="paraMethodResolver"/>
      </property>
      
      <property name="userBaseInfoService">
			<ref bean="userBaseInfoService"/>	
      </property>
</bean>


在controller层为了减少java类的生成,我们利用了spring方法多处理器来进行方法的映射。

值此整个工程配置文件结束。希望对大家有点帮助。

猜你喜欢

转载自tianzongqi.iteye.com/blog/1688434