spring-context.xml configuration (reference)
<?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"> <context:annotation-config /> <context:component-scan base-package="com.funtl.my.shop.web.admin"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 配置 Bean Validator 定义 --> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/> <bean id="beanValidator" class="com.funtl.my.shop.commons.validator.BeanValidator"> <property name="validator" ref="validator" /> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource "ref =" dataSource "/> <-! open the annotation-driven transactions -> </ bean> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
BeanValidator Tools
org.springframework.beans.factory.annotation.Autowired Import; Import javax.validation.ConstraintViolation; Import javax.validation.ConstraintViolationException; Import javax.validation.Validator; Import of java.util.ArrayList; Import the java.util.HashMap; Import Java .util.List; Import a java.util.Map; Import java.util.Set; / ** . * JSR303 the Validator (the Validator the Hibernate) tools * <P> . * ConstraintViolation contains propertyPath, message information and invalidValue * provided convert a variety of methods for different needs of i18n: * 1. List <String>, String content to the Message * 2. List <String>, String Separator + + content to the Message propertyPath * 3. the Map <propertyPath, the Message> * <p> * For details, see Wiki: https://github.com/springside/springside4/wiki/HibernateValidator * * <P> the Title: BeanValidator </ P> * <P> the Description: </ P> * * @author Lusifer * @version 1.0.0 * @date 2018/6/26 17:21 * / public class BeanValidator { @Autowired // can not be automatically injected, is a static property can not inject, you can delete this comment, the following need to manually inject Private static Validator Validator; public void setValidator static (the Validator Validator) { BeanValidator.validator = Validator; } / ** * JSR303 call validate the method, if the validation fails ConstraintViolationException thrown. * / private static void validateWithException(Validator validator, Object object, Class<?>... groups) throws ConstraintViolationException { Set constraintViolations = validator.validate(object, groups); if (!constraintViolations.isEmpty()) { throw new ConstraintViolationException(constraintViolations); } } /** * 辅助方法, 转换 ConstraintViolationException 中的 Set<ConstraintViolations> 中为 List<message>. */ private static List<String> extractMessage(ConstraintViolationException e) { return extractMessage(e.getConstraintViolations()); } /** * 辅助方法, 转换 Set<ConstraintViolation> 为 List<message> */ private static List<String> extractMessage(Set<? extends ConstraintViolation> constraintViolations) { List<String> errorMessages = new ArrayList<>(); for (ConstraintViolation violation : constraintViolations) { errorMessages.add(violation.getMessage()); } return errorMessages; } /** * 辅助方法, 转换 ConstraintViolationException 中的 Set<ConstraintViolations> 为 Map<property, message>. */ private static Map<String, String> extractPropertyAndMessage(ConstraintViolationException e) { return extractPropertyAndMessage(e.getConstraintViolations()); } /** * 辅助方法, 转换 Set<ConstraintViolation> 为 Map<property, message>. */ private static Map<String, String> extractPropertyAndMessage(Set<? extends ConstraintViolation> constraintViolations) { Map<String, String> errorMessages = new HashMap<>(); for (ConstraintViolation violation : constraintViolations) { errorMessages.put(violation.getPropertyPath().toString(), violation.getMessage()); } return errorMessages; } /** * 辅助方法, 转换 ConstraintViolationException 中的 Set<ConstraintViolations> 为 List<propertyPath message>. */ private static List<String> extractPropertyAndMessageAsList(ConstraintViolationException e) { return extractPropertyAndMessageAsList(e.getConstraintViolations(), " "); } /** * 辅助方法, 转换 Set<ConstraintViolations> 为 List<propertyPath message>. */ private static List<String> extractPropertyAndMessageAsList(Set<? extends ConstraintViolation> constraintViolations) { return extractPropertyAndMessageAsList(constraintViolations, " "); } /** * 辅助方法, 转换 ConstraintViolationException 中的 Set<ConstraintViolations> 为 List<propertyPath + separator + message>. */ private static List<String> extractPropertyAndMessageAsList(ConstraintViolationException e, String separator) { return extractPropertyAndMessageAsList(e.getConstraintViolations(), separator); } /** * 辅助方法, 转换 Set<ConstraintViolation> 为 List<propertyPath + separator + message>. */ private static List<String> extractPropertyAndMessageAsList(Set<? extends ConstraintViolation> constraintViolations, String separator) { List<String> errorMessages = new ArrayList<>(); for (ConstraintViolation violation : constraintViolations) { errorMessages.add(violation.getPropertyPath() + separator + violation.getMessage()); } return errorMessages; } /** * 服务端参数有效性验证 * * @Param object validation entity object * @param groups validation group * @return verify success: return null; verification failed: returning an error message * / public static String Validator (Object Object, Class ... Groups <?>) { The try { validateWithException (Validator, Object, Groups); } the catch (ConstraintViolationException EX) { List <string> List = extractMessage (EX); List.add (0, "the data verification failed:"); // error message is a string package the StringBuilder new new SB = the StringBuilder (); for (int I = 0; I <list.size (); I ++) { String ExMSG = List.get (I); IF (! I = 0) { sb.append(String.format("%s. %s", i, exMsg)).append(list.size() > 1 ? "<br/>" : ""); } else { sb.append(exMsg).append(list.size() > 1 ? "<br/>" : ""); } } return sb.toString(); } return null; } }
spring-mvc.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:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <description>Spring MVC Configuration</ the Description> <-! load the configuration properties file -> <context: the ignore-unresolvable Property-placeholder = "to true" LOCATION = "the CLASSPATH: myshop.properties" /> <-! Annotation using automatic enrollment Bean, scan only @Controller -> <context: the Component-Base-Package Penalty for Scan = "com.funtl.my.shop.web.admin" use-default-Filters = "to false"> <context: the include filter-type = "Annotation" = expression The "org.springframework.stereotype.Controller" /> </ context: the Component-Scan> <- supports default mapping annotations ->! <MVC: annotation-Driven /> ! <- view file parsing definitions -> <bean class = "org.springframework.web.servlet .view.InternalResourceViewResolver "> <Property name =" prefix "value =" $ {web.view.prefix}"/> <property name="suffix" value="${web.view.suffix}"/> </bean> <!-- 静态资源映射 --> <mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/> <!-- 拦截器配置 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <mvc:exclude-mapping path="/login" /> <mvc:exclude-mapping path="/static/**" /> <bean class="com.funtl.my.shop.web.admin.web.interceptor.LoginInterceptor" /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.funtl.my.shop.web.admin.web.interceptor.PermissionInterceptor" /> </mvc:interceptor> </mvc:interceptors> <-! Upload files to intercept, set the maximum upload file size = 10 * 1024 * 10M 1024 (B) = 10485760 bytes -> <bean the above mentioned id = "the MultipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver "> <Property name =" maxUploadSize "value =" $ {web.maxUploadSize} "/> </ the bean> </ Beans>
mybatis-config.xml
<? xml Version = "1.0" encoding = "UTF-8"?> <DOCTYPE the Configuration the PUBLIC! "- // mybatis.org//DTD Config 3.0 // EN" "http://mybatis.org/dtd/mybatis config.dtd--3 "> <the Configuration> <-! global parameters -> <Settings> <-! printing SQL statements -> <Setting name =" logImpl "value =" STDOUT_LOGGING "/> <! - - the global map to enable or disable the cache. -> <Setting name = "cacheEnabled" value = "false" /> <-! Globally enable or disable lazy loading. When disabled, all associations will be eagerly loaded. -> <Setting name = "lazyLoadingEnabled" value = "to true" /> <-! When enabled, there is a delay loading the object properties will be fully loaded when any property is called. Otherwise, each property will be loaded on demand. -> <Setting name = "multipleResultSetsEnabled" value = "to true" /> <-! Can alias (depending on the drive compatibility) default column: to true -> <Setting name = "useColumnLabel" value = "to true" /> <! - allows JDBC generate primary key. You need to drive support. If set to a true, this setting will be forced to use to generate the primary key, there are some drivers are not compatible, but can still perform. default: to false -> <Setting name = "useGeneratedKeys" value = "to false" /> <- Specifies how to automatically map columns NONE MyBatis data base table:! does not map the PARTIAL: Part FULL: All -> <Setting name = "autoMappingBehavior" value = "the PARTIAL" /> ! <- this is the default execution type (sIMPLE: simple; rEUSE: actuator may reuse prepared statements statements; bATCH: the actuator can repeat the statements and batch updates) - -> <Setting name = "defaultExecutorType" value = " <Setting name = "mapUnderscoreToCamelCase" value = "to true" /> <- set up a local cache range session:! data will be shared statement: statement range (this would not have shared data) defalut: session -> <setting name = "localCacheScope" value = "the SESSION" /> <- set the JDBC type is empty, some drivers to the specified value, default:! OTHER, insertion need not specify the type of null -> <setting = name "jdbcTypeForNull" value = "NULL" /> </ Settings> </ Configuration>
spring-context-mybatis.xml
<? XML Version = "1.0" encoding = "UTF-. 8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: the xsi = "http://www.w3.org / 2001 / XMLSchema-instance " xsi: schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP: // the WWW. http://www.springframework.org/schema/tx/spring-tx.xsd springframework.org/schema/tx "> <-! configure the SqlSession -> <the bean ID =" SqlSessionFactory "class =" org.mybatis .spring.SqlSessionFactoryBean "> <Property name =" the dataSource "REF =" the dataSource "/> <-! for the entity class corresponding to the configuration where the package can be used among a plurality of package ',' split number -> < property name = "typeAliasesPackage "value =" com.funtl.my.shop.domain "/> <-! for configuring object-relational mapping configuration file directory -> <property name="mapperLocations" value="classpath:/mapper/**/*.xml"/> <property name="configLocation" value="classpath:/mybatis-config.xml"></property> </bean> <!-- 扫描 Mapper --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.funtl.my.shop.web.admin.dao" /> </bean> </beans>
log4j.properties
log4j.rootLogger=DEBUG, console, file log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.file=org.apache.log4j.DailyRollingFileAppender log4j.appender.file.File=D://logs/log.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.A3.MaxFileSize=1024KB log4j.appender.A3.MaxBackupIndex=10 log4j.appender.file.layout.ConversionPattern=%d %p [%c] - %m%n