关闭

Maven下SSM框架搭建-下

标签: javaspring框架mavenspring mvc
183人阅读 评论(0) 收藏 举报
分类:

汉语版(E-version)

具体内容如下(Here are the contens):

【6】配置Spring Mvc(config spring-mvc.xml)

<?xml version="1.0" en-coding="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:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:context="http://www.springframework.org/schema/context"   
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">  
     
    <!-- 自动扫描该包,使SpringMVC认为包下用了controller注解的类是控制器 -->  
    <context:component-scan base-package="edu.tostronger.scu"/>   
      
    <!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
    <mvc:annotation-driven />             
    
    <!-- 配置fastjson转换器 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"></bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    
    <!-- 启动AspectJ支持 -->   
    <aop:aspectj-autoproxy proxy-target-class="true">  
        <aop:include name="controllerAspect"/>  
    </aop:aspectj-autoproxy>  
    
    <!-- 静态资源配置 -->
    <!-- 可以通过URL -->
    <mvc:resources location="/img/" mapping="/img/**"/>  
    <mvc:resources location="/js/" mapping="/js/**"/>  
    <mvc:resources location="/css/" mapping="/css/**"/>  
    <mvc:resources location="/fonts/" mapping="/fonts/**"/>
    <mvc:resources location="/html/" mapping="/html/**"/>
  
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀   -->
<!-- 要保证这个视图解析具有最高优先级,数字越大,优先级越高 -->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/html/"/>   
        <property name="suffix" value=".html" />
        <property name="order" value="2"/>
    </bean>
    
    <!-- 配置velocity渲染-->
    <bean id="velocityConfig"
        class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value ="/WEB-INF/velocity/templates"/>
        <property name="configLocation" value="classpath:velocity.properties" />
        <property name="velocityProperties">    
         <props>    
             <prop key="input.encoding">UTF-8</prop>    
             <prop key="output.encoding">UTF-8</prop>      
          </props>    
      </property>  
    </bean>


<!-- 解析velocity渲染视图-->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="suffix" value=".vm" />
        <property name="contentType" value="text/html;charset=utf-8" />
        <property name="exposeSpringMacroHelpers" value="true"/> 
<property name="exposeRequestAttributes" value="true"/> 
<property name="exposeSessionAttributes" value="true"/> 
<property name="allowSessionOverride" value="true"/> 
<property name="allowRequestOverride" value="true"/>
<property name="order" value="1" /> 
    </bean> 
</beans>


【7】配置velocity属性

velocimacro.permissions.allow.inline=true  
velocimacro.permissions.allow.inline.to.replace.global=true  
velocimacro.permissions.allow.inline.local.scope=true  
resource.loader=webapp, class  
class.resource.loader.description=Velocity Classpath Resource Loader  
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader  
webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoader  
webapp.resource.loader.path=/WEB-INF/velocity/templates/
webapp.resource.loader.cache=false


【8】配置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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">


<!-- 引入属性文件,获取数据库配置 -->
<context:property-placeholder location="classpath:jdbc.properties" />


<!-- 自动扫描,扫描到带注解的包自动装载到Spring-BEAN里 -->
<context:component-scan base-package="edu.tostronger.scu.service" />


<!-- 配置数据源:这里选择是c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 基本的属性配置:驱动 -->
<property name="driverClass" value="${jdbc.driver}" />
<!-- 基本的属性配置:路径 -->
<property name="jdbcUrl" value="${jdbc.url}" />
<!-- 基本的属性配置:用户名 -->
<property name="user" value="${jdbc.username}" />
<!-- 基本的属性配置:密码 -->
<property name="password" value="${jdbc.password}" />
<!-- 指定连接数据库连接池的最小连接数 -->
<property name="minPoolSize" value="10" />
<!-- 指定连接数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="30" />
<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
<property name="maxIdleTime" value="1800" />
<property name="acquireIncrement" value="2" />
<property name="maxStatements" value="0" />
<!-- 指定连接数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="2" />
<property name="idleConnectionTestPeriod" value="1800" />
<property name="acquireRetryAttempts" value="30" />
<property name="breakAfterAcquireFailure" value="true" />
<property name="testConnectionOnCheckout" value="false" />
</bean>


<!-- myBatis配置,加载所有的mybatis-*.xml文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 扫描rescources/mybatis下的文件 -->
<property name="configLocation" value="classpath:mybatis.xml" />
<property name="mapperLocations"
value="classpath:mybatis/*.xml" />
</bean>


<!-- mybatis与spring配置,自动查找mapper文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="edu.tostronger.scu.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>


<!-- 配置事务管理,以便通过注解提交事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>


<!-- 注解提交事务 -->
<!-- 声明式的事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />

<!--需要回滚的方法-->
<tx:advice transaction-manager="transactionManager" id="serviceAdvice">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" read-only="false"/>
     <tx:method name="del*" propagation="REQUIRED" read-only="false"/>
     <tx:method name="update*" propagation="REQUIRED" read-only="false"/>
     <tx:method name="add*" propagation="REQUIRED" read-only="false"/>
     <tx:method name="find*" propagation="REQUIRED" read-only="false"/>
     <tx:method name="get*" propagation="REQUIRED" read-only="false"/>
     <tx:method name="apply*" propagation="REQUIRED" read-only="false"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>

<!-- 需要进行事务管理 -->
    <aop:config proxy-target-class="true">
        <aop:pointcut id="servicePointcut"
            expression="execution(* edu.tostronger.scu.implement.*Implement.*(..))" />            
        <aop:advisor pointcut-ref="servicePointcut" advice-ref="serviceAdvice" />
    </aop:config>
</beans>  

【9】配置mybatis.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>
         <typeAlias alias="User" type="edu.tostronger.scu.entity.User"/>
    </typeAliases>
   
</configuration>
【10】构建Java下的包与mybatis文件夹下的文件
<?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="edu.tostronger.scu.mapper.UserMapper">


<resultMap type="edu.tostronger.scu.entity.User" id="BaseResultMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
</resultMap>


</mapper>


【11】配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">


<!-- web.xml文件中需要配置的信息 -->


<!-- 0、WEB_APP显示名 -->
<display-name>Archetype Created Web Application</display-name>


<!-- 1、防止乱码的过滤器 -->
<filter>
<filter-name>encodingFilter</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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>


<!-- 2、设定监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 3、防止Spring内存溢出监听器 -->  
    <listener>  
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
    </listener> 


<!-- 4、配置请求分派器 -->
<servlet>
<description>spring mvc servlet</description>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>spring mvc config</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>


<!-- 5、配置Spring-mybatis的配置文件 -->
<context-param>
<description>spring mybatis config</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>


<!-- 6、首页,欢迎页面 -->
<welcome-file-list>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>


<!-- 7、session登出时间 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>

----------------------------------------------------------------------------------------------------------------------------------------------

git分享:https://gitee.com/tostronger/

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1647次
    • 积分:155
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类