1、spring 事物处理机制
需要抛出异常事物处理机制才能捕捉到
<!-- 配置事务 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 使用annotation定义事务--> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <aop:config proxy-target-class="true"> <aop:pointcut id="servicePointcut" expression="execution(service.impl..*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut" order="2"/> <aop:advisor advice-ref="dataSourceExchange" pointcut-ref="servicePointcut" order="1"/> </aop:config>
public Result insertEnumber(EnumberDto enumberDto) throws Exception { Result result = new Result(); List<EnumberDetailDto> detailList = new ArrayList<EnumberDetailDto>(); int num = enumberDto.getQuantity(); for (int i = 0; i < num; i++) { EnumberDetailDto detailBean = new EnumberDetailDto(); detailBean.setBatchNo(enumberDto.getBatchNo()); detailBean.seteCode(DateUtils.getTimestampStr() + new Random().nextInt(10000)); detailBean.setState("NOTOUT"); detailBean.setAgentId(enumberDto.getAgentId()); detailBean.setAgentName(enumberDto.getAgentName()); detailBean.setCreateBy(enumberDto.getCreateBy()); detailBean.setModifyBy(enumberDto.getModifyBy()); detailList.add(detailBean); } enumberDao.insertEnumber(enumberDto); enumberDetailDao.insertEnumberDetailList(detailList); result.setSuccess(true); return result; }
2、将对象自动封装成json传到前台
在方法上加入@ResponseBody,同时方法返回值为实体对象,spring会自动将对象转换为json格式,并返回到客户端。如下所示:
@RequestMapping("/json1") @ResponseBody public Demo json1() { Demo demo=new Demo(); demo.setBirthday(new Date()); demo.setCreateTime(new Date()); demo.setHeight(170); demo.setName("tomcat"); demo.setRemark("json测试"); demo.setStatus((short)1); return demo; }
3、避免IE执行AJAX时,返回JSON出现下载文件的配置
4、post提交中文乱码解决方案
<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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
5、想要通过AppContextUtil.getBean("***")获取对象,必须将该对象对应的路径配置到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:p="http://www.springframework.org/schema/p" 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-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- 引入jdbc配置文件 --> <context:property-placeholder location="/WEB-INF/jdbc.properties" /> <!-- 要通过 AppContextUtil.getBean("privilegeService")获得service这个一定要在Mybatis配置文件中--> <context:component-scan base-package="com.service" /> <!--创建jdbc数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <!-- 创建session工厂 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描com/dao/impl/下的*.Impl文件, 省掉mybatis-config.xml里的手工配置 --> <property name="mapperLocations" value="classpath*:/com/dao/impl/*Impl.xml"/> </bean> <!-- 扫描,自动装配com.dao下的类(映射扫描) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.dao" /> </bean> <!--把mybatis的事务交由spring去管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注意:此处的数据源要与sqlSessionFactory中的dataSource相同 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 注解方式配置事物 --> <tx:annotation-driven /> </beans>
说明: 要想通过 AppContextUtil.getBean("privilegeService")获得service,一定洋将
<context:component-scan base-package="com.service" />,配置到mybatis配置数据员的配置文件中,如上面例子。
6、下载文件
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; public class FileDown { /** * 下载pdf * @param pictureShoppertrakAddress pdf地址 * @param pdfName pdf文件名 */ public void downPdfFile(String pictureShoppertrakAddress,String pdfName){ HttpServletResponse response = ServletActionContext.getResponse(); response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); //pdfName = "activityPdf_1441441792005.pdf" response.setHeader("Content-Disposition", "attachment;fileName=" + pdfName); try { //pictureShoppertrakAddress = "E:/shoppertrak/file"; InputStream inputStream = new FileInputStream(new File(pictureShoppertrakAddress + File.separator + pdfName)); OutputStream os = response.getOutputStream(); byte[] b = new byte[2048]; int length; while ((length = inputStream.read(b)) > 0) { os.write(b, 0, length); } // 这里主要关闭。 os.close(); inputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
7、springMVC组件
@Component:组件,项目启动后加载累
@Scheduled(fixedRate = 50000):延时,XXX毫秒;
package com.wanrong.task.jobs; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; ...... /** * 类ScheduledTasks.java的实现描述:TODO 类实现描述 */ @Component public class ScheduledTasks { protected Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired TaskInfoService taskInfoService; @Scheduled(fixedRate = 50000) public void taskStart() { QueryResult<TaskInfoDTO> result = taskInfoService.queryTaskInfoAll(); // 判断是否成功 if (result.isSuccess()) { List<TaskInfoDTO> taskList = result.getResults(); for (TaskInfoDTO taskInfo : taskList) { if (taskInfo.getStatus() == 1) { boolean flag = QuartzManager.checkExists(taskInfo.getJobName(), taskInfo.getJobGroup()); if (!flag) { QuartzManager.addJob(taskInfo.getJobName(), taskInfo.getJobGroup(), taskInfo.getTriggerName(),taskInfo.getTriggerGroupName(), taskInfo.getProcessClass(), taskInfo.getCronExpression()); logger.info("------" + taskInfo.getJobName() + "----" + taskInfo.getJobGroup() + "-------add succeed-----"); } } } } else { logger.error("call taskInfoService.queryTaskInfoAllWithCached error"); } } }
8、两个对象之间的属性拷贝
org.springframework.beans.BeanUtils.copyProperties(msg, cardInfo);
说明:将msg对象的属性拷贝到cardInfo对象中
相关推荐
赠送jar包:sentinel-spring-webmvc-adapter-1.8.0.jar; 赠送原API文档:sentinel-spring-webmvc-adapter-1.8.0-javadoc.jar; 赠送源代码:sentinel-spring-webmvc-adapter-1.8.0-sources.jar; 赠送Maven依赖信息...
赠送jar包:springfox-spring-webmvc-3.0.0.jar; 赠送原API文档:springfox-spring-webmvc-3.0.0-javadoc.jar; 赠送源代码:springfox-spring-webmvc-3.0.0-sources.jar; 赠送Maven依赖信息文件:springfox-...
赠送jar包:sentinel-spring-webmvc-adapter-1.8.0.jar; 赠送原API文档:sentinel-spring-webmvc-adapter-1.8.0-javadoc.jar; 赠送源代码:sentinel-spring-webmvc-adapter-1.8.0-sources.jar; 赠送Maven依赖信息...
赠送jar包:springfox-spring-webmvc-3.0.0.jar; 赠送原API文档:springfox-spring-webmvc-3.0.0-javadoc.jar; 赠送源代码:springfox-spring-webmvc-3.0.0-sources.jar; 赠送Maven依赖信息文件:springfox-...
赠送jar包:spring-webmvc-5.2.15.RELEASE.jar; 赠送原API文档:spring-webmvc-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-webmvc-5.2.15.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-...
spring-webmvc-portlet-3.2.7.RELEASE.jarspring-webmvc-portlet-3.2.7.RELEASE.jarspring-webmvc-portlet-3.2.7.RELEASE.jarspring-webmvc-portlet-3.2.7.RELEASE.jar
赠送jar包:spring-webmvc-5.3.15.jar; 赠送原API文档:spring-webmvc-5.3.15-javadoc.jar; 赠送源代码:spring-webmvc-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.3.15.pom; 包含翻译后的API...
赠送jar包:spring-webmvc-5.3.7.jar; 赠送原API文档:spring-webmvc-5.3.7-javadoc.jar; 赠送源代码:spring-webmvc-5.3.7-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.3.7.pom; 包含翻译后的API文档...
赠送jar包:spring-webmvc-5.0.8.RELEASE.jar; 赠送原API文档:spring-webmvc-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-webmvc-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.0.8....
赠送jar包:spring-webmvc-5.3.10.jar; 赠送原API文档:spring-webmvc-5.3.10-javadoc.jar; 赠送源代码:spring-webmvc-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.3.10.pom; 包含翻译后的API...
spring-webmvc-struts.jar对Struts和Spring整合时需要用到的包
开发工具 spring-webmvc-4.3.6.RELEASE开发工具 spring-webmvc-4.3.6.RELEASE开发工具 spring-webmvc-4.3.6.RELEASE开发工具 spring-webmvc-4.3.6.RELEASE开发工具 spring-webmvc-4.3.6.RELEASE开发工具 spring-web...
赠送jar包:spring-webmvc-5.3.7.jar; 赠送原API文档:spring-webmvc-5.3.7-javadoc.jar; 赠送源代码:spring-webmvc-5.3.7-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.3.7.pom; 包含翻译后的API文档...
Spring Web MVC模块(spring-webmvc-5.2.3.RELEASE.jar)是Spring框架中用于构建MVC(Model-View-Controller)架构的Web应用程序的核心部分。它为Web开发提供了一种强大且灵活的模型绑定、数据验证、异常处理和视图...
spring-webmvc-4.3.7.RELEASE-sources.jar spring-webmvc-4.3.7.RELEASE-sources.jar
spring-webmvc-portlet-4.0.0.RELEASE.jar
赠送jar包:spring-webmvc-5.0.8.RELEASE.jar; 赠送原API文档:spring-webmvc-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-webmvc-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.0.8....
spring-webmvc-5.1.5.RELEASE.jar
`spring-mvc-4.2.xsd`是Spring MVC 4.2版本的XML Schema定义文件。这个文件包含了所有在Spring MVC 4.2版本中可以使用的XML配置元素和属性,例如`<mvc:annotation-driven>`、`<bean>`、`<context:component-scan>`等...
"spring-mvc-showcase"项目是Spring官网提供的一个实战示例,旨在帮助开发者深入理解和运用Spring MVC的关键特性。本文将围绕这个项目,详细介绍Spring MVC的核心概念、工作原理以及如何在实际开发中运用。 一、...