一、 导包

18个必须的包

二、配置Spring MVC的web文件

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    id="WebApp_ID" version="3.1"> 
 
          
    <!-- 中文过滤器 --> 
    <filter> 
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name> 
        <url-pattern>/*</url-pattern> 
    </filter-mapping> 
     
    <!-- SpringMVC配置 --> 
    <servlet> 
        <servlet-name>springDispatcherServlet</servlet-name> 
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
        <init-param> 
            <param-name>contextConfigLocation</param-name> 
            <param-value>classpath:springmvc.xml</param-value> 
        </init-param> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
  
    <servlet-mapping> 
        <servlet-name>springDispatcherServlet</servlet-name> 
        <url-pattern>*.do</url-pattern> 
    </servlet-mapping> 
     
    <!-- Spring配置 --> 
    <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>classpath:spring-all.xml</param-value> 
    </context-param> 
    <listener> 
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
</web-app>

三、Spring整合MyBatis

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> 
    <!-- 引入外部文件 --> 
    <context:property-placeholder location="classpath:db.properties" /> 
    <!-- 配置扫描器 --> 
    <context:component-scan base-package="com.controller,com.util" /> 
    <!-- 数据源 --> 
    <bean id="dataSource" 
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="${jdbc.driver}"></property> 
        <property name="url" value="${jdbc.jdbcUrl}"></property> 
        <property name="username" value="${jdbc.username}"></property> 
        <property name="password" value="${jdbc.password}"></property> 
    </bean> 
    <!-- 配置mybatis核心,SqlSession --> 
    <bean id="SqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource"></property> 
        <property name="configLocation" value="classpath:mybatis-cfg.xml"></property> 
        <property name="mapperLocations" value="classpath:com/mapper/*.xml"></property> 
    </bean> 
    <!-- 配置数据库操作接口 --> 
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
        <property name="basePackage" value="com.dao"></property> 
        <property name="sqlSessionFactoryBeanName" value="SqlSession"></property> 
    </bean> 
    <!-- Spring的事务管理器 --> 
    <bean id="transactionManager" 
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
        <property name="dataSource" ref="dataSource"></property> 
    </bean> 
    <!-- 配置事务属性 --> 
    <tx:advice id="txadvice" transaction-manager="transactionManager"> 
        <tx:attributes> 
            <tx:method name="*" read-only="false" /> 
        </tx:attributes> 
    </tx:advice> 
    <!-- 配置切面 --> 
    <aop:config> 
        <aop:pointcut expression="execution(* com.dao.*.*(..))" 
            id="aoppointcut" /> 
        <aop:advisor advice-ref="txadvice" pointcut-ref="aoppointcut" /> 
    </aop:config> 
</beans>

classpath:db.properties文件

jdbc.driver=oracle.jdbc.OracleDriver 
jdbc.jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl 
jdbc.username=test1116 
jdbc.password=123456

mybatis-cfg.xml文件,只留别名配置

<?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> 
        <package name="com.model"/> 
    </typeAliases> 
  </configuration>

四、SpringMVC配置

<?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:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 
 
    <context:component-scan base-package="com.controller"></context:component-scan> 
    <!-- 转换编码为utf-8的bean --> 
    <bean 
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 
        <property name="messageConverters"> 
            <list> 
                <bean 
                    class="org.springframework.http.converter.StringHttpMessageConverter"> 
                    <property name="supportedMediaTypes"> 
                        <list> 
                            <value>text/plain;charset=UTF-8</value> 
                        </list> 
                    </property> 
                </bean> 
            </list> 
        </property> 
    </bean> 
     
   <mvc:annotation-driven/> 
    <bean 
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
        p:prefix="/" p:suffix=".jsp"> 
    </bean> 
</beans>

 

发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

[mysql] 归档工具pt-archiver,binlog格式由mixed变成row详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。