1,导入所需要的jar包(maven坐标)

<dependency>
      <groupId>org.spring work</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.9.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
    <dependency>
      <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.46</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.spring work/spring-jdbc -->
    <dependency>
      <groupId>org.spring work</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.25</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.12</version>
    </dependency>

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.10.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.47</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.3</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-core</artifactId>
      <version>4.0.6</version>
    </dependency>

    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
      <version>1.1.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.oracle/ojdbc6 -->
    <!-- 导入的是本地的oraclejar包-->
    <dependency>
      <groupId>com.oracle</groupId>
      <artifactId>ojdbc6</artifactId>
      <version>11.1.0.6.0</version>
      <scope>system</scope>
      <systemPath>${ dir}/web/WEB-INF/lib/ojdbc6-11.1.0.6.0.jar</systemPath>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.5</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.1</version>
    </dependency>

2,springmvc配置文件

springmvc-servlet.

<?  version=\"1.0\" encoding=\"UTF-8\"?>
<beans  ns=\"http://www.spring work.org/schema/beans\"
        ns:xsi=\"http://www.w3.org/2001/ Schema-instance\"
        ns:mvc=\"http://www.spring work.org/schema/mvc\"
        ns:context=\"http://www.spring work.org/schema/context\"
        ns:aop=\"http://www.spring work.org/schema/aop\"
       xsi:schemaLocation=\"http://www.spring work.org/schema/beans
		        http://www.spring work.org/schema/beans/spring-beans.xsd
		        http://www.spring work.org/schema/context
		        http://www.spring work.org/schema/context/spring-context.xsd
		        http://www.spring work.org/schema/mvc
		        http://www.spring work.org/schema/mvc/spring-mvc.xsd http://www.spring work.org/schema/aop http://www.spring work.org/schema/aop/spring-aop.xsd\">

    <!--视图解析器-->
    <bean class=\"org.spring work.web.servlet.view.InternalResourceViewResolver\">
        <!--指定视图的前缀和后缀,Controller返回的String类型与这里的前后缀拼接,构成返回的视图页面地址-->
        <property name=\"prefix\" value=\"/\"/>
        <property name=\"suffix\" value=\".jsp\"/>
    </bean>

    <!--开启注解扫描功能-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 处理请求返回json字符串的中文乱码问题 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults=\"true\">
            <!-- 解决Controller返回json中文乱码问题 -->
            <bean class=\"org.spring work.http.converter.StringHttpMessageConverter\">
                <!-- <property name=\"supportedMediaTypes\" value=\"text/html;charset=UTF-8\" /> -->
                <!-- <property name=\"supportedMediaTypes\" value=\"application/json;charset=UTF-8\" > -->
                <property name=\"supportedMediaTypes\">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>

            <!-- fastJson配置 -->
            <bean id=\"fastJsonHttpMessageConverter\" class=\"com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter\">
                <property name=\"supportedMediaTypes\">
                    <list>
                        <value>text/json;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>

        </mvc:message-converters>
    </mvc:annotation-driven>


    <!--定义注解扫描的包-->
    <context:component-scan  -package=\"com.qy.controller\"></context:component-scan>

    <!--处理静态资源-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>

    <!--将链接中的静态的访问路径映射为URL,常用于加载html、js、css、图片、视频等静态资源-->
    <!-- <mvc:resources mapping=\"/js/**\" location=\"/background/js/\"/>
     <mvc:resources mapping=\"/images/**\" location=\"/background/images/\"/>
     <mvc:resources mapping=\"/css/**\" location=\"/background/css/\"/>
     <mvc:resources mapping=\"/fonts/**\" location=\"/background/fonts/\"/>
     <mvc:resources mapping=\"/lib/**\" location=\"/background/lib/\"/>-->

    <!--登录拦截器-->
    <!-- <mvc:interceptors>
         <mvc:interceptor>
             &lt;!&ndash; 拦截所有URL中包含/user/的请求 &ndash;&gt;
             <mvc:mapping path=\"/**\"/>
             <mvc:exclude-mapping path=\"/**/fonts/**\"/>
             <mvc:exclude-mapping path=\"/**/css/**\"/>
             <mvc:exclude-mapping path=\"/**/js/**\"/>
             <mvc:exclude-mapping path=\"/**/images/**\"/>
             <mvc:exclude-mapping path=\"/**/lib/**\"/>
             <mvc:exclude-mapping path=\"/**/login.html\"/>
             &lt;!&ndash;<mvc:exclude-mapping path=\"/**/index.html\"/>&ndash;&gt;
             <mvc:exclude-mapping path=\"/user/toLogin\"/>
             <bean class=\"com.qy.controller.LoginInterceptor\"></bean>
         </mvc:interceptor>
     </mvc:interceptors>
 -->
    <!--springmvc定义好的,用来处理上传文件的类-->
    <bean id=\"multipartResolver\" class=\"org.spring work.web.multipart.commons.CommonsMultipartResolver\">
        <!-- 设置上传文件的最大尺寸为1MB -->
        <property name=\"maxUploadSize\">
            <value>1048576</value>
        </property>
    </bean>

</beans>

3,spring配置文件

applicationContext.

<?  version=\"1.0\" encoding=\"UTF-8\"?>
<beans  ns=\"http://www.spring work.org/schema/beans\"
        ns:xsi=\"http://www.w3.org/2001/ Schema-instance\"
        ns:aop=\"http://www.spring work.org/schema/aop\"
        ns:context=\"http://www.spring work.org/schema/context\"
        ns:tx=\"http://www.spring work.org/schema/tx\"
       xsi:schemaLocation=\"http://www.spring work.org/schema/beans
       http://www.spring work.org/schema/beans/spring-beans.xsd
       http://www.spring work.org/schema/aop
       http://www.spring work.org/schema/aop/spring-aop.xsd
       http://www.spring work.org/schema/context
       http://www.spring work.org/schema/context/spring-context.xsd
       http://www.spring work.org/schema/tx
       http://www.spring work.org/schema/tx/spring-tx.xsd\">

    <!--开启注解扫描功能-->
    <context:annotation-config></context:annotation-config>
    <!--定义注解扫描的包-->
    <context:component-scan  -package=\"com.qy.mapper\"></context:component-scan>
    <context:component-scan  -package=\"com.qy.service\"></context:component-scan>
    <context:component-scan  -package=\"com.qy.domain\"></context:component-scan>
    <!--开启aop自动代理模式-->
    <aop:aspectj-autoproxy proxy-target-class=\"true\"/>

    <!--从上下文环境中读取db.properties配置文件-->
    <context:property-placeholder location=\"classpath*:db.properties\"></context:property-placeholder>

    <bean id=\"dataSource\"  class=\"org.apache.commons.dbcp.BasicDataSource\">
        <property name=\"driverClassName\" value=\"${oracle.driverClassName}\"></property>
        <property name=\"url\" value=\"${oracle.url}\"></property>
        <property name=\"username\" value=\"${oracle.username}\"></property>
        <property name=\"password\" value=\"${oracle.password}\"></property>
        <property name=\"maxActive\" value=\"${oracle.maxActive}\"></property>
        <property name=\"initialSize\" value=\"${oracle.initialSize}\"></property>
    </bean>

    <!--通过mybatis和spring结合的类,创建sqlsessionFactory-->
    <bean id=\"sqlSessionFactory\" class=\"org.mybatis.spring.SqlSessionFactoryBean\">
        <property name=\"dataSource\" ref=\"dataSource\" />
        <!--指定myabtis配置文件位置-->
        <property name=\"configLocation\" value=\"classpath:mybatis-config. \" />
    </bean>
    <!-- scan for mappers and let them be autowired
    MapperScannerConfigurer Mybatis-Spring 会自动为我们注册Mapper对应的MapperFactoryBean对象-->
    <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
    <bean class=\"org.mybatis.spring.mapper.MapperScannerConfigurer\">
        <property name=\" Package\" value=\"com.qy.mapper\" />
    </bean>
    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id=\"transactionManager\" class=\"org.spring work.jdbc.datasource.DataSourceTransactionManager\">
        <property name=\"dataSource\" ref=\"dataSource\" />
    </bean>

    <!-- 配置事务属性 -->
    <tx:advice id=\"txAdvice\" transaction-manager=\"transactionManager\">
        <tx:attributes>
            <tx:method name=\"save*\"    propagation=\"REQUIRED\"/>
            <tx:method name=\"update*\"  propagation=\"REQUIRED\"/>
            <tx:method name=\"delete*\"  propagation=\"REQUIRED\"/>
            <tx:method name=\"*\" propagation=\"NOT_SUPPORTED\"></tx:method>
        </tx:attributes>
    </tx:advice>

    <!-- 配置事务切入点 -->
    <aop:config>
        <aop:pointcut  =\"execution(* com.qy.service.impl.*.*(..))\" id=\"pointCut\"/>
        <aop:advisor advice-ref=\"txAdvice\" pointcut-ref=\"pointCut\"/>
    </aop:config>

</beans>

 4,mybatis配置文件

mybatis-config.

<?  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>

    <settings>
        <!--是否使用缓存 开发中禁用-->
        <setting name=\"cacheEnabled\" value=\"false\"/>
        <!--下划线和驼峰式命名法的匹配  -->
        <setting name=\"mapUnderscoreToCamelCase\" value=\"true\"/>
        <!--使用启用懒加载机制  true启用-->
        <setting name=\"logImpl\" value=\"LOG4J2\"></setting>
        <setting name=\"lazyLoadingEnabled\" value=\"true\"/>
        <setting name=\"multipleResultSetsEnabled\" value=\"true\"/>
        <setting name=\"useColumnLabel\" value=\"true\"/>
        <setting name=\"useGeneratedKeys\" value=\"true\"/>
        <setting name=\"autoMappingBehavior\" value=\"PARTIAL\"/>
        <setting name=\"autoMappingUnknownColumnBehavior\" value=\"WARNING\"/>
        <setting name=\"defaultExecutorType\" value=\"SIMPLE\"/>
        <setting name=\"defaultStatementTimeout\" value=\"25\"/>
        <setting name=\"defaultFetchSize\" value=\"100\"/>
        <setting name=\"safeRowBoundsEnabled\" value=\"false\"/>
        <setting name=\"localCacheScope\" value=\"SESSION\"/>
        <setting name=\"jdbcTypeForNull\" value=\"OTHER\"/>
        <setting name=\"lazyLoadTriggerMethods\" value=\"equals,clone,hashCode,toString\"/>
    </settings>

    <typeAliases>
        <!--给类起别名-->
        <!--<typeAlias type=\"com.qy.domain.Question\" alias=\"Question\"></typeAlias>-->
        <!--给包中所有的类起别名 默认名字为类名 -->
        <package name=\"com.qy.domain\"></package>
    </typeAliases>

    <mappers>
        <!--引入mapper映射文件的位置-->
        <!--<mapper resource=\"com/qy/mapper/DeptMapper. \"/>-->
        <!-- <mapper resource=\"mapper/UserMapper. \"/>
         <mapper resource=\"com/qy/mapper/QuestionMapper. \"/>
         <mapper resource=\"com/qy/mapper/EmpMapper. \"/>-->
    </mappers>


</configuration>

 5.配置web.

<?  version=\"1.0\" encoding=\"UTF-8\"?>
<web-app  ns=\"http:// ns.jcp.org/ /ns/javaee\"
          ns:xsi=\"http://www.w3.org/2001/ Schema-instance\"
         xsi:schemaLocation=\"http:// ns.jcp.org/ /ns/javaee http:// ns.jcp.org/ /ns/javaee/web-app_3_1.xsd\"
         version=\"3.1\">

    <!--filter 过滤器 用来解决中文乱码问题-->
    <filter>
        <filter-name>characterEncoding</filter-name>
        <filter-class>org.spring work.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>default1</servlet-name>
        <servlet-class>org.spring work.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet. </param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>default1</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--配置spring-->
    <!-- 配置去哪裡加載spring的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext. </param-value>
    </context-param>
    <!-- 配置spring的監聽器,加載資源初始化文件 -->
    <listener>
        <listener-class>org.spring work.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

6,配置连接数据库文件db.properties

oracle.driverClassName=oracle.jdbc.OracleDriver
oracle.url=jdbc:oracle:thin:@localhost:1521:orcl
oracle.username=scott
oracle.password=123456
oracle.maxActive=100
oracle.initialSize=5

 

7,如果使用日志,可以再配置一个log4j.

<?  version=\"1.0\" encoding=\"UTF-8\"?>
<Configuration status=\"trace\">
    <Appenders>
        <Console name=\"Console\" target=\"SYSTEM_OUT\">
            <PatternLayout pattern=\"%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n\"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level=\"trace\">
            <AppenderRef ref=\"Console\"/>
        </Root>
        <Logger name=\"com.qy.mapper\" level=\"trace\" additivity=\"false\">
            <AppenderRef ref=\"Console\"/>
        </Logger>

    </Loggers>
</Configuration>

配置完成

 

收藏 打印