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

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

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

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

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

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

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

看我们的项目结构:

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

是一个典型的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构建

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

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

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

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

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

  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.0</modelVersion>
  4. <groupId>com.eliteams</groupId>
  5. <artifactId>quick4j</artifactId>
  6. <packaging>war</packaging>
  7. <version>1.0.0</version>
  8. <name>quick4j App</name>
  9. <url>https://github.com/starzou/quick4j</url>
  10. <build>
  11. <finalName>quick4j</finalName>
  12. <plugins>
  13. <!-- Mybatis generator代码生成插件 配置 -->
  14. <plugin>
  15. <groupId>org.mybatis.generator</groupId>
  16. <artifactId>mybatis-generator-maven-plugin</artifactId>
  17. <version>${plugin.mybatis.generator}</version>
  18. <configuration>
  19. <configurationFile>${mybatis.generator.generatorConfig.xml}</configurationFile>
  20. <overwrite>true</overwrite>
  21. <verbose>true</verbose>
  22. </configuration>
  23. </plugin>
  24. <!--Maven编译插件 配置-->
  25. <plugin>
  26. <groupId>org.apache.maven.plugins</groupId>
  27. <artifactId>maven-compiler-plugin</artifactId>
  28. <version>${plugin.maven-compiler}</version>
  29. <configuration>
  30. <source>${project.build.jdk}</source>
  31. <target>${project.build.jdk}</target>
  32. <encoding>${project.build.sourceEncoding}</encoding>
  33. </configuration>
  34. </plugin>
  35. </plugins>
  36. <!--配置Maven 对resource文件 过滤 -->
  37. <resources>
  38. <resource>
  39. <directory>src/main/resources</directory>
  40. <includes>
  41. <include>**/*.properties</include>
  42. <include>**/*.xml</include>
  43. </includes>
  44. <filtering>true</filtering>
  45. </resource>
  46. <resource>
  47. <directory>src/main/java</directory>
  48. <includes>
  49. <include>**/*.properties</include>
  50. <include>**/*.xml</include>
  51. </includes>
  52. <filtering>true</filtering>
  53. </resource>
  54. </resources>
  55. </build>
  56. <properties>
  57. <!-- base setting -->
  58. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  59. <project.build.locales>zh_CN</project.build.locales>
  60. <project.build.jdk>1.7</project.build.jdk>
  61. <!-- plugin setting -->
  62. <mybatis.generator.generatorConfig.xml>${basedir}/src/test/resources/generatorConfig.xml</mybatis.generator.generatorConfig.xml>
  63. <mybatis.generator.generatorConfig.properties>file:///${basedir}/src/test/resources/generatorConfig.properties</mybatis.generator.generatorConfig.properties>
  64. <!-- plugin versions -->
  65. <plugin.mybatis.generator>1.3.1</plugin.mybatis.generator>
  66. <plugin.maven-compiler>3.1</plugin.maven-compiler>
  67. <!-- lib versions -->
  68. <junit.version>4.11</junit.version>
  69. <spring.version>4.0.2.RELEASE</spring.version>
  70. <mybatis.version>3.2.2</mybatis.version>
  71. <mybatis.spring.version>1.2.2</mybatis.spring.version>
  72. <mysql.connector.version>5.1.30</mysql.connector.version>
  73. <postgresql.version>9.1-901.jdbc4</postgresql.version>
  74. <slf4j.version>1.6.6</slf4j.version>
  75. <log4j.version>1.2.12</log4j.version>
  76. <httpclient.version>4.1.2</httpclient.version>
  77. <jackson.version>1.9.13</jackson.version>
  78. <c3p0.version>0.9.1.2</c3p0.version>
  79. <druid.version>1.0.5</druid.version>
  80. <tomcat.jdbc.version>7.0.53</tomcat.jdbc.version>
  81. <jstl.version>1.2</jstl.version>
  82. <google.collections.version>1.0</google.collections.version>
  83. <cglib.version>3.1</cglib.version>
  84. <shiro.version>1.2.3</shiro.version>
  85. <commons.fileupload.version>1.3.1</commons.fileupload.version>
  86. <commons.codec.version>1.9</commons.codec.version>
  87. <commons.net.version>3.3</commons.net.version>
  88. <aspectj.version>1.6.12</aspectj.version>
  89. <netty.version>4.0.18.Final</netty.version>
  90. <hibernate.validator.version>5.1.1.Final</hibernate.validator.version>
  91. </properties>
  92. <dependencies>
  93. <!-- junit -->
  94. <dependency>
  95. <groupId>junit</groupId>
  96. <artifactId>junit</artifactId>
  97. <version>${junit.version}</version>
  98. </dependency>
  99. <!-- springframe start -->
  100. <dependency>
  101. <groupId>org.springframework</groupId>
  102. <artifactId>spring-core</artifactId>
  103. <version>${spring.version}</version>
  104. </dependency>
  105. <dependency>
  106. <groupId>org.springframework</groupId>
  107. <artifactId>spring-web</artifactId>
  108. <version>${spring.version}</version>
  109. </dependency>
  110. <dependency>
  111. <groupId>org.springframework</groupId>
  112. <artifactId>spring-oxm</artifactId>
  113. <version>${spring.version}</version>
  114. </dependency>
  115. <dependency>
  116. <groupId>org.springframework</groupId>
  117. <artifactId>spring-tx</artifactId>
  118. <version>${spring.version}</version>
  119. </dependency>
  120. <dependency>
  121. <groupId>org.springframework</groupId>
  122. <artifactId>spring-jdbc</artifactId>
  123. <version>${spring.version}</version>
  124. </dependency>
  125. <dependency>
  126. <groupId>org.springframework</groupId>
  127. <artifactId>spring-webmvc</artifactId>
  128. <version>${spring.version}</version>
  129. </dependency>
  130. <dependency>
  131. <groupId>org.springframework</groupId>
  132. <artifactId>spring-aop</artifactId>
  133. <version>${spring.version}</version>
  134. </dependency>
  135. <dependency>
  136. <groupId>org.springframework</groupId>
  137. <artifactId>spring-context-support</artifactId>
  138. <version>${spring.version}</version>
  139. </dependency>
  140. <dependency>
  141. <groupId>org.springframework</groupId>
  142. <artifactId>spring-test</artifactId>
  143. <version>${spring.version}</version>
  144. </dependency>
  145. <!-- springframe end -->
  146. <!-- mybatis start-->
  147. <dependency>
  148. <groupId>org.mybatis</groupId>
  149. <artifactId>mybatis</artifactId>
  150. <version>${mybatis.version}</version>
  151. </dependency>
  152. <dependency>
  153. <groupId>org.mybatis</groupId>
  154. <artifactId>mybatis-spring</artifactId>
  155. <version>${mybatis.spring.version}</version>
  156. </dependency>
  157. <!--mybatis end-->
  158. <!-- mysql-connector -->
  159. <dependency>
  160. <groupId>mysql</groupId>
  161. <artifactId>mysql-connector-java</artifactId>
  162. <version>${mysql.connector.version}</version>
  163. </dependency>
  164. <!-- DruidDataSource -->
  165. <dependency>
  166. <groupId>com.alibaba</groupId>
  167. <artifactId>druid</artifactId>
  168. <version>${druid.version}</version>
  169. </dependency>
  170. <!-- jackson -->
  171. <dependency>
  172. <groupId>org.codehaus.jackson</groupId>
  173. <artifactId>jackson-mapper-asl</artifactId>
  174. <version>${jackson.version}</version>
  175. </dependency>
  176. <!-- log start -->
  177. <dependency>
  178. <groupId>log4j</groupId>
  179. <artifactId>log4j</artifactId>
  180. <version>${log4j.version}</version>
  181. </dependency>
  182. <dependency>
  183. <groupId>org.slf4j</groupId>
  184. <artifactId>slf4j-api</artifactId>
  185. <version>${slf4j.version}</version>
  186. </dependency>
  187. <dependency>
  188. <groupId>org.slf4j</groupId>
  189. <artifactId>slf4j-log4j12</artifactId>
  190. <version>${slf4j.version}</version>
  191. </dependency>
  192. <!-- log end -->
  193. <!-- servlet api -->
  194. <dependency>
  195. <groupId>javax.servlet</groupId>
  196. <artifactId>javax.servlet-api</artifactId>
  197. <version>3.0.1</version>
  198. <scope>provided</scope>
  199. </dependency>
  200. <!-- jstl -->
  201. <dependency>
  202. <groupId>javax.servlet</groupId>
  203. <artifactId>jstl</artifactId>
  204. <version>${jstl.version}</version>
  205. </dependency>
  206. <!-- start apache -->
  207. <dependency>
  208. <groupId>commons-fileupload</groupId>
  209. <artifactId>commons-fileupload</artifactId>
  210. <version>${commons.fileupload.version}</version>
  211. </dependency>
  212. <dependency>
  213. <groupId>org.apache.httpcomponents</groupId>
  214. <artifactId>httpclient</artifactId>
  215. <version>${httpclient.version}</version>
  216. </dependency>
  217. <dependency>
  218. <groupId>commons-codec</groupId>
  219. <artifactId>commons-codec</artifactId>
  220. <version>${commons.codec.version}</version>
  221. </dependency>
  222. <dependency>
  223. <groupId>commons-net</groupId>
  224. <artifactId>commons-net</artifactId>
  225. <version>${commons.net.version}</version>
  226. </dependency>
  227. <dependency>
  228. <groupId>commons-logging</groupId>
  229. <artifactId>commons-logging</artifactId>
  230. <version>1.1.3</version>
  231. </dependency>
  232. <dependency>
  233. <groupId>commons-collections</groupId>
  234. <artifactId>commons-collections</artifactId>
  235. <version>3.2.1</version>
  236. </dependency>
  237. <!-- end apache -->
  238. <!-- google -->
  239. <dependency>
  240. <groupId>com.google.collections</groupId>
  241. <artifactId>google-collections</artifactId>
  242. <version>${google.collections.version}</version>
  243. </dependency>
  244. <!-- cglib -->
  245. <dependency>
  246. <groupId>cglib</groupId>
  247. <artifactId>cglib-nodep</artifactId>
  248. <version>${cglib.version}</version>
  249. </dependency>
  250. <!-- shiro -->
  251. <dependency>
  252. <groupId>org.apache.shiro</groupId>
  253. <artifactId>shiro-spring</artifactId>
  254. <version>${shiro.version}</version>
  255. </dependency>
  256. <dependency>
  257. <groupId>org.apache.shiro</groupId>
  258. <artifactId>shiro-ehcache</artifactId>
  259. <version>${shiro.version}</version>
  260. </dependency>
  261. <dependency>
  262. <groupId>org.apache.shiro</groupId>
  263. <artifactId>shiro-core</artifactId>
  264. <version>${shiro.version}</version>
  265. </dependency>
  266. <dependency>
  267. <groupId>org.apache.shiro</groupId>
  268. <artifactId>shiro-web</artifactId>
  269. <version>${shiro.version}</version>
  270. </dependency>
  271. <dependency>
  272. <groupId>org.apache.shiro</groupId>
  273. <artifactId>shiro-quartz</artifactId>
  274. <version>${shiro.version}</version>
  275. </dependency>
  276. <!-- aspectjweaver -->
  277. <dependency>
  278. <groupId>org.aspectj</groupId>
  279. <artifactId>aspectjweaver</artifactId>
  280. <version>${aspectj.version}</version>
  281. </dependency>
  282. <dependency>
  283. <groupId>org.aspectj</groupId>
  284. <artifactId>aspectjrt</artifactId>
  285. <version>${aspectj.version}</version>
  286. </dependency>
  287. <!-- hibernate-validator -->
  288. <dependency>
  289. <groupId>org.hibernate</groupId>
  290. <artifactId>hibernate-validator</artifactId>
  291. <version>${hibernate.validator.version}</version>
  292. </dependency>
  293. <!-- netty -->
  294. <dependency>
  295. <groupId>io.netty</groupId>
  296. <artifactId>netty-all</artifactId>
  297. <version>${netty.version}</version>
  298. </dependency>
  299. <dependency>
  300. <groupId>org.mybatis.generator</groupId>
  301. <artifactId>mybatis-generator-core</artifactId>
  302. <version>1.3.2</version>
  303. <type>jar</type>
  304. <scope>test</scope>
  305. </dependency>
  306. </dependencies>
  307. </project>

3、配置web.xml

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

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

  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. <!-- Spring -->
  5. <!-- 配置Spring配置文件路径 -->
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>
  9. classpath*:applicationContext.xml
  10. classpath*:applicationContext-shiro.xml
  11. </param-value>
  12. </context-param>
  13. <!-- 配置Spring上下文监听器 -->
  14. <listener>
  15. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  16. </listener>
  17. <!-- Spring -->
  18. <!-- 配置Spring字符编码过滤器 -->
  19. <filter>
  20. <filter-name>encodingFilter</filter-name>
  21. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  22. <init-param>
  23. <param-name>encoding</param-name>
  24. <param-value>UTF-8</param-value>
  25. </init-param>
  26. <init-param>
  27. <param-name>forceEncoding</param-name>
  28. <param-value>true</param-value>
  29. </init-param>
  30. </filter>
  31. <filter-mapping>
  32. <filter-name>encodingFilter</filter-name>
  33. <url-pattern>/*</url-pattern>
  34. </filter-mapping>
  35. <!-- shiro 安全过滤器 -->
  36. <filter>
  37. <filter-name>shiroFilter</filter-name>
  38. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  39. <async-supported>true</async-supported>
  40. <init-param>
  41. <param-name>targetFilterLifecycle</param-name>
  42. <param-value>true</param-value>
  43. </init-param>
  44. </filter>
  45. <filter-mapping>
  46. <filter-name>shiroFilter</filter-name>
  47. <url-pattern>/*</url-pattern>
  48. </filter-mapping>
  49. <!-- 配置log4j配置文件路径 -->
  50. <context-param>
  51. <param-name>log4jConfigLocation</param-name>
  52. <param-value>classpath:log4j.properties</param-value>
  53. </context-param>
  54. <!-- 60s 检测日志配置 文件变化 -->
  55. <context-param>
  56. <param-name>log4jRefreshInterval</param-name>
  57. <param-value>60000</param-value>
  58. </context-param>
  59. <!-- 配置Log4j监听器 -->
  60. <listener>
  61. <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  62. </listener>
  63. <!-- Spring MVC 核心控制器 DispatcherServlet 配置 -->
  64. <servlet>
  65. <servlet-name>dispatcher</servlet-name>
  66. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  67. <init-param>
  68. <param-name>contextConfigLocation</param-name>
  69. <param-value>classpath*:spring-mvc.xml</param-value>
  70. </init-param>
  71. <load-on-startup>1</load-on-startup>
  72. </servlet>
  73. <servlet-mapping>
  74. <servlet-name>dispatcher</servlet-name>
  75. <!-- 拦截所有/rest/* 的请求,交给DispatcherServlet处理,性能最好 -->
  76. <url-pattern>/rest/*</url-pattern>
  77. </servlet-mapping>
  78. <!-- 首页 -->
  79. <welcome-file-list>
  80. <welcome-file>rest/index</welcome-file>
  81. </welcome-file-list>
  82. <!-- 错误页 -->
  83. <error-page>
  84. <error-code>404</error-code>
  85. <location>/rest/page/404</location>
  86. </error-page>
  87. <error-page>
  88. <error-code>500</error-code>
  89. <location>/rest/page/500</location>
  90. </error-page>
  91. <error-page>
  92. <exception-type>org.apache.shiro.authz.AuthorizationException</exception-type>
  93. <location>/rest/page/401</location>
  94. </error-page>
  95. </web-app>

4、spring配置:

applicationContext.xml

  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. <!-- 自动扫描quick4j包 ,将带有注解的类 纳入spring容器管理 -->
  23. <context:component-scan base-package="com.eliteams.quick4j"></context:component-scan>
  24. <!-- 引入配置文件 -->
  25. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  26. <property name="locations">
  27. <list>
  28. <value>classpath*:application.properties</value>
  29. </list>
  30. </property>
  31. </bean>
  32. <!-- dataSource 配置 -->
  33. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  34. <!-- 基本属性 url、user、password -->
  35. <property name="url" value="${jdbc.url}"/>
  36. <property name="username" value="${jdbc.username}"/>
  37. <property name="password" value="${jdbc.password}"/>
  38. <!-- 配置初始化大小、最小、最大 -->
  39. <property name="initialSize" value="${ds.initialSize}"/>
  40. <property name="minIdle" value="${ds.minIdle}"/>
  41. <property name="maxActive" value="${ds.maxActive}"/>
  42. <!-- 配置获取连接等待超时的时间 -->
  43. <property name="maxWait" value="${ds.maxWait}"/>
  44. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  45. <property name="timeBetweenEvictionRunsMillis" value="${ds.timeBetweenEvictionRunsMillis}"/>
  46. <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  47. <property name="minEvictableIdleTimeMillis" value="${ds.minEvictableIdleTimeMillis}"/>
  48. <property name="validationQuery" value="SELECT 'x'"/>
  49. <property name="testWhileIdle" value="true"/>
  50. <property name="testOnBorrow" value="false"/>
  51. <property name="testOnReturn" value="false"/>
  52. <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
  53. <property name="poolPreparedStatements" value="false"/>
  54. <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
  55. <!-- 配置监控统计拦截的filters -->
  56. <property name="filters" value="stat"/>
  57. </bean>
  58. <!-- mybatis文件配置,扫描所有mapper文件 -->
  59. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource"
  60. p:configLocation="classpath:mybatis-config.xml"
  61. p:mapperLocations="classpath:com/eliteams/quick4j/web/dao/*.xml"/>
  62. <!-- spring与mybatis整合配置,扫描所有dao -->
  63. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.eliteams.quick4j.web.dao"
  64. p:sqlSessionFactoryBeanName="sqlSessionFactory"/>
  65. <!-- 对dataSource 数据源进行事务管理 -->
  66. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
  67. p:dataSource-ref="dataSource"/>
  68. <!-- 事务管理 通知 -->
  69. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  70. <tx:attributes>
  71. <!-- 对insert,update,delete 开头的方法进行事务管理,只要有异常就回滚 -->
  72. <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>
  73. <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>
  74. <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>
  75. <!-- select,count开头的方法,开启只读,提高数据库访问性能 -->
  76. <tx:method name="select*" read-only="true"/>
  77. <tx:method name="count*" read-only="true"/>
  78. <!-- 对其他方法 使用默认的事务管理 -->
  79. <tx:method name="*"/>
  80. </tx:attributes>
  81. </tx:advice>
  82. <!-- 事务 aop 配置 -->
  83. <aop:config>
  84. <aop:pointcut id="serviceMethods" expression="execution(* com.eliteams.quick4j.web.service..*(..))"/>
  85. <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
  86. </aop:config>
  87. <!-- 配置使Spring采用CGLIB代理 -->
  88. <aop:aspectj-autoproxy proxy-target-class="true"/>
  89. <!-- 启用对事务注解的支持 -->
  90. <tx:annotation-driven transaction-manager="transactionManager"/>
  91. <!-- Cache配置 -->
  92. <cache:annotation-driven cache-manager="cacheManager"/>
  93. <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
  94. p:configLocation="classpath:ehcache.xml"/>
  95. <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
  96. p:cacheManager-ref="ehCacheManagerFactory"/>
  97. </beans>

application.properties

  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. ##DataSource Global Setting
  7. #配置初始化大小、最小、最大
  8. ds.initialSize=1
  9. ds.minIdle=1
  10. ds.maxActive=20
  11. #配置获取连接等待超时的时间
  12. ds.maxWait=60000
  13. #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
  14. ds.timeBetweenEvictionRunsMillis=60000
  15. #配置一个连接在池中最小生存的时间,单位是毫秒
  16. ds.minEvictableIdleTimeMillis=300000

ehcache.xml

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

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

  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. <description>apache shiro配置</description>
  8. <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  9. <property name="securityManager" ref="securityManager"/>
  10. <property name="loginUrl" value="/rest/page/login"/>
  11. <property name="successUrl" value="/rest/index"/>
  12. <property name="unauthorizedUrl" value="/rest/page/401"/>
  13. <property name="filterChainDefinitions">
  14. <value>
  15. <!-- 静态资源允许访问 -->
  16. /app/** = anon
  17. /assets/** = anon
  18. <!-- 登录页允许访问 -->
  19. /rest/user/login = anon
  20. <!-- 其他资源需要认证 -->
  21. /** = authc
  22. </value>
  23. </property>
  24. </bean>
  25. <!-- 缓存管理器 使用Ehcache实现 -->
  26. <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
  27. <property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml"/>
  28. </bean>
  29. <!-- 会话DAO -->
  30. <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.MemorySessionDAO"/>
  31. <!-- 会话管理器 -->
  32. <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
  33. <property name="sessionDAO" ref="sessionDAO"/>
  34. </bean>
  35. <!-- 安全管理器 -->
  36. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  37. <property name="realms">
  38. <list>
  39. <ref bean="securityRealm"/>
  40. </list>
  41. </property>
  42. <!-- cacheManager,集合spring缓存工厂 -->
  43. <!-- <property name="cacheManager" ref="shiroEhcacheManager" /> -->
  44. <!-- <property name="sessionManager" ref="sessionManager" /> -->
  45. </bean>
  46. <!-- Shiro生命周期处理器 -->
  47. <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
  48. </beans>

ehcache-shiro.xml

  1. <ehcache updateCheck="false" name="shiroCache">
  2. <defaultCache
  3. maxElementsInMemory="10000"
  4. eternal="false"
  5. timeToIdleSeconds="120"
  6. timeToLiveSeconds="120"
  7. overflowToDisk="false"
  8. diskPersistent="false"
  9. diskExpiryThreadIntervalSeconds="120"
  10. />
  11. </ehcache>

6、MyBatis 配置

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <properties>
  7. <property name="dialectClass" value="com.eliteams.quick4j.core.feature.orm.dialect.MySql5Dialect"/>
  8. </properties>
  9. <!-- 配置mybatis的缓存,延迟加载等等一系列属性 -->
  10. <settings>
  11. <!-- 全局映射器启用缓存 -->
  12. <setting name="cacheEnabled" value="true"/>
  13. <!-- 查询时,关闭关联对象即时加载以提高性能 -->
  14. <setting name="lazyLoadingEnabled" value="true"/>
  15. <!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->
  16. <setting name="multipleResultSetsEnabled" value="true"/>
  17. <!-- 允许使用列标签代替列名 -->
  18. <setting name="useColumnLabel" value="true"/>
  19. <!-- 不允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->
  20. <setting name="useGeneratedKeys" value="false"/>
  21. <!-- 给予被嵌套的resultMap以字段-属性的映射支持 FULL,PARTIAL -->
  22. <setting name="autoMappingBehavior" value="PARTIAL"/>
  23. <!-- 对于批量更新操作缓存SQL以提高性能 BATCH,SIMPLE -->
  24. <!-- <setting name="defaultExecutorType" value="BATCH" /> -->
  25. <!-- 数据库超过25000秒仍未响应则超时 -->
  26. <!-- <setting name="defaultStatementTimeout" value="25000" /> -->
  27. <!-- Allows using RowBounds on nested statements -->
  28. <setting name="safeRowBoundsEnabled" value="false"/>
  29. <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn. -->
  30. <setting name="mapUnderscoreToCamelCase" value="true"/>
  31. <!-- MyBatis uses local cache to prevent circular references and speed up repeated nested queries. By default (SESSION) all queries executed during a session are cached. If localCacheScope=STATEMENT
  32. local session will be used just for statement execution, no data will be shared between two different calls to the same SqlSession. -->
  33. <setting name="localCacheScope" value="SESSION"/>
  34. <!-- Specifies the JDBC type for null values when no specific JDBC type was provided for the parameter. Some drivers require specifying the column JDBC type but others work with generic values
  35. like NULL, VARCHAR or OTHER. -->
  36. <setting name="jdbcTypeForNull" value="OTHER"/>
  37. <!-- Specifies which Object's methods trigger a lazy load -->
  38. <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
  39. <!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->
  40. <setting name="aggressiveLazyLoading" value="false"/>
  41. </settings>
  42. <typeAliases>
  43. <package name="com.eliteams.quick4j.web.model"/>
  44. <package name="com.eliteams.quick4j.web.enums"/>
  45. </typeAliases>
  46. <plugins>
  47. <plugin interceptor="com.eliteams.quick4j.core.feature.orm.mybatis.PaginationResultSetHandlerInterceptor"/>
  48. <plugin interceptor="com.eliteams.quick4j.core.feature.orm.mybatis.PaginationStatementHandlerInterceptor"/>
  49. </plugins>
  50. </configuration>

7、Spring MVC 配置

  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. <!-- 扫描controller(controller层注入) -->
  15. <context:component-scan base-package="com.eliteams.quick4j.web.controller"/>
  16. <!-- 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的 -->
  17. <!-- 指定自己定义的validator -->
  18. <mvc:annotation-driven validator="validator"/>
  19. <!-- 以下 validator ConversionService 在使用 mvc:annotation-driven 会 自动注册 -->
  20. <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
  21. <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
  22. <!-- 如果不加默认到 使用classpath下的 ValidationMessages.properties -->
  23. <property name="validationMessageSource" ref="messageSource"/>
  24. </bean>
  25. <!-- 国际化的消息资源文件(本系统中主要用于显示/错误消息定制) -->
  26. <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  27. <property name="basenames">
  28. <list>
  29. <!-- 在web环境中一定要定位到classpath 否则默认到当前web应用下找 -->
  30. <value>classpath:messages</value>
  31. <value>classpath:org/hibernate/validator/ValidationMessages</value>
  32. </list>
  33. </property>
  34. <property name="useCodeAsDefaultMessage" value="false"/>
  35. <property name="defaultEncoding" value="UTF-8"/>
  36. <property name="cacheSeconds" value="60"/>
  37. </bean>
  38. <mvc:interceptors>
  39. <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
  40. </mvc:interceptors>
  41. <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
  42. <property name="defaultLocale" value="zh_CN"/>
  43. </bean>
  44. <!-- 支持返回json(避免IE在ajax请求时,返回json出现下载 ) -->
  45. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  46. <property name="messageConverters">
  47. <list>
  48. <ref bean="mappingJacksonHttpMessageConverter"/>
  49. </list>
  50. </property>
  51. </bean>
  52. <bean id="mappingJacksonHttpMessageConverter"
  53. class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
  54. <property name="supportedMediaTypes">
  55. <list>
  56. <value>text/plain;charset=UTF-8</value>
  57. <value>application/json;charset=UTF-8</value>
  58. </list>
  59. </property>
  60. </bean>
  61. <!-- 支持返回json -->
  62. <!-- 对模型视图添加前后缀 -->
  63. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  64. p:prefix="/WEB-INF/views/" p:suffix=".jsp"/>
  65. <!-- 配置springMVC处理上传文件的信息 -->
  66. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  67. <property name="defaultEncoding" value="utf-8"/>
  68. <property name="maxUploadSize" value="10485760000"/>
  69. <property name="maxInMemorySize" value="40960"/>
  70. </bean>
  71. <!-- 启用shrio授权注解拦截方式 -->
  72. <aop:config proxy-target-class="true"></aop:config>
  73. <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
  74. <property name="securityManager" ref="securityManager"/>
  75. </bean>
  76. </beans>

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

  1. #user
  2. user.username.null=用户名不能为空
  3. user.password.null=密码不能为空

log4j.properties :

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

quick4j.sql

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


下一篇:[杂记]PostgreSQL命令行使用