Spring3+MyBatis 整合多方法使用

springlin2011發表於2012-03-10
[color=blue]
web.xml配置檔案
[/color]

<?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">
<display-name></display-name>
<welcome-file-list>
<welcome-file>/WEB-INF/view/welcome.jsp</welcome-file>
</welcome-file-list>

<!-- 配置載入Spring配置檔案路徑 -->
<!-- <context-param>-->
<!-- <param-name>contextConfigLocation</param-name>-->
<!-- <param-value>/WEB-INF/applicationContext.xml</param-value>-->
<!-- </context-param>-->
<!--
預設的spring配置檔案是在WEB-INF下的applicationContext.xml
Spring 容器啟動監聽器
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 配置字元過濾器 -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value> <!-- 強制進行轉碼 -->
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配置 Spring view分發器 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始配置化檔案,前面contextConfigLocation看情況二選一 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml,
/WEB-INF/config/dispatcher-servlet.xml
</param-value>
</init-param>
<!-- 啟動載入一次 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!-- 這裡可以用 / 但不能用 /* ,攔截了所有請求會導致靜態資源無法訪問 -->
<url-pattern>/</url-pattern>
</servlet-mapping>


<!-- 設定session超時 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>

<!-- 配置異常頁面 -->
<error-page>
<error-code>404</error-code>
<location>/error_page.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error_page.jsp</location>
</error-page>

<!-- 配置要使用到的標籤 -->
<jsp-config>
<taglib>
<taglib-uri>http://www.springframework.org/tags</taglib-uri>
<taglib-location>/WEB-INF/tld/spring.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/tld/c.tld</taglib-location>
</taglib>
</jsp-config>

</web-app>


[color=blue]
applicationContext.xml Spring配置檔案
[/color]

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" default-autowire="byName"
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.0.xsd">


<!-- 載入讀取properties配置檔案引數 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/jdbc.properties</value>
</property>
</bean>
<!-- 匯入屬性配置檔案 -->
<!--< context:property-placeholder location = "classpath:jdbc.properties" /> -->

<!-- 配置資料庫連線池,使用dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.databaseurl}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- 連線池啟動時的初始值 -->
<property name="initialSize" value="1" />
<!-- 連線池的最大值 -->
<property name="maxActive" value="500" />
<!-- 最大空閒值.當經過一個高峰時間後,
連線池可以慢慢將已經用不到的連線慢慢釋放一部分,一直減少到maxIdle為止 -->
<property name="maxIdle" value="2" />
<!-- 最小空閒值.當空閒的連線數少於閥值時,
連線池就會預申請去一些連線,以免洪峰來時來不及申請 -->
<property name="minIdle" value="1" />
</bean>

<!-- MyBatis定義資料來源,同意載入配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="/WEB-INF/config/mybatis_config.xml" />
<property name="dataSource" ref="dataSource" />
<!-- <property name="mapperLocations" value="classpath*:mappers-*.xml" /> -->
</bean>

<bean id="userInfoDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.skillmuster.core.dao.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 可配置多個這樣的介面對映 -->
<!--<bean id="userInfoDao" class="org.mybatis.spring.mapper.MapperFactoryBean">-->
<!-- <property name="mapperInterface" value="com.skillmuster.core.dao.UserMapper2" />-->
<!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" />-->
<!--</bean>-->

<!-- MyBatis 對映配置,如果介面和mybatis對映檔案在同一路徑下且命名相同,可採用自動掃描包的方式來註冊各種Mapper -->
<!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">-->
<!-- <property name="basePackage" value="com.skillmuster.core.dao"/>-->
<!-- markerInterface介面的子介面都參與到這個掃描 -->
<!-- <property name="markerInterface" value="com.skillmuster.cor.dao.UserMapper" /> -->
<!--</bean>-->

<!-- 配置事物管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<!-- 申明事務通知 -->
<tx:advice id="txAdivice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*"
isolation="READ_COMMITTED"
propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="query*"
propagation="NOT_SUPPORTED"
isolation="DEFAULT"
rollback-for="java.io.IOException"
no-rollback-for="java.lang.ArithmeticException,java.lang.*"
timeout="30"
read-only="true" />
</tx:attributes>
</tx:advice>

<!-- 將通知和切入點聯接 -->
<aop:config>
<!-- 分開配置 -->
<!-- <aop:pointcut expression="execution(* com.iss.is.service.impl..*.*(..))" id="allServiceMethod" /> -->
<!-- <aop:advisor advice-ref="txAdivice" pointcut-ref="allServiceMethod" />-->
<!-- 一起配置 -->
<!-- <aop:advisor advice-ref="txAdivice" pointcut="exection(* com.skillmuster.core.service.*ServiceImpl.*(..))" />-->
</aop:config>

</beans>


[color=blue]
dispatcher-servlet Spring MVC配置檔案
[/color]

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-autowire="byName">
<!-- default-autowire="byName",約定優於配置 -->

<!-- @Controller 請求對映註解掃描,必須加上這個,不然請求controller時會出現no mapping url錯誤-->
<mvc:annotation-driven />

<!-- 配置靜態資源,直接對映到對應的資料夾,不被DispatcherServlet處理,3.04新增功能,需要重新設定spring-mvc-3.0.xsd -->
<mvc:resources mapping="/img/**" location="/img/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/html/**" location="/html/"/>
<!--
①:註解掃描,對包中的所有類進行掃描,以完成Bean建立和自動依賴注入的功能
-->
<context:component-scan base-package="com.skillmuster.core" />

<!--
②:啟動Spring MVC的註解功能,完成請求和註解POJO的對映,//新增攔截器,類級別的處理器對映
-->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> -->
<!-- <property name="interceptors">-->
<!-- <list>-->
<!-- <bean class="com.fsj.spring.util.MyHandlerInterceptor"/>-->
<!-- </list>-->
<!-- </property>-->
<!-- <property name="order"><value>1</value></property> -->
<!-- </bean> -->

<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> -->
<property name="cacheSeconds" value="0" />
<!-- 配置一下對json資料的轉換 -->
<!-- <property name="messageConverters">-->
<!-- <list>-->
<!-- <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>-->
<!-- </list>-->
<!-- </property>-->
<!-- </bean> -->

<!--
③:對模型檢視名稱的解析,即在模型檢視名稱新增前字尾
InternalResourceViewResolver預設的就是JstlView所以這裡就不用配置viewClass了
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- p:prefix="/WEB-INF/view/" p:suffix=".jsp" />-->
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>


<!-- spring2.0的配置處理方式 -->
<!-- 處理器對映,它將收到的HTTP請求對映到bean的名字上 -->
<!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">-->
<!-- <property name="order"><value>1</value></property>-->
<!-- </bean>-->
<!-- 檢視解析器 -->
<!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
<!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />-->
<!-- <property name="contentType">-->
<!-- <value>text/html;charset=UTF-8</value>-->
<!-- </property>-->
<!-- 頁面路徑 -->
<!-- <property name="prefix" value="/WEB-INF/pages/" />-->
<!-- <property name="suffix" value=".jsp" />-->
<!-- </bean> -->
<!-- Controller配置 -->
<!-- <bean name="/saveStudent.do" class="com.iss.is.web.controller.demo.student.SaveStudentController">-->
<!-- <property name="studentService">-->
<!-- <ref local="studentService"/>-->
<!-- </property>-->
<!-- <property name="commandClass">-->
<!-- <value>com.iss.is.dto.StudentDTO</value>-->
<!-- </property>-->
<!-- -->
<!-- </bean> -->

</beans>


[color=blue]
jdbc.properties
[/color]

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.databaseurl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123



[color=blue]mybatis-config.xml MyBatis總配置檔案[/color]

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<!-- 實體類,簡稱 -->
<typeAliases>
<typeAlias alias="userinfo" type="com.skillmuster.core.pojo.User" />
</typeAliases>
<!-- 實體介面對映資源 -->
<mappers>
<mapper resource="com/skillmuster/core/dao/UserMapper.xml"/>
</mappers>

</configuration>



[color=blue]UserMapper.xml [/color]

<?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="com.skillmuster.core.dao.UserMapper">

<resultMap type="userinfo" id="userResultMap">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="password" column="password" />
</resultMap>

<insert id="insertUser" parameterType="userinfo">
<![CDATA[
insert into user(username,password) values(#{username},#{password})
]]>
</insert>

<!-- mybsits_config中配置的alias類別名,也可直接配置resultType為類路勁 -->
<select id="getUser" resultType="userinfo">
select * from user
</select>

<!-- 當使用該Mybatis與Spring整合的時候,該檔案必須和相應的Mapper介面檔案同名,並在同一路徑下 -->
</mapper>

[color=blue]UserMapper 對映介面[/color]

public interface UserMapper {
void insertUser(User user);
List<User> getUser();
}

相關文章