基于Spring + Spring MVC + Mybatis 高性能web构建

转自:http://blog.csdn.net/zoutongyuan/article/details/41379851

基于Spring + Spring MVC + Mybatis 高性能web构建

一直想写这篇文章,前段时间 痴迷于JavaScript、NodeJs、AngularJs,做了大量的研究,对前后端交互有了更深层次的认识。

今天抽个时间写这篇文章,我有预感,这将是一篇很详细的文章,详细的配置,详细的注释,看起来应该很容易懂。

用最合适的技术去实现,并不断追求最佳实践。这就是架构之道。

希望这篇文章能给你们带来一些帮助,同时希望你们可以为这个项目贡献你的想法。


源码地址:https://github.com/starzou/quick4j 点击打开


看我们的项目结构:

基于Spring + Spring MVC + Mybatis 高性能web构建_第1张图片

是一个典型的Maven 项目 :

src/main/java:存放java源文件
src/main/resources:存放程序资源、配置文件
src/test/java:存放测试代码文件
src/main/webapp:web根目录
pom.xml : maven项目配置文件,管理依赖,编译,打包


主要的后端架构:Spring + Spring MVC + Mybatis + Apache Shiro

前端界面主要使用MetroNic 模板,


先看我们搭建完成,跑起来的效果,这样你才有兴趣看下去:

基于Spring + Spring MVC + Mybatis 高性能web构建_第2张图片

基于Spring + Spring MVC + Mybatis 高性能web构建_第3张图片


你可以 在github 上 checkout quick4j项目 查看 ,并跟下面步骤 来搭建:

强烈建议你,checkout  https://github.com/starzou/quick4j ,在本地跑起来,再试着自己搭建框架


1、首先创建 maven 项目 ,用 idea 、eclipse 或 mvn 命令行都行 

2、配置 pom.xml ,添加框架依赖

[html]  view plain copy print ?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0modelVersion>  
  4.     <groupId>com.eliteamsgroupId>  
  5.     <artifactId>quick4jartifactId>  
  6.     <packaging>warpackaging>  
  7.     <version>1.0.0version>  
  8.     <name>quick4j Appname>  
  9.     <url>https://github.com/starzou/quick4jurl>  
  10.   
  11.     <build>  
  12.         <finalName>quick4jfinalName>  
  13.         <plugins>  
  14.               
  15.             <plugin>  
  16.                 <groupId>org.mybatis.generatorgroupId>  
  17.                 <artifactId>mybatis-generator-maven-pluginartifactId>  
  18.                 <version>${plugin.mybatis.generator}version>  
  19.                 <configuration>  
  20.                     <configurationFile>${mybatis.generator.generatorConfig.xml}configurationFile>  
  21.                     <overwrite>trueoverwrite>  
  22.                     <verbose>trueverbose>  
  23.                 configuration>  
  24.             plugin>  
  25.   
  26.               
  27.             <plugin>  
  28.                 <groupId>org.apache.maven.pluginsgroupId>  
  29.                 <artifactId>maven-compiler-pluginartifactId>  
  30.                 <version>${plugin.maven-compiler}version>  
  31.                 <configuration>  
  32.                     <source>${project.build.jdk}source>  
  33.                     <target>${project.build.jdk}target>  
  34.                     <encoding>${project.build.sourceEncoding}encoding>  
  35.                 configuration>  
  36.             plugin>  
  37.         plugins>  
  38.   
  39.           
  40.         <resources>  
  41.             <resource>  
  42.                 <directory>src/main/resourcesdirectory>  
  43.                 <includes>  
  44.                     <include>**/*.propertiesinclude>  
  45.                     <include>**/*.xmlinclude>  
  46.                 includes>  
  47.                 <filtering>truefiltering>  
  48.             resource>  
  49.             <resource>  
  50.                 <directory>src/main/javadirectory>  
  51.                 <includes>  
  52.                     <include>**/*.propertiesinclude>  
  53.                     <include>**/*.xmlinclude>  
  54.                 includes>  
  55.                 <filtering>truefiltering>  
  56.             resource>  
  57.         resources>  
  58.     build>  
  59.   
  60.     <properties>  
  61.           
  62.         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
  63.         <project.build.locales>zh_CNproject.build.locales>  
  64.         <project.build.jdk>1.7project.build.jdk>  
  65.   
  66.           
  67.         <mybatis.generator.generatorConfig.xml>${basedir}/src/test/resources/generatorConfig.xmlmybatis.generator.generatorConfig.xml>  
  68.         <mybatis.generator.generatorConfig.properties>file:///${basedir}/src/test/resources/generatorConfig.propertiesmybatis.generator.generatorConfig.properties>  
  69.   
  70.           
  71.         <plugin.mybatis.generator>1.3.1plugin.mybatis.generator>  
  72.         <plugin.maven-compiler>3.1plugin.maven-compiler>  
  73.   
  74.           
  75.         <junit.version>4.11junit.version>  
  76.         <spring.version>4.0.2.RELEASEspring.version>  
  77.         <mybatis.version>3.2.2mybatis.version>  
  78.         <mybatis.spring.version>1.2.2mybatis.spring.version>  
  79.         <mysql.connector.version>5.1.30mysql.connector.version>  
  80.         <postgresql.version>9.1-901.jdbc4postgresql.version>  
  81.         <slf4j.version>1.6.6slf4j.version>  
  82.         <log4j.version>1.2.12log4j.version>  
  83.         <httpclient.version>4.1.2httpclient.version>  
  84.         <jackson.version>1.9.13jackson.version>  
  85.         <c3p0.version>0.9.1.2c3p0.version>  
  86.         <druid.version>1.0.5druid.version>  
  87.         <tomcat.jdbc.version>7.0.53tomcat.jdbc.version>  
  88.         <jstl.version>1.2jstl.version>  
  89.         <google.collections.version>1.0google.collections.version>  
  90.         <cglib.version>3.1cglib.version>  
  91.         <shiro.version>1.2.3shiro.version>  
  92.         <commons.fileupload.version>1.3.1commons.fileupload.version>  
  93.         <commons.codec.version>1.9commons.codec.version>  
  94.         <commons.net.version>3.3commons.net.version>  
  95.         <aspectj.version>1.6.12aspectj.version>  
  96.         <netty.version>4.0.18.Finalnetty.version>  
  97.         <hibernate.validator.version>5.1.1.Finalhibernate.validator.version>  
  98.     properties>  
  99.   
  100.     <dependencies>  
  101.           
  102.         <dependency>  
  103.             <groupId>junitgroupId>  
  104.             <artifactId>junitartifactId>  
  105.             <version>${junit.version}version>  
  106.         dependency>  
  107.   
  108.           
  109.         <dependency>  
  110.             <groupId>org.springframeworkgroupId>  
  111.             <artifactId>spring-coreartifactId>  
  112.             <version>${spring.version}version>  
  113.         dependency>  
  114.   
  115.         <dependency>  
  116.             <groupId>org.springframeworkgroupId>  
  117.             <artifactId>spring-webartifactId>  
  118.             <version>${spring.version}version>  
  119.         dependency>  
  120.   
  121.         <dependency>  
  122.             <groupId>org.springframeworkgroupId>  
  123.             <artifactId>spring-oxmartifactId>  
  124.             <version>${spring.version}version>  
  125.         dependency>  
  126.   
  127.         <dependency>  
  128.             <groupId>org.springframeworkgroupId>  
  129.             <artifactId>spring-txartifactId>  
  130.             <version>${spring.version}version>  
  131.         dependency>  
  132.   
  133.         <dependency>  
  134.             <groupId>org.springframeworkgroupId>  
  135.             <artifactId>spring-jdbcartifactId>  
  136.             <version>${spring.version}version>  
  137.         dependency>  
  138.   
  139.         <dependency>  
  140.             <groupId>org.springframeworkgroupId>  
  141.             <artifactId>spring-webmvcartifactId>  
  142.             <version>${spring.version}version>  
  143.         dependency>  
  144.   
  145.         <dependency>  
  146.             <groupId>org.springframeworkgroupId>  
  147.             <artifactId>spring-aopartifactId>  
  148.             <version>${spring.version}version>  
  149.         dependency>  
  150.   
  151.         <dependency>  
  152.             <groupId>org.springframeworkgroupId>  
  153.             <artifactId>spring-context-supportartifactId>  
  154.             <version>${spring.version}version>  
  155.         dependency>  
  156.   
  157.         <dependency>  
  158.             <groupId>org.springframeworkgroupId>  
  159.             <artifactId>spring-testartifactId>  
  160.             <version>${spring.version}version>  
  161.         dependency>  
  162.           
  163.   
  164.           
  165.         <dependency>  
  166.             <groupId>org.mybatisgroupId>  
  167.             <artifactId>mybatisartifactId>  
  168.             <version>${mybatis.version}version>  
  169.         dependency>  
  170.   
  171.         <dependency>  
  172.             <groupId>org.mybatisgroupId>  
  173.             <artifactId>mybatis-springartifactId>  
  174.             <version>${mybatis.spring.version}version>  
  175.         dependency>  
  176.           
  177.   
  178.           
  179.         <dependency>  
  180.             <groupId>mysqlgroupId>  
  181.             <artifactId>mysql-connector-javaartifactId>  
  182.             <version>${mysql.connector.version}version>  
  183.         dependency>  
  184.   
  185.           
  186.         <dependency>  
  187.             <groupId>com.alibabagroupId>  
  188.             <artifactId>druidartifactId>  
  189.             <version>${druid.version}version>  
  190.         dependency>  
  191.   
  192.           
  193.         <dependency>  
  194.             <groupId>org.codehaus.jacksongroupId>  
  195.             <artifactId>jackson-mapper-aslartifactId>  
  196.             <version>${jackson.version}version>  
  197.         dependency>  
  198.   
  199.           
  200.         <dependency>  
  201.             <groupId>log4jgroupId>  
  202.             <artifactId>log4jartifactId>  
  203.             <version>${log4j.version}version>  
  204.         dependency>  
  205.         <dependency>  
  206.             <groupId>org.slf4jgroupId>  
  207.             <artifactId>slf4j-apiartifactId>  
  208.             <version>${slf4j.version}version>  
  209.         dependency>  
  210.         <dependency>  
  211.             <groupId>org.slf4jgroupId>  
  212.             <artifactId>slf4j-log4j12artifactId>  
  213.             <version>${slf4j.version}version>  
  214.         dependency>  
  215.           
  216.   
  217.           
  218.         <dependency>  
  219.             <groupId>javax.servletgroupId>  
  220.             <artifactId>javax.servlet-apiartifactId>  
  221.             <version>3.0.1version>  
  222.             <scope>providedscope>  
  223.         dependency>  
  224.   
  225.           
  226.         <dependency>  
  227.             <groupId>javax.servletgroupId>  
  228.             <artifactId>jstlartifactId>  
  229.             <version>${jstl.version}version>  
  230.         dependency>  
  231.   
  232.           
  233.         <dependency>  
  234.             <groupId>commons-fileuploadgroupId>  
  235.             <artifactId>commons-fileuploadartifactId>  
  236.             <version>${commons.fileupload.version}version>  
  237.         dependency>  
  238.   
  239.         <dependency>  
  240.             <groupId>org.apache.httpcomponentsgroupId>  
  241.             <artifactId>httpclientartifactId>  
  242.             <version>${httpclient.version}version>  
  243.         dependency>  
  244.   
  245.         <dependency>  
  246.             <groupId>commons-codecgroupId>  
  247.             <artifactId>commons-codecartifactId>  
  248.             <version>${commons.codec.version}version>  
  249.         dependency>  
  250.   
  251.         <dependency>  
  252.             <groupId>commons-netgroupId>  
  253.             <artifactId>commons-netartifactId>  
  254.             <version>${commons.net.version}version>  
  255.         dependency>  
  256.   
  257.         <dependency>  
  258.             <groupId>commons-logginggroupId>  
  259.             <artifactId>commons-loggingartifactId>  
  260.             <version>1.1.3version>  
  261.         dependency>  
  262.         <dependency>  
  263.             <groupId>commons-collectionsgroupId>  
  264.             <artifactId>commons-collectionsartifactId>  
  265.             <version>3.2.1version>  
  266.         dependency>  
  267.   
  268.           
  269.   
  270.           
  271.         <dependency>  
  272.             <groupId>com.google.collectionsgroupId>  
  273.             <artifactId>google-collectionsartifactId>  
  274.             <version>${google.collections.version}version>  
  275.         dependency>  
  276.   
  277.           
  278.         <dependency>  
  279.             <groupId>cglibgroupId>  
  280.             <artifactId>cglib-nodepartifactId>  
  281.             <version>${cglib.version}version>  
  282.         dependency>  
  283.   
  284.   
  285.           
  286.         <dependency>  
  287.             <groupId>org.apache.shirogroupId>  
  288.             <artifactId>shiro-springartifactId>  
  289.             <version>${shiro.version}version>  
  290.         dependency>  
  291.         <dependency>  
  292.             <groupId>org.apache.shirogroupId>  
  293.             <artifactId>shiro-ehcacheartifactId>  
  294.             <version>${shiro.version}version>  
  295.         dependency>  
  296.         <dependency>  
  297.             <groupId>org.apache.shirogroupId>  
  298.             <artifactId>shiro-coreartifactId>  
  299.             <version>${shiro.version}version>  
  300.         dependency>  
  301.         <dependency>  
  302.             <groupId>org.apache.shirogroupId>  
  303.             <artifactId>shiro-webartifactId>  
  304.             <version>${shiro.version}version>  
  305.         dependency>  
  306.         <dependency>  
  307.             <groupId>org.apache.shirogroupId>  
  308.             <artifactId>shiro-quartzartifactId>  
  309.             <version>${shiro.version}version>  
  310.         dependency>  
  311.   
  312.           
  313.         <dependency>  
  314.             <groupId>org.aspectjgroupId>  
  315.             <artifactId>aspectjweaverartifactId>  
  316.             <version>${aspectj.version}version>  
  317.         dependency>  
  318.         <dependency>  
  319.             <groupId>org.aspectjgroupId>  
  320.             <artifactId>aspectjrtartifactId>  
  321.             <version>${aspectj.version}version>  
  322.         dependency>  
  323.   
  324.           
  325.         <dependency>  
  326.             <groupId>org.hibernategroupId>  
  327.             <artifactId>hibernate-validatorartifactId>  
  328.             <version>${hibernate.validator.version}version>  
  329.         dependency>  
  330.   
  331.           
  332.         <dependency>  
  333.             <groupId>io.nettygroupId>  
  334.             <artifactId>netty-allartifactId>  
  335.             <version>${netty.version}version>  
  336.         dependency>  
  337.   
  338.         <dependency>  
  339.             <groupId>org.mybatis.generatorgroupId>  
  340.             <artifactId>mybatis-generator-coreartifactId>  
  341.             <version>1.3.2version>  
  342.             <type>jartype>  
  343.             <scope>testscope>  
  344.         dependency>  
  345.   
  346.     dependencies>  
  347. project>  


3、配置web.xml

web.xml是一个项目的核心,看看它的一些配置:
配置 ContextLoaderListener 监听器
配置Spring字符编码过滤器
配置shiro 安全过滤器
配置Spring MVC 核心控制器 DispatcherServlet
配置一些页面

spring 和 apache shiro 是由一个 ContextLoaderListener 监听器 加载的配置文件,并初始化

[html]  view plain copy print ?
  1. xml version="1.0" encoding="utf-8"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  4.       
  5.       
  6.     <context-param>  
  7.         <param-name>contextConfigLocationparam-name>  
  8.         <param-value>  
  9.             classpath*:applicationContext.xml  
  10.             classpath*:applicationContext-shiro.xml  
  11.         param-value>  
  12.     context-param>  
  13.       
  14.     <listener>  
  15.         <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>  
  16.     listener>  
  17.       
  18.   
  19.       
  20.     <filter>  
  21.         <filter-name>encodingFilterfilter-name>  
  22.         <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>  
  23.         <init-param>  
  24.             <param-name>encodingparam-name>  
  25.             <param-value>UTF-8param-value>  
  26.         init-param>  
  27.         <init-param>  
  28.             <param-name>forceEncodingparam-name>  
  29.             <param-value>trueparam-value>  
  30.         init-param>  
  31.     filter>  
  32.     <filter-mapping>  
  33.         <filter-name>encodingFilterfilter-name>  
  34.         <url-pattern>/*url-pattern>  
  35.     filter-mapping>  
  36.   
  37.       
  38.     <filter>  
  39.         <filter-name>shiroFilterfilter-name>  
  40.         <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>  
  41.         <async-supported>trueasync-supported>  
  42.         <init-param>  
  43.             <param-name>targetFilterLifecycleparam-name>  
  44.             <param-value>trueparam-value>  
  45.         init-param>  
  46.     filter>  
  47.     <filter-mapping>  
  48.         <filter-name>shiroFilterfilter-name>  
  49.         <url-pattern>/*url-pattern>  
  50.     filter-mapping>  
  51.   
  52.       
  53.     <context-param>  
  54.         <param-name>log4jConfigLocationparam-name>  
  55.         <param-value>classpath:log4j.propertiesparam-value>  
  56.     context-param>  
  57.       
  58.     <context-param>  
  59.         <param-name>log4jRefreshIntervalparam-name>  
  60.         <param-value>60000param-value>  
  61.     context-param>  
  62.   
  63.       
  64.     <listener>  
  65.         <listener-class>org.springframework.web.util.Log4jConfigListenerlistener-class>  
  66.     listener>  
  67.   
  68.       
  69.     <servlet>  
  70.         <servlet-name>dispatcherservlet-name>  
  71.         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
  72.         <init-param>  
  73.             <param-name>contextConfigLocationparam-name>  
  74.             <param-value>classpath*:spring-mvc.xmlparam-value>  
  75.         init-param>  
  76.         <load-on-startup>1load-on-startup>  
  77.     servlet>  
  78.     <servlet-mapping>  
  79.         <servlet-name>dispatcherservlet-name>  
  80.           
  81.         <url-pattern>/rest/*url-pattern>  
  82.     servlet-mapping>  
  83.   
  84.       
  85.     <welcome-file-list>  
  86.         <welcome-file>rest/indexwelcome-file>  
  87.     welcome-file-list>  
  88.   
  89.       
  90.     <error-page>  
  91.         <error-code>404error-code>  
  92.         <location>/rest/page/404location>  
  93.     error-page>  
  94.     <error-page>  
  95.         <error-code>500error-code>  
  96.         <location>/rest/page/500location>  
  97.     error-page>  
  98.     <error-page>  
  99.         <exception-type>org.apache.shiro.authz.AuthorizationExceptionexception-type>  
  100.         <location>/rest/page/401location>  
  101.     error-page>  
  102.   
  103. web-app>  


4、spring配置:

applicationContext.xml

[html]  view plain copy print ?
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"  
  5.        xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
  6.        xmlns:cache="http://www.springframework.org/schema/cache"  
  7.        xsi:schemaLocation="  
  8.     http://www.springframework.org/schema/context  
  9.     http://www.springframework.org/schema/context/spring-context.xsd  
  10.     http://www.springframework.org/schema/beans  
  11.     http://www.springframework.org/schema/beans/spring-beans.xsd  
  12.     http://www.springframework.org/schema/tx  
  13.     http://www.springframework.org/schema/tx/spring-tx.xsd  
  14.     http://www.springframework.org/schema/jdbc  
  15.     http://www.springframework.org/schema/jdbc/spring-jdbc.xsd  
  16.     http://www.springframework.org/schema/cache  
  17.     http://www.springframework.org/schema/cache/spring-cache.xsd  
  18.     http://www.springframework.org/schema/aop  
  19.     http://www.springframework.org/schema/aop/spring-aop.xsd  
  20.     http://www.springframework.org/schema/util  
  21.     http://www.springframework.org/schema/util/spring-util.xsd">  
  22.   
  23.       
  24.     <context:component-scan base-package="com.eliteams.quick4j">context:component-scan>  
  25.   
  26.       
  27.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  28.         <property name="locations">  
  29.             <list>  
  30.                 <value>classpath*:application.propertiesvalue>  
  31.             list>  
  32.         property>  
  33.     bean>  
  34.   
  35.       
  36.     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
  37.           
  38.         <property name="url" value="${jdbc.url}"/>  
  39.         <property name="username" value="${jdbc.username}"/>  
  40.         <property name="password" value="${jdbc.password}"/>  
  41.   
  42.           
  43.         <property name="initialSize" value="${ds.initialSize}"/>  
  44.         <property name="minIdle" value="${ds.minIdle}"/>  
  45.         <property name="maxActive" value="${ds.maxActive}"/>  
  46.   
  47.           
  48.         <property name="maxWait" value="${ds.maxWait}"/>  
  49.   
  50.           
  51.         <property name="timeBetweenEvictionRunsMillis" value="${ds.timeBetweenEvictionRunsMillis}"/>  
  52.   
  53.           
  54.         <property name="minEvictableIdleTimeMillis" value="${ds.minEvictableIdleTimeMillis}"/>  
  55.   
  56.         <property name="validationQuery" value="SELECT 'x'"/>  
  57.         <property name="testWhileIdle" value="true"/>  
  58.         <property name="testOnBorrow" value="false"/>  
  59.         <property name="testOnReturn" value="false"/>  
  60.   
  61.           
  62.         <property name="poolPreparedStatements" value="false"/>  
  63.         <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>  
  64.   
  65.           
  66.         <property name="filters" value="stat"/>  
  67.     bean>  
  68.   
  69.       
  70.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource"  
  71.           p:configLocation="classpath:mybatis-config.xml"  
  72.           p:mapperLocations="classpath:com/eliteams/quick4j/web/dao/*.xml"/>  
  73.   
  74.       
  75.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.eliteams.quick4j.web.dao"  
  76.           p:sqlSessionFactoryBeanName="sqlSessionFactory"/>  
  77.   
  78.       
  79.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"  
  80.           p:dataSource-ref="dataSource"/>  
  81.   
  82.       
  83.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  84.         <tx:attributes>  
  85.               
  86.             <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>  
  87.             <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>  
  88.             <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>  
  89.               
  90.             <tx:method name="select*" read-only="true"/>  
  91.             <tx:method name="count*" read-only="true"/>  
  92.               
  93.             <tx:method name="*"/>  
  94.         tx:attributes>  
  95.     tx:advice>  
  96.   
  97.       
  98.     <aop:config>  
  99.         <aop:pointcut id="serviceMethods" expression="execution(* com.eliteams.quick4j.web.service..*(..))"/>  
  100.         <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>  
  101.     aop:config>  
  102.   
  103.       
  104.     <aop:aspectj-autoproxy proxy-target-class="true"/>  
  105.   
  106.       
  107.     <tx:annotation-driven transaction-manager="transactionManager"/>  
  108.   
  109.       
  110.     <cache:annotation-driven cache-manager="cacheManager"/>  
  111.     <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"  
  112.           p:configLocation="classpath:ehcache.xml"/>  
  113.     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"  
  114.           p:cacheManager-ref="ehCacheManagerFactory"/>  
  115. beans>  

application.properties

[plain]  view plain copy print ?
  1. ##JDBC Global Setting  
  2. jdbc.driver=com.mysql.jdbc.Driver  
  3. jdbc.url=jdbc:mysql://localhost:3306/quick4j?useUnicode=true&characterEncoding=utf-8  
  4. jdbc.username=root  
  5. jdbc.password=admin123  
  6.   
  7. ##DataSource Global Setting  
  8.   
  9. #配置初始化大小、最小、最大  
  10. ds.initialSize=1  
  11. ds.minIdle=1  
  12. ds.maxActive=20  
  13.   
  14. #配置获取连接等待超时的时间   
  15. ds.maxWait=60000  
  16.   
  17. #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒  
  18. ds.timeBetweenEvictionRunsMillis=60000  
  19.   
  20. #配置一个连接在池中最小生存的时间,单位是毫秒  
  21. ds.minEvictableIdleTimeMillis=300000  


ehcache.xml

[html]  view plain copy print ?
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <ehcache updateCheck="false" name="txswx-ehcache">  
  3.     <diskStore path="java.io.tmpdir"/>  
  4.       
  5.     <defaultCache maxEntriesLocalHeap="10000" eternal="true" timeToIdleSeconds="300" timeToLiveSeconds="600"  
  6.                   overflowToDisk="true" maxEntriesLocalDisk="100000"/>  
  7. ehcache>  

5、 Apache Shiro 配置 : 要配置realms bean

[html]  view plain copy print ?
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="  
  5.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">  
  7.   
  8.     <description>apache shiro配置description>  
  9.   
  10.     <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
  11.         <property name="securityManager" ref="securityManager"/>  
  12.         <property name="loginUrl" value="/rest/page/login"/>  
  13.         <property name="successUrl" value="/rest/index"/>  
  14.         <property name="unauthorizedUrl" value="/rest/page/401"/>  
  15.         <property name="filterChainDefinitions">  
  16.             <value>  
  17.                   
  18.                 /app/** = anon  
  19.                 /assets/** = anon  
  20.                   
  21.                 /rest/user/login = anon  
  22.                   
  23.                 /** = authc  
  24.             value>  
  25.         property>  
  26.     bean>  
  27.   
  28.       
  29.     <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">  
  30.         <property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml"/>  
  31.     bean>  
  32.   
  33.       
  34.     <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.MemorySessionDAO"/>  
  35.   
  36.       
  37.     <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">  
  38.         <property name="sessionDAO" ref="sessionDAO"/>  
  39.     bean>  
  40.   
  41.       
  42.     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
  43.         <property name="realms">  
  44.             <list>  
  45.                 <ref bean="securityRealm"/>  
  46.             list>  
  47.         property>  
  48.           
  49.           
  50.           
  51.     bean>  
  52.   
  53.       
  54.     <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>  
  55.   
  56. beans>  

ehcache-shiro.xml

[html]  view plain copy print ?
  1. <ehcache updateCheck="false" name="shiroCache">  
  2.   
  3.     <defaultCache  
  4.             maxElementsInMemory="10000"  
  5.             eternal="false"  
  6.             timeToIdleSeconds="120"  
  7.             timeToLiveSeconds="120"  
  8.             overflowToDisk="false"  
  9.             diskPersistent="false"  
  10.             diskExpiryThreadIntervalSeconds="120"  
  11.             />  
  12. ehcache>  


6、MyBatis 配置

[html]  view plain copy print ?
  1. xml version="1.0" encoding="UTF-8" ?>  
  2.         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  3.         "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  4. <configuration>  
  5.     <properties>  
  6.         <property name="dialectClass" value="com.eliteams.quick4j.core.feature.orm.dialect.MySql5Dialect"/>  
  7.     properties>  
  8.   
  9.       
  10.     <settings>  
  11.   
  12.           
  13.         <setting name="cacheEnabled" value="true"/>  
  14.   
  15.           
  16.         <setting name="lazyLoadingEnabled" value="true"/>  
  17.   
  18.           
  19.         <setting name="multipleResultSetsEnabled" value="true"/>  
  20.   
  21.           
  22.         <setting name="useColumnLabel" value="true"/>  
  23.   
  24.           
  25.         <setting name="useGeneratedKeys" value="false"/>  
  26.   
  27.           
  28.         <setting name="autoMappingBehavior" value="PARTIAL"/>  
  29.   
  30.           
  31.           
  32.   
  33.           
  34.           
  35.   
  36.           
  37.         <setting name="safeRowBoundsEnabled" value="false"/>  
  38.   
  39.           
  40.         <setting name="mapUnderscoreToCamelCase" value="true"/>  
  41.   
  42.           
  43.         <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>  
  44.   
  45.           
  46.         <setting name="aggressiveLazyLoading" value="false"/>  
  47.   
  48.     settings>  
  49.   
  50.     <typeAliases>  
  51.         <package name="com.eliteams.quick4j.web.model"/>  
  52.         <package name="com.eliteams.quick4j.web.enums"/>  
  53.     typeAliases>  
  54.   
  55.     <plugins>  
  56.         <plugin interceptor="com.eliteams.quick4j.core.feature.orm.mybatis.PaginationResultSetHandlerInterceptor"/>  
  57.         <plugin interceptor="com.eliteams.quick4j.core.feature.orm.mybatis.PaginationStatementHandlerInterceptor"/>  
  58.     plugins>  
  59.   
  60. configuration>  



7、 Spring MVC 配置

[html]  view plain copy print ?
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:aop="http://www.springframework.org/schema/aop"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.        xmlns:tx="http://www.springframework.org/schema/tx"  
  7.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  8.        xmlns:p="http://www.springframework.org/schema/p"  
  9.        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
  10.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  11.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd   
  12.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd   
  13.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">  
  14.   
  15.       
  16.     <context:component-scan base-package="com.eliteams.quick4j.web.controller"/>  
  17.   
  18.       
  19.       
  20.     <mvc:annotation-driven validator="validator"/>  
  21.   
  22.       
  23.     <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">  
  24.         <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>  
  25.           
  26.         <property name="validationMessageSource" ref="messageSource"/>  
  27.     bean>  
  28.   
  29.       
  30.     <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">  
  31.         <property name="basenames">  
  32.             <list>  
  33.                   
  34.                 <value>classpath:messagesvalue>  
  35.                 <value>classpath:org/hibernate/validator/ValidationMessagesvalue>  
  36.             list>  
  37.         property>  
  38.         <property name="useCodeAsDefaultMessage" value="false"/>  
  39.         <property name="defaultEncoding" value="UTF-8"/>  
  40.         <property name="cacheSeconds" value="60"/>  
  41.     bean>  
  42.   
  43.     <mvc:interceptors>  
  44.         <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>  
  45.     mvc:interceptors>  
  46.   
  47.     <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">  
  48.         <property name="defaultLocale" value="zh_CN"/>  
  49.     bean>  
  50.   
  51.       
  52.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  53.         <property name="messageConverters">  
  54.             <list>  
  55.                 <ref bean="mappingJacksonHttpMessageConverter"/>  
  56.             list>  
  57.         property>  
  58.     bean>  
  59.     <bean id="mappingJacksonHttpMessageConverter"  
  60.           class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
  61.         <property name="supportedMediaTypes">  
  62.             <list>  
  63.                 <value>text/plain;charset=UTF-8value>  
  64.                 <value>application/json;charset=UTF-8value>  
  65.             list>  
  66.         property>  
  67.     bean>  
  68.       
  69.   
  70.       
  71.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
  72.           p:prefix="/WEB-INF/views/" p:suffix=".jsp"/>  
  73.   
  74.       
  75.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  76.         <property name="defaultEncoding" value="utf-8"/>  
  77.         <property name="maxUploadSize" value="10485760000"/>  
  78.         <property name="maxInMemorySize" value="40960"/>  
  79.     bean>  
  80.   
  81.       
  82.     <aop:config proxy-target-class="true">aop:config>  
  83.     <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">  
  84.         <property name="securityManager" ref="securityManager"/>  
  85.     bean>  
  86.   
  87. beans>  


messages.properties : hibernate-validator 配置文件,国际化资源文件

[plain]  view plain copy print ?
  1. #user  
  2. user.username.null=用户名不能为空  
  3. user.password.null=密码不能为空  

log4j.properties : 

[plain]  view plain copy print ?
  1. # DEBUG,INFO,WARN,ERROR,FATAL  
  2. LOG_LEVEL=INFO  
  3.   
  4. log4j.rootLogger=${LOG_LEVEL},CONSOLE,FILE  
  5.   
  6. log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender  
  7. log4j.appender.CONSOLE.Encoding=utf-8  
  8. log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout  
  9. #log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %C{8}@(%F:%L):%m%n   
  10. log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %C{1}@(%F:%L):%m%n  
  11.   
  12. log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender  
  13. log4j.appender.FILE.File=${catalina.base}/logs/quick4j.log  
  14. log4j.appender.FILE.Encoding=utf-8  
  15. log4j.appender.FILE.DatePattern='.'yyyy-MM-dd  
  16. log4j.appender.FILE.layout=org.apache.log4j.PatternLayout  
  17. #log4j.appender.FILE.layout=org.apache.log4j.HTMLLayout  
  18. log4j.appender.FILE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH\:mm\:ss} %C{8}@(%F\:%L)\:%m%n   

quick4j.sql

[sql]  view plain copy print ?
  1. /*  
  2. SQLyog 企业版 - MySQL GUI v8.14   
  3. MySQL - 5.5.27 : Database - quick4j  
  4. *********************************************************************  
  5. */  
  6.   
  7.   
  8. /*!40101 SET NAMES utf8 */;  
  9.   
  10. /*!40101 SET SQL_MODE=''*/;  
  11.   
  12. /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;  
  13. /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;  
  14. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;  
  15. /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;  
  16. CREATE DATABASE /*!32312 IF NOT EXISTS*/`quick4j` /*!40100 DEFAULT CHARACTER SET utf8 */;  
  17.   
  18. USE `quick4j`;  
  19.   
  20. /*Table structure for table `permission` */  
  21.   
  22. DROP TABLE IF EXISTS `permission`;  
  23.   
  24. CREATE TABLE `permission` (  
  25.   `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '权限id',  
  26.   `permission_name` varchar(32) DEFAULT NULL COMMENT '权限名',  
  27.   `permission_sign` varchar(128) DEFAULT NULL COMMENT '权限标识,程序中判断使用,如"user:create"',  
  28.   `description` varchar(256) DEFAULT NULL COMMENT '权限描述,UI界面显示使用',  
  29.   PRIMARY KEY (`id`)  
  30. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='权限表';  
  31.   
  32. /*Data for the table `permission` */  
  33.   
  34. insert  into `permission`(`id`,`permission_name`,`permission_sign`,`description`) values (1,'用户新增','user:create',NULL);  
  35.   
  36. /*Table structure for table `role` */  
  37.   
  38. DROP TABLE IF EXISTS `role`;  
  39.   
  40. CREATE TABLE `role` (  
  41.   `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '角色id',  
  42.   `role_name` varchar(32) DEFAULT NULL COMMENT '角色名',  
  43.   `role_sign` varchar(128) DEFAULT NULL COMMENT '角色标识,程序中判断使用,如"admin"',  
  44.   `description` varchar(256) DEFAULT NULL COMMENT '角色描述,UI界面显示使用',  
  45.   PRIMARY KEY (`id`)  
  46. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='角色表';  
  47.   
  48. /*Data for the table `role` */  
  49.   
  50. insert  into `role`(`id`,`role_name`,`role_sign`,`description`) values (1,'admin','admin','管理员');  
  51.   
  52. /*Table structure for table `role_permission` */  
  53.   
  54. DROP TABLE IF EXISTS `role_permission`;  
  55.   
  56. CREATE TABLE `role_permission` (  
  57.   `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '表id',  
  58.   `role_id` bigint(20) unsigned DEFAULT NULL COMMENT '角色id',  
  59.   `permission_id` bigint(20) unsigned DEFAULT NULL COMMENT '权限id',  
  60.   PRIMARY KEY (`id`)  
  61. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='角色与权限关联表';  
  62.   
  63. /*Data for the table `role_permission` */  
  64.   
  65. insert  into `role_permission`(`id`,`role_id`,`permission_id`) values (1,2,1);  
  66.   
  67. /*Table structure for table `user` */  
  68.   
  69. DROP TABLE IF EXISTS `user`;  
  70.   
  71. CREATE TABLE `user` (  
  72.   `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户id',  
  73.   `username` varchar(50) DEFAULT NULL COMMENT '用户名',  
  74.   `passwordchar(64) DEFAULT NULL COMMENT '密码',  
  75.   `state` varchar(32) DEFAULT NULL COMMENT '状态',  
  76.   `create_time` datetime DEFAULT NULL COMMENT '创建时间',  
  77.   PRIMARY KEY (`id`)  
  78. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='用户表';  
  79.   
  80. /*Data for the table `user` */  
  81.   
  82. insert  into `user`(`id`,`username`,`password`,`state`,`create_time`) values (1,'starzou','8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92',NULL,'2014-07-17 12:59:08');  
  83.   
  84. /*Table structure for table `user_role` */  
  85.   
  86. DROP TABLE IF EXISTS `user_role`;  
  87.   
  88. CREATE TABLE `user_role` (  
  89.   `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '表id',  
  90.   `user_id` bigint(20) unsigned DEFAULT NULL COMMENT '用户id',  
  91.   `role_id` bigint(20) unsigned DEFAULT NULL COMMENT '角色id',  
  92.   PRIMARY KEY (`id`)  
  93. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='用户与角色关联表';  
  94.   
  95. /*Data for the table `user_role` */  
  96.   

你可能感兴趣的:(实用技术总结)