关于Spring3.x + MyBatis 3.x架构的一点经验

该文章仅为记录自己第一次构建Spring3.x 和 MyBatis3.x的一些经验,为了给很多刚刚接触构建该类项目的朋友借鉴,老鸟请绕道,谢谢!

1. 准备工作:

      a. spring-framework-3.0.6.RELEASE-with-docs.zip

         spring的包,必须的,这个你懂的,有可能还需要下载一些依赖包,支持Annotation。

      b. mysql-connector-java-5.1.13-bin.jar

         mysql的连接包,若是其他数据库,请下载相应包

      c. commons-logging-1.1.1-bin.zip

         spring框架启动时需要的包,若没有会报错。

      d. mybatis-3.0.6-bundle.zip

         Mybatis核心包,必须的,这个你也懂的。

      e. mybatis-spring-1.0.3-SNAPSHOT.jar

         Mybatis 跟 spring结合需要的一个依赖包。

      f. Commons-dbcp.jar Commons-pool.jar

         数据库链接需要的包

      g. jakarta-log4j-1.2.8.zip

         Log4j日志包,日志必备。O啦!

2. 创建一个普通的Web Project

      我创建的时候使用了分包形式来做,这样以后便于更好的管理项目,目录结构如下

     

project结构
--project
   --src/java
       --com.projectname.xxx
          --javaclass
--src/config 
   --config
      --configfile

3. 将准备好的所有包导入到该project下

      该步骤若是用Myeclipse的话,你可以直接先用ME的Spring插件直接加入Spring Capability,然后再导入其他的包,或者直接解压spring-framework-3.0.6.RELEASE-with-docs.zip 出来,找到dist里面的spring包来导入。建议使用spring Capability来操作,因为有些依赖包Me在此都提供了。

4. 创建数据库链接信息properties文件

      在config 包下创建。注意,所有在src/config --config包下的文件,访问的路径都是WEB-INF/classes/config

   jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
user=root
password=root

5. 创建spring configuration 文件 ApplicationContext.xml  

        该文件因为便于管理,我们也创建到src/config --config包下。

    ApplicationContext.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:context="http://www.springframework.org/schema/context"
	  xmlns:aop="http://www.springframework.org/schema/aop"
	  xmlns:tx="http://www.springframework.org/schema/tx"
	  xmlns:mvc="http://www.springframework.org/schema/mvc"
	  xsi:schemaLocation="http://www.springframework.org/schema/beans
	  					  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	  					  http://www.springframework.org/schema/context
	  					  http://www.springframework.org/schema/context/spring-context-2.5.xsd
	  					  http://www.springframework.org/schema/aop
	  					  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	  					  http://www.springframework.org/schema/tx
	  					  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
	  					  http://www.springframework.org/schema/mvc
	  					  http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	<!-- 将数据源参数properties文件交给spring来管理 -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations" value="WEB-INF/classes/config/jdbc.properties" />
	</bean>
              <!-- 配置dataSource 数据源及参数 -->
	<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="${user}" />
		  <property name="password" value="${password}" />
		  
		  <property name="initialSize" value="60" />
		  <property name="maxActive" value="100" />
		  <property name="maxIdle" value="50" />
		  <property name="minIdle" value="10" />
	</bean>
              <!-- MyBatis在spring中Bean的配置 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">   
		<property name="configLocation" value="WEB-INF/classes/config/SqlMapConfig.xml" />   
		<property name="dataSource" ref="dataSource" />  
	</bean>  
	<bean id="session" class="org.mybatis.spring.SqlSessionTemplate">   
		<constructor-arg index="0" ref="sqlSessionFactory" />  
	</bean>
	<!-- 具体的Spring管理的Bean -->
 	<bean id="testDao" class="com.orderme.dao.TestDaoImpl">
		<property name="session" ref="session"/>
	</bean>
</beans> 

6. 在web.xml中添加mybatis相关配置

  

   web.xml

  

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>WEB-INF/classes/config/ApplicationContext.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- Log4j 配置 -->  
  <context-param>   
  	<param-name>log4jRefreshInterval</param-name>   
  	<param-value>60000</param-value>  
  </context-param>  
  <listener>   
  	<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  
  <!-- 访问测试的时候添加的servlet配置 -->
  <servlet>
  	<servlet-name>testServlet</servlet-name>
  	<servlet-class>com.org.tang.servlet.test.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>testServlet</servlet-name>
  	<url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

7.  为MyBatis创建配置文件SqlMapConfig.xml

        创建地址也可以是 src/config --config这个文件夹下面

  

   SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//ibatis.apache.org//DTD Config 3.0/EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
	<typeAliases>
	<!-- 为每一个vo类取一个别名,后面用到的时候可以直接用该vo的别名而不用写全地址 -->
		<typeAlias type="com.org.tang.vo.TestVo" alias="TestVo" />
	</typeAliases>
              <!-- Mapping 具体的每个Object -->
	<mappers>
		<mapper resource="com/org/tang/dao/mapper/TestVo.xml" />
	</mappers>
</configuration>

 8.  配置具体的数据模型和它所对应的 Mapping xml

             注意,以下的数据模型对应的mapping配置只配置了他的插入方法和删除方法。数据模型TestVo就是个简单的javaBean,在此就不贴出代码了。

      TestVo.xml

猜你喜欢

转载自goalietang.iteye.com/blog/1722745