SSM配置

xiaoxiao2021-02-27  274

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="true"> <!--修改servlet版本为3.1--> <!-- 配置DispatcherServlet --> <servlet> <servlet-name>seckill-dispather</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置SpringMVC需要加载的配置文件 --> <!-- spring-dao.xml,spring-service.xml,spring-web.xl --> <!-- Mybatis->Spring->SpringMVC --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>seckill-dispather</servlet-name> <!-- 默认匹配所有的请求 --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

spring-web.xml

<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置SpringMVC --> <!-- 1.开启SpringMVC注解模式 --> <!-- 简化配置: (1).自动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter (2).提供一系列:数据绑定,数字和日期的format @NumberFormat,@DataTimeFormat, xml,json默认读写支持 --> <mvc:annotation-driven/> <!-- servlet-mapping 映射路径:"/" --> <!-- 2.静态资源默认servlet配置 (1).加入对静态资源的处理:js/gif/png (2).允许使用"/"做整体映射 --> <mvc:default-servlet-handler/> <!-- 配置拦截器 SeckillInterceptor 只要实现HandlerInterceptor,并复写她的方法就行 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/seckill/list"/> <bean class="org.seckill.interceptor.SeckillInterceptor"/> </mvc:interceptor> </mvc:interceptors> <!-- 3.配置jsp 显示ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 4.扫描web相关的bean --> <context:component-scan base-package="org.seckill.web"/> </beans>

spring-service.xml

<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 扫描service 包下所有注解的类型 --> <context:component-scan base-package="org.seckill.service"/> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置基于注解的声明式事务 默认使用注解来管理事务行为 使用注解控制事务的优点: 1.开发团队达成一致约定,明确标注事务方法的编程风格. 2.保证事务方法的执行时间尽可能短,不要穿插其他网络操作RPC/HTTP请求或者剥离到事务方法外部.保证实务操作干净清晰. 3.不是所有的方法都需要事务.如只有一条修改操作的,只读操作不需要事务控制。(mysql行级锁) --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>

spring-dao.xml

<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="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.xsd"> <!-- 配置整合Mybatis过程 --> <!-- 1.配置数据库相关参数 properties的属性: ${url} --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 2.数据库连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 配置连接池属性 --> <property name="driverClass" value="${driver}"/> <property name="jdbcUrl" value="${url}"/> <property name="user" value="${user}"/> <property name="password" value="${password}"/> <!-- c3p0连接池私有属性 --> <property name="maxPoolSize" value="30"/> <property name="minPoolSize" value="10"/> <!-- 关闭连接后不自动commit --> <property name="autoCommitOnClose" value="false"/> <!-- 获取连接超时时间 --> <property name="checkoutTimeout" value="1000"/> <!-- 当连接失败重试次数 --> <property name="acquireRetryAttempts" value="2"/> </bean> <!-- 约定大于配置 --> <!-- 3.配置SqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource"/> <!-- 配置Mybatis全局配置文件:mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!-- 扫描entity包 使用别名 (value间使用分号分隔)--> <property name="typeAliasesPackage" value="org.seckill.entity"/> <!-- 扫描SQL配置文件:mapper需要的xml文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!-- 配置扫描DAO接口包,动态实现DAO接口,注入到Spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 给出扫描DAO接口包 --> <property name="basePackage" value="org.seckill.dao"/> </bean> <bean id="redisDao" class="org.seckill.dao.cache.RedisDao"> <!--建议放在配置文件中--> <constructor-arg index="0" value="localhost"></constructor-arg> <constructor-arg index="1" value="6379"></constructor-arg> </bean> </beans>

SeckillController

//ajax json /** * @responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后, * 写入到response对象的body区,通常用来返回JSON数据或者是XML数据,需要注意的呢,在使用此注解之后不会再走试图处理器, * 而是直接将数据写入到输入流中,他的效果等同于通过response对象输出指定格式的数据。 * @param seckillId * @return */ @RequestMapping(value = "/{seckillId}/exposer", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) @ResponseBody public SeckillResult<Exposer> exposer(@PathVariable("seckillId") Long seckillId) { SeckillResult<Exposer> result; try { Exposer exposer = seckillService.exportSeckillUrl(seckillId); result = new SeckillResult<Exposer>(true, exposer); } catch (Exception e) { logger.error(e.getMessage(), e); result = new SeckillResult<Exposer>(false, e.getMessage()); } return result; }

转载请注明原文地址: https://www.6miu.com/read-9238.html

最新回复(0)