環境:spring3.1.1+mybatis3.2.8+mybatis-spring1.2.3 html
網絡上關於這個架構的搭建文章,實在是太多了,本文是對於本人初次搭建時的一些注意點的整理。 spring
主要是一些配置文件的內容和架構的目錄。 sql
0. project 目錄 數據庫
1. spring-resources.xml
這個文件是用來完成spring和mybatis的整合的xml。注意properties文件的讀入方式。 express
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd" default-autowire="byName" default-lazy-init="false"> <!-- 自動掃描組件,須要把controller去掉,不然影響事務管理 --> <context:component-scan base-package="org.com.mars"> <context:exclude-filter type="regex" expression="org.com.mars.controller.*" /> </context:component-scan> <!-- 引入jdbc配置文件 --> <context:property-placeholder location="classpath:prop/jdbc.properties" /> <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 聲明式事務管理 --> <aop:config> <aop:advisor pointcut="execution(* org.com.mars.service..*.*(..))" advice-ref="myAdvice" /> </aop:config> <tx:advice id="myAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="query*" propagation="SUPPORTS" read-only="true" /> <tx:method name="list*" propagation="SUPPORTS" read-only="true" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <!-- 建立SqlSessionFactory,同時指定數據源 --> <bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自動掃描entity目錄, 省掉Configuration.xml裏的手工配置 --> <property name="mapperLocations" value="classpath*:maper/*.map.xml" /> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="org.com.mars.dao" /> <property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory" /> </bean> <!-- 可經過註解控制事務 --> <tx:annotation-driven /> <!-- 數據庫鏈接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- Connection Info --> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- Connection Pooling Info --> <property name="maxActive" value="${dbcp.maxActive}" /> <property name="maxIdle" value="${dbcp.maxIdle}" /> <property name="defaultAutoCommit" value="false" /> <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="3600000" /> <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="3600000" /> </bean> </beans>
2. mapper.xml apache
該xml 如何使用配置,請參看spring-resources.xml,注意mapper.xml中 namespace的配置。 網絡
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.com.mars.dao.test.UserDao"> <!-- mapping --> <resultMap id="User_t" type="org.com.mars.pojo.test.User"> <result property="userId" column="user_id" jdbcType="VARCHAR" /> <result property="userName" column="user_name" jdbcType="VARCHAR" /> </resultMap> <sql id="columns"> user_id, user_name </sql> <!-- 根據ID查詢用戶信息 --> <select id="queryUserById" resultMap="User_t"> SELECT <include refid="columns" /> FROM user_t WHERE user_id = #{userId} </select> </mapper>
springmvc + mybatis整合詳細,及遇到的問題請參看如下資料: mybatis
參考資料: 架構
http://www.itnose.net/detail/6074493.html mvc
http://my.oschina.net/wangbiglei/blog/489583
http://my.oschina.net/wangbiglei/blog/489604
感謝熱心的前輩!!本文若有不正,請指出。
以上。