版权声明:本文为博主原创文章,未经博主允许不得转载。

 
 

利用STS(Spring Tool Suite)开发工具可以很方便的创建一个基于Maven的Spring MVC工程项目。然后再分别集成Freemarker以及Mybatis。 
STS官网下载地址:点此下载Spring Tool Suite 运行STS后, 
设置下工作空间的默认编码方式为UTF-8,Window->Preferences->General->Workspace。 
配置下JRE,选择你自己安装的JDK(如1.8的版本),Window->Preferences->Java->Installed JREs 。 
配置Maven本地仓库位置,默认是C盘路径,Window->Preferences->Maven->User Settings,可以自己安装个Maven,若嫌麻烦则默认用STS自带的即可不用配置。

1.创建一个Spring MVC项目

File->New->Spring Legacy Project,弹出向导后输入项目名称并选择Spring MVC Project,然后Next, 
输入包名,Finish。第一次创建会提示下载模板文件,点下载即可,创建好的工程目录如下: 
 
运行项目Run as -> Run on Server ,可以用Tomcat 作为服务器,自己自行配置。如果项目有错误,请检查Build Path 以及pom.xml中的jar包是否在maven本地仓库中下载好了。 
运行成功后,会打印出Hello world 以及服务器的时间,时间若是乱码,在jsp中 加上charset=utf-8即可。

2.集成Freemarker

表现层技术主要有三种:jsp、freemarker、velocity。 
其实集成Freemarker是可选的,jsp就很好了。三者优缺点都有,看个人喜好吧。 
1)在pom.xml中添加Freemarker 依赖:

  1. <!-- freemarker 依赖于 spring-context-support-->
  2. <dependency>
  3. <groupId>org.freemarker</groupId>
  4. <artifactId>freemarker</artifactId>
  5. <version>2.3.23</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework</groupId>
  9. <artifactId>spring-context-support</artifactId>
  10. </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2)找到servlet-context.xml,添加Freemarker的配置 
为了看起来更简洁,beans和mvc的命名空间前缀调换了下,自己可以对照着更改。 
文件在最后列出来了,在此不重复列出

Freemarker配置中的视图后缀可以是任意的,默认的是.ftl . 
如果要配置多个视图解析器,如同时配置jsp和Freemarker,那么Freemarker的优先级要设置的比jsp的高,通过order来设置。 
很简单,就这两步,配置完后,运行成功则表示Freemarker配置成功。

3.集成Mybatis

实体映射框架可能会选择Hibernate,个人觉得Hibernate难学点,特别是那些对应关系。而Mybatis是半自动映射框架,灵活性高,学起来也简单。 
1)依赖jar

  1. <!-- mybatis -->
  2. <dependency>
  3. <groupId>org.mybatis</groupId>
  4. <artifactId>mybatis</artifactId>
  5. <version>3.2.6</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.mybatis</groupId>
  9. <artifactId>mybatis-spring</artifactId>
  10. <version>1.2.2</version>
  11. </dependency>
  12. <!-- microsoft jdbc -->
  13. <dependency>
  14. <groupId>net.sourceforge.jtds</groupId>
  15. <artifactId>jtds</artifactId>
  16. <version>1.3.1</version>
  17. </dependency>
  18. <!-- 事务 -->
  19. <dependency>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>spring-jdbc</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-tx</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.aspectj</groupId>
  29. <artifactId>aspectjweaver</artifactId>
  30. <version>${org.aspectj-version}</version>
  31. </dependency>
  32. <!-- 导入dbcp的jar包 -->
  33. <dependency>
  34. <groupId>commons-dbcp</groupId>
  35. <artifactId>commons-dbcp</artifactId>
  36. <version>1.4</version>
  37. </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

驱动是Sqlserver的,自己可以替换成其他的 
2)jdbc.properties

  1. hibernate.dialect=org.hibernate.dialect.SQLServerDialect
  2. driverClassName=net.sourceforge.jtds.jdbc.Driver
  3. validationQuery=SELECT 1
  4. jdbc_url=jdbc:jtds:sqlserver://127.0.0.1:1433/PTMPDB
  5. jdbc_password=12345
  6. jdbc_username=part_user
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3)spring-mybatis.xml 
文件在后面列举出来了,在此不重复列举

4)最后别忘了将spring-mytabis.xml 让spring容器加载进去,在web.xml中加入:

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>/WEB-INF/spring/root-context.xml,classpath:spring-mybatis.xml</param-value>
  4. </context-param>
  • 1
  • 2
  • 3
  • 4

至此配置已经完成了,最后还得测试下是否集成成功。

4.Mybatis自动生成工具

可以根据数据库表自动生成dao、mapping、entity。Mybatis实体映射自动生成工具下载 
运行项目,看是否能成功连接数据库并且能查出数据以验证是否配置成功。 
 
1)pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.jykj</groupId>
  6. <artifactId>demo</artifactId>
  7. <name>Demo</name>
  8. <packaging>war</packaging>
  9. <version>1.0.0-BUILD-SNAPSHOT</version>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <java-version>1.6</java-version>
  13. <org.springframework-version>4.1.2.RELEASE</org.springframework-version>
  14. <org.aspectj-version>1.6.10</org.aspectj-version>
  15. <org.slf4j-version>1.6.6</org.slf4j-version>
  16. </properties>
  17. <dependencyManagement>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>spring-framework-bom</artifactId>
  22. <version>${org.springframework-version}</version>
  23. <type>pom</type>
  24. <scope>import</scope>
  25. </dependency>
  26. </dependencies>
  27. </dependencyManagement>
  28. <dependencies>
  29. <!-- Spring -->
  30. <dependency>
  31. <groupId>org.springframework</groupId>
  32. <artifactId>spring-context</artifactId>
  33. <exclusions>
  34. <!-- Exclude Commons Logging in favor of SLF4j -->
  35. <exclusion>
  36. <groupId>commons-logging</groupId>
  37. <artifactId>commons-logging</artifactId>
  38. </exclusion>
  39. </exclusions>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework</groupId>
  43. <artifactId>spring-webmvc</artifactId>
  44. </dependency>
  45. <!-- AspectJ -->
  46. <dependency>
  47. <groupId>org.aspectj</groupId>
  48. <artifactId>aspectjrt</artifactId>
  49. <version>${org.aspectj-version}</version>
  50. </dependency>
  51. <!-- Logging -->
  52. <dependency>
  53. <groupId>org.slf4j</groupId>
  54. <artifactId>slf4j-api</artifactId>
  55. <version>${org.slf4j-version}</version>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.slf4j</groupId>
  59. <artifactId>jcl-over-slf4j</artifactId>
  60. <version>${org.slf4j-version}</version>
  61. <scope>runtime</scope>
  62. </dependency>
  63. <dependency>
  64. <groupId>org.slf4j</groupId>
  65. <artifactId>slf4j-log4j12</artifactId>
  66. <version>${org.slf4j-version}</version>
  67. <scope>runtime</scope>
  68. </dependency>
  69. <dependency>
  70. <groupId>log4j</groupId>
  71. <artifactId>log4j</artifactId>
  72. <version>1.2.15</version>
  73. <exclusions>
  74. <exclusion>
  75. <groupId>javax.mail</groupId>
  76. <artifactId>mail</artifactId>
  77. </exclusion>
  78. <exclusion>
  79. <groupId>javax.jms</groupId>
  80. <artifactId>jms</artifactId>
  81. </exclusion>
  82. <exclusion>
  83. <groupId>com.sun.jdmk</groupId>
  84. <artifactId>jmxtools</artifactId>
  85. </exclusion>
  86. <exclusion>
  87. <groupId>com.sun.jmx</groupId>
  88. <artifactId>jmxri</artifactId>
  89. </exclusion>
  90. </exclusions>
  91. <scope>runtime</scope>
  92. </dependency>
  93. <!-- @Inject -->
  94. <dependency>
  95. <groupId>javax.inject</groupId>
  96. <artifactId>javax.inject</artifactId>
  97. <version>1</version>
  98. </dependency>
  99. <!-- Servlet -->
  100. <dependency>
  101. <groupId>javax.servlet</groupId>
  102. <artifactId>servlet-api</artifactId>
  103. <version>2.5</version>
  104. <scope>provided</scope>
  105. </dependency>
  106. <dependency>
  107. <groupId>javax.servlet.jsp</groupId>
  108. <artifactId>jsp-api</artifactId>
  109. <version>2.1</version>
  110. <scope>provided</scope>
  111. </dependency>
  112. <dependency>
  113. <groupId>javax.servlet</groupId>
  114. <artifactId>jstl</artifactId>
  115. <version>1.2</version>
  116. </dependency>
  117. <!-- Test -->
  118. <dependency>
  119. <groupId>junit</groupId>
  120. <artifactId>junit</artifactId>
  121. <version>4.7</version>
  122. <scope>test</scope>
  123. </dependency>
  124. <!-- freemarker 依赖于 spring-context-support-->
  125. <dependency>
  126. <groupId>org.freemarker</groupId>
  127. <artifactId>freemarker</artifactId>
  128. <version>2.3.23</version>
  129. </dependency>
  130. <dependency>
  131. <groupId>org.springframework</groupId>
  132. <artifactId>spring-context-support</artifactId>
  133. </dependency>
  134. <!-- mybatis -->
  135. <dependency>
  136. <groupId>org.mybatis</groupId>
  137. <artifactId>mybatis</artifactId>
  138. <version>3.2.6</version>
  139. </dependency>
  140. <dependency>
  141. <groupId>org.mybatis</groupId>
  142. <artifactId>mybatis-spring</artifactId>
  143. <version>1.2.2</version>
  144. </dependency>
  145. <!-- microsoft jdbc -->
  146. <dependency>
  147. <groupId>net.sourceforge.jtds</groupId>
  148. <artifactId>jtds</artifactId>
  149. <version>1.3.1</version>
  150. </dependency>
  151. <!-- 事务 -->
  152. <dependency>
  153. <groupId>org.springframework</groupId>
  154. <artifactId>spring-jdbc</artifactId>
  155. </dependency>
  156. <dependency>
  157. <groupId>org.springframework</groupId>
  158. <artifactId>spring-tx</artifactId>
  159. </dependency>
  160. <dependency>
  161. <groupId>org.aspectj</groupId>
  162. <artifactId>aspectjweaver</artifactId>
  163. <version>${org.aspectj-version}</version>
  164. </dependency>
  165. <!-- 导入dbcp的jar包 -->
  166. <dependency>
  167. <groupId>commons-dbcp</groupId>
  168. <artifactId>commons-dbcp</artifactId>
  169. <version>1.4</version>
  170. </dependency>
  171. </dependencies>
  172. <build>
  173. <plugins>
  174. <plugin>
  175. <artifactId>maven-eclipse-plugin</artifactId>
  176. <version>2.9</version>
  177. <configuration>
  178. <additionalProjectnatures>
  179. <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
  180. </additionalProjectnatures>
  181. <additionalBuildcommands>
  182. <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
  183. </additionalBuildcommands>
  184. <downloadSources>true</downloadSources>
  185. <downloadJavadocs>true</downloadJavadocs>
  186. </configuration>
  187. </plugin>
  188. <plugin>
  189. <groupId>org.apache.maven.plugins</groupId>
  190. <artifactId>maven-compiler-plugin</artifactId>
  191. <version>2.5.1</version>
  192. <configuration>
  193. <source>1.6</source>
  194. <target>1.6</target>
  195. <compilerArgument>-Xlint:all</compilerArgument>
  196. <showWarnings>true</showWarnings>
  197. <showDeprecation>true</showDeprecation>
  198. </configuration>
  199. </plugin>
  200. <plugin>
  201. <groupId>org.codehaus.mojo</groupId>
  202. <artifactId>exec-maven-plugin</artifactId>
  203. <version>1.2.1</version>
  204. <configuration>
  205. <mainClass>org.test.int1.Main</mainClass>
  206. </configuration>
  207. </plugin>
  208. </plugins>
  209. </build>
  210. </project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216

2)web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  5. <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>/WEB-INF/spring/root-context.xml,classpath:spring-mybatis.xml</param-value>
  9. </context-param>
  10. <!-- Creates the Spring Container shared by all Servlets and Filters -->
  11. <listener>
  12. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  13. </listener>
  14. <!-- Processes application requests -->
  15. <servlet>
  16. <servlet-name>appServlet</servlet-name>
  17. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  18. <init-param>
  19. <param-name>contextConfigLocation</param-name>
  20. <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
  21. </init-param>
  22. <load-on-startup>1</load-on-startup>
  23. </servlet>
  24. <servlet-mapping>
  25. <servlet-name>appServlet</servlet-name>
  26. <url-pattern>/</url-pattern>
  27. </servlet-mapping>
  28. <!-- 编码格式 -->
  29. <filter>
  30. <filter-name>encodingFilter</filter-name>
  31. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  32. <init-param>
  33. <param-name>encoding</param-name>
  34. <param-value>UTF-8</param-value>
  35. </init-param>
  36. <init-param>
  37. <param-name>forceEncoding</param-name>
  38. <param-value>true</param-value>
  39. </init-param>
  40. </filter>
  41. <filter-mapping>
  42. <filter-name>encodingFilter</filter-name>
  43. <url-pattern>/*</url-pattern>
  44. </filter-mapping>
  45. <!-- 日志配置文件 -->
  46. <context-param>
  47. <param-name>log4jConfigLocation</param-name>
  48. <param-value>classpath:log4j.xml</param-value>
  49. </context-param>
  50. <listener>
  51. <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  52. </listener>
  53. </web-app>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

3)servlet-context.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns:mvc="http://www.springframework.org/schema/mvc"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns="http://www.springframework.org/schema/beans"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  9. <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
  10. <!-- Enables the Spring MVC @Controller programming model -->
  11. <mvc:annotation-driven />
  12. <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
  13. <mvc:resources mapping="/resources/**" location="/resources/" />
  14. <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
  15. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  16. <property name="prefix" value="/WEB-INF/views/" />
  17. <property name="suffix" value=".jsp" />
  18. <property name="order" value="2" />
  19. </bean>
  20. <!-- freemarker的配置 -->
  21. <bean id="freemarkerConfigurer"
  22. class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  23. <property name="templateLoaderPath" value="/WEB-INF/views/" />
  24. <property name="defaultEncoding" value="utf-8" />
  25. <property name="freemarkerSettings">
  26. <props>
  27. <prop key="template_update_delay">5</prop>
  28. <prop key="default_encoding">UTF-8</prop>
  29. <!-- <prop key="locale">UTF-8</prop> -->
  30. <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
  31. <prop key="time_format">HH:mm:ss</prop>
  32. <prop key="number_format">0.####</prop>
  33. <prop key="boolean_format">true,false</prop>
  34. <prop key="whitespace_stripping">true</prop>
  35. <prop key="tag_syntax">auto_detect</prop>
  36. <prop key="url_escaping_charset">UTF-8</prop>
  37. </props>
  38. </property>
  39. </bean>
  40. <!-- FreeMarker视图解析 ,优先级要高于默认的jsp-->
  41. <bean id="viewResolver"
  42. class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
  43. <property name="order" value="1" />
  44. <property name="cache" value="true" />
  45. <property name="prefix" value="" />
  46. <property name="suffix" value=".ftl" />
  47. <property name="contentType" value="text/html;charset=UTF-8"></property>
  48. <property name="requestContextAttribute" value="request" />
  49. <property name="exposeSpringMacroHelpers" value="true" />
  50. <property name="exposeRequestAttributes" value="true" />
  51. <property name="exposeSessionAttributes" value="true" />
  52. </bean>
  53. <!-- 控制器 -->
  54. <context:component-scan base-package="com.jykj.demo.controller" />
  55. <!-- service -->
  56. <context:component-scan base-package="com.jykj.demo.service" />
  57. </beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

4)spring-mybatis.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/tx
  9. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  14. ">
  15. <!--
  16. <context:component-scan base-package="com.jykj.demo" /> -->
  17. <!-- 引入配置文件 -->
  18. <bean id="propertyConfigurer"
  19. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  20. <property name="location" value="classpath:jdbc.properties" />
  21. </bean>
  22. <!-- 配置数据源 -->
  23. <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  24. destroy-method="close">
  25. <property name="driverClassName" value="${driverClassName}"/>
  26. <property name="url" value="${jdbc_url}" />
  27. <property name="username" value="${jdbc_username}" />
  28. <property name="password" value="${jdbc_password}" />
  29. <!-- 初始化连接大小 -->
  30. <property name="initialSize" value="0" />
  31. <!-- 连接池最大使用连接数量 -->
  32. <property name="maxActive" value="20" />
  33. <!-- 连接池最小空闲 -->
  34. <property name="minIdle" value="0" />
  35. <!-- 获取连接最大等待时间 -->
  36. <property name="maxWait" value="60000" />
  37. <property name="poolPreparedStatements" value="true" />
  38. <!-- <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
  39. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  40. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  41. <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  42. <property name="minEvictableIdleTimeMillis" value="25200000" />
  43. <!-- 打开removeAbandoned功能 -->
  44. <property name="removeAbandoned" value="true" />
  45. <!-- 1800秒,也就是30分钟 -->
  46. <property name="removeAbandonedTimeout" value="1800" />
  47. <!-- 关闭abanded连接时输出错误日志 -->
  48. <property name="logAbandoned" value="true" />
  49. <!-- 监控数据库 -->
  50. <!-- <property name="filters" value="mergeStat" /> -->
  51. </bean>
  52. <!-- myBatis文件 -->
  53. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  54. <property name="dataSource" ref="dataSource" />
  55. <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
  56. <property name="mapperLocations" value="classpath:com/jykj/demo/mapping/*.xml" />
  57. <property name="typeAliasesPackage" value="com.jykj.demo.entity" />
  58. </bean>
  59. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  60. <property name="basePackage" value="com.jykj.demo.dao" />
  61. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
  62. </bean>
  63. <!-- 配置事务管理器 -->
  64. <bean id="transactionManager"
  65. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  66. <property name="dataSource" ref="dataSource" />
  67. </bean>
  68. <!-- 注解方式配置事物 -->
  69. <!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
  70. <!-- 拦截器方式配置事物 -->
  71. <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
  72. <tx:attributes>
  73. <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
  74. <tx:method name="append*" propagation="REQUIRED"
  75. rollback-for="Exception" />
  76. <tx:method name="insert*" propagation="REQUIRED"
  77. rollback-for="Exception" />
  78. <tx:method name="save*" propagation="REQUIRED"
  79. rollback-for="Exception" />
  80. <tx:method name="update*" propagation="REQUIRED"
  81. rollback-for="Exception" />
  82. <tx:method name="modify*" propagation="REQUIRED"
  83. rollback-for="Exception" />
  84. <tx:method name="edit*" propagation="REQUIRED"
  85. rollback-for="Exception" />
  86. <tx:method name="delete*" propagation="REQUIRED"
  87. rollback-for="Exception" />
  88. <tx:method name="remove*" propagation="REQUIRED"
  89. rollback-for="Exception" />
  90. <tx:method name="repair" propagation="REQUIRED"
  91. rollback-for="Exception" />
  92. <tx:method name="delAndRepair" propagation="REQUIRED"
  93. rollback-for="Exception" />
  94. <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
  95. <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
  96. <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
  97. <tx:method name="load*" propagation="SUPPORTS" read-only="true" />
  98. <tx:method name="search*" propagation="SUPPORTS" read-only="true" />
  99. <tx:method name="datagrid*" propagation="SUPPORTS"
  100. read-only="true" />
  101. <tx:method name="*" propagation="SUPPORTS" rollback-for="Exception" />
  102. </tx:attributes>
  103. </tx:advice>
  104. <!-- 切面注入事务 -->
  105. <aop:config>
  106. <aop:pointcut id="transactionPointcut" expression="execution(* com.jykj.demo.service.*.*(..))" />
  107. <aop:advisor pointcut-ref="transactionPointcut"
  108. advice-ref="transactionAdvice" />
  109. </aop:config>
  110. </beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123

总的来说,集成的话就是spring的配置。如果集成出现问题,请不要灰心,找出原因,多半是pom.xml中的jar包没有成功下载下来,这样的话需要手动下载后放到maven本地仓库中。 
也可以参考其他人的博客: 
参考链接一 
参考链接二 
参考链接三 
Mybatis实体映射自动生成工具下载

Spring MVC+Maven+Freemarker+Mybatis开发环境搭建的更多相关文章

  1. Spring学习之第一个Spring MVC程序(IDEA开发环境)

    回顾Java平台上Web开发历程来看,从Servlet出现开始,到JSP繁盛一时,然后是Servlet+JSP时代,最后演化为现在Web开发框架盛行的时代.一般接触到一个新的Web框架,都会想问这个框 ...

  2. 精尽Spring MVC源码分析 - 调式环境搭建

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  3. Maven+SpringMVC+Mybatis 开发环境整合

    1.maven build遇到了如下问题:  [ERROR] Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:rede ...

  4. mybatis 开发环境搭建

    不说废话直接上代码,首先看下我的目录机构: 红色部分,表明你所需的jar包,已经配置文件. 创建用户表,以及插入数据. create table books(id int (11) not null ...

  5. Hadoop项目开发环境搭建(Eclipse\MyEclipse + Maven)

    写在前面的话 可详细参考,一定得去看 HBase 开发环境搭建(Eclipse\MyEclipse + Maven) Zookeeper项目开发环境搭建(Eclipse\MyEclipse + Mav ...

  6. 转载 Elasticsearch开发环境搭建(Eclipse\MyEclipse + Maven)

    概要: 1.使用Eclipse搭建Elasticsearch详情参考下面链接 2.Java Elasticsearch 配置 3.ElasticSearch Java Api(一) -添加数据创建索引 ...

  7. Elasticsearch开发环境搭建(Eclipse\MyEclipse + Maven)

    前提是, Elasticsearch 编程API入门系列---说在前面的话 Eclipse下Maven新建项目.自动打依赖jar包(包含普通项目和Web项目) setting.xml配置文件 如何在M ...

  8. 基于Maven的Spring + Spring MVC + Mybatis的环境搭建

    基于Maven的Spring + Spring MVC + Mybatis的环境搭建项目开发,先将环境先搭建起来.上次做了一个Spring + Spring MVC + Mybatis + Log4J ...

  9. 【转载】Maven+druid+MyBatis+Spring+Oracle+Dubbo开发环境搭建

    原地址:http://blog.csdn.net/wp1603710463/article/details/48247817#t16 Maven+druid+MyBatis+spring+Oracle ...

随机推荐

  1. C#同一位置切换显示两个Panel内容

    如果两个panel重合在一起,点击不同按钮切换显示不同的panel,需要xxx.BringToFront(); 1.首先让两个panel的visible都为false, 在加载页面load方法里可以让 ...

  2. 1.精通前端系列技术之js正则表达式

    在不会正则的时候,我们寻找字符串某些规律或者截取部分特殊字符的时候,我们需要写很多行代码来获取我们想要的字符串,在使用正则之后,代码量会大量简洁很多 1.字符串的比较,判断是否数字类型的字符串,我们用 ...

  3. Android设计画面中有EditText时取消启动时自动获得焦点调用系统输入法的方法

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  4. 如何利用SVN合并代码

    一.  背景     平时在进行开发时,一般都会有多版本同时进行,包括项目版本.周版本.紧急版本等,当某一个版本具备上线条件后,需要在上一个已发布的版本基础上进行发布,才能够避免出现版本相互覆盖,因此 ...

  5. c++形参和实参同名时,如何单步执行观察形参的变化。

    c++形参和实参同名时,如何单步执行观察形参的变化? 方法:当程序运行到函数中时,添加变量观察即可.

  6. 解决:Ubuntu12.04下使用ping命令返回ping:icmp open socket: Operation not permitted的解决

    ping命令在运行中采用了ICMP协议,需要发送ICMP报文.但是只有root用户才能建立ICMP报文.而正常情况下,ping命令的权限应为-rwsr-xr-x,即带有suid的文件,一旦该权限被修改 ...

  7. Interview----将一棵二叉树转换成其镜像

    题目:输入一颗二元查找树,将该树转换为它的镜像, 即在转换后的二元查找树中,左子树的结点都大于右子树的结点. 用递归和循环两种方法完成树的镜像转换. 例如输入:        8      /    ...

  8. PHP流程控制分支结构

    1.顺序结构2.分支结构(条件结构.选择结构)    (1)单路分支        //条件bool,ture或false        if(条件){            执行语句:       ...

  9. js实现图片预显示

    html页面代码 <div id="localImag" style="display:none"><img  id="previe ...

  10. 【LEETCODE OJ】Copy List with Random Pointer

    Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...