Java Spring SSM

项目背景

  1. 支持Restful风格的请求
  2. 支持PageHelper分页插件
  3. 支持Mybatis逆向工程配置文件
  4. 使用阿里Druid管理数据源

    1、在IDEA中创建新的Maven项目

    image.png

image.png

image.png
image.png

2、添加项目相关依赖jar包

A.使用Maven管理jar包

  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. <maven.compiler.source>1.8</maven.compiler.source>
  4. <maven.compiler.target>1.8</maven.compiler.target>
  5. <spring.version>5.1.8.RELEASE</spring.version>
  6. </properties>
  7. <dependencies>
  8. <dependency>
  9. <groupId>junit</groupId>
  10. <artifactId>junit</artifactId>
  11. <version>4.12</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>commons-logging</groupId>
  15. <artifactId>commons-logging</artifactId>
  16. <version>1.2</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>commons-fileupload</groupId>
  20. <artifactId>commons-fileupload</artifactId>
  21. <version>1.4</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-test</artifactId>
  26. <version>${spring.version}</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring-beans</artifactId>
  31. <version>${spring.version}</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework</groupId>
  35. <artifactId>spring-context</artifactId>
  36. <version>${spring.version}</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework</groupId>
  40. <artifactId>spring-core</artifactId>
  41. <version>${spring.version}</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework</groupId>
  45. <artifactId>spring-expression</artifactId>
  46. <version>${spring.version}</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.springframework</groupId>
  50. <artifactId>spring-tx</artifactId>
  51. <version>${spring.version}</version>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.springframework</groupId>
  55. <artifactId>spring-web</artifactId>
  56. <version>${spring.version}</version>
  57. </dependency>
  58. <dependency>
  59. <groupId>org.springframework</groupId>
  60. <artifactId>spring-webmvc</artifactId>
  61. <version>${spring.version}</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>org.springframework</groupId>
  65. <artifactId>spring-aspects</artifactId>
  66. <version>${spring.version}</version>
  67. </dependency>
  68. <dependency>
  69. <groupId>org.springframework</groupId>
  70. <artifactId>spring-jdbc</artifactId>
  71. <version>${spring.version}</version>
  72. </dependency>
  73. <!-- AOP相关的jar包依赖-->
  74. <dependency>
  75. <groupId>org.springframework</groupId>
  76. <artifactId>spring-aop</artifactId>
  77. <version>${spring.version}</version>
  78. </dependency>
  79. <dependency>
  80. <groupId>aopalliance</groupId>
  81. <artifactId>aopalliance</artifactId>
  82. <version>1.0</version>
  83. </dependency>
  84. <dependency>
  85. <groupId>org.aspectj</groupId>
  86. <artifactId>aspectjweaver</artifactId>
  87. <version>1.9.2</version>
  88. </dependency>
  89. <dependency>
  90. <groupId>mysql</groupId>
  91. <artifactId>mysql-connector-java</artifactId>
  92. <version>8.0.13</version>
  93. </dependency>
  94. <dependency>
  95. <groupId>org.projectlombok</groupId>
  96. <artifactId>lombok</artifactId>
  97. <version>1.18.4</version>
  98. </dependency>
  99. <!--Mybatis相关的jar包-->
  100. <dependency>
  101. <groupId>org.apache.ant</groupId>
  102. <artifactId>ant</artifactId>
  103. <version>1.9.6</version>
  104. </dependency>
  105. <dependency>
  106. <groupId>org.apache.ant</groupId>
  107. <artifactId>ant-launcher</artifactId>
  108. <version>1.9.6</version>
  109. </dependency>
  110. <dependency>
  111. <groupId>asm</groupId>
  112. <artifactId>asm</artifactId>
  113. <version>3.3.1</version>
  114. </dependency>
  115. <dependency>
  116. <groupId>cglib</groupId>
  117. <artifactId>cglib</artifactId>
  118. <version>3.2.6</version>
  119. </dependency>
  120. <dependency>
  121. <groupId>javassist</groupId>
  122. <artifactId>javassist</artifactId>
  123. <version>3.11.0.GA</version>
  124. </dependency>
  125. <dependency>
  126. <groupId>log4j</groupId>
  127. <artifactId>log4j</artifactId>
  128. <version>1.2.17</version>
  129. </dependency>
  130. <dependency>
  131. <groupId>org.apache.logging.log4j</groupId>
  132. <artifactId>log4j-api</artifactId>
  133. <version>2.11.1</version>
  134. </dependency>
  135. <dependency>
  136. <groupId>org.apache.logging.log4j</groupId>
  137. <artifactId>log4j-core</artifactId>
  138. <version>2.11.0</version>
  139. </dependency>
  140. <dependency>
  141. <groupId>org.mybatis</groupId>
  142. <artifactId>mybatis</artifactId>
  143. <version>3.4.6</version>
  144. </dependency>
  145. <dependency>
  146. <groupId>org.mybatis.generator</groupId>
  147. <artifactId>mybatis-generator-core</artifactId>
  148. <version>1.3.7</version>
  149. </dependency>
  150. <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
  151. <dependency>
  152. <groupId>com.baomidou</groupId>
  153. <artifactId>mybatis-plus</artifactId>
  154. <version>3.1.2</version>
  155. </dependency>
  156. <dependency>
  157. <groupId>org.mybatis</groupId>
  158. <artifactId>mybatis-spring</artifactId>
  159. <version>2.0.2</version>
  160. </dependency>
  161. <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper -->
  162. <dependency>
  163. <groupId>tk.mybatis</groupId>
  164. <artifactId>mapper</artifactId>
  165. <version>4.1.5</version>
  166. </dependency>
  167. <dependency>
  168. <groupId>ognl</groupId>
  169. <artifactId>ognl</artifactId>
  170. <version>3.2.8</version>
  171. </dependency>
  172. <dependency>
  173. <groupId>org.slf4j</groupId>
  174. <artifactId>slf4j-api</artifactId>
  175. <version>1.7.25</version>
  176. </dependency>
  177. <dependency>
  178. <groupId>org.slf4j</groupId>
  179. <artifactId>slf4j-simple</artifactId>
  180. <version>2.0.0-alpha0</version>
  181. </dependency>
  182. <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop -->
  183. <dependency>
  184. <groupId>org.slf4j</groupId>
  185. <artifactId>slf4j-nop</artifactId>
  186. <version>2.0.0-alpha0</version>
  187. </dependency>
  188. <dependency>
  189. <groupId>org.slf4j</groupId>
  190. <artifactId>slf4j-log4j12</artifactId>
  191. <version>1.7.25</version>
  192. </dependency>
  193. <dependency>
  194. <groupId>javax.servlet</groupId>
  195. <artifactId>jsp-api</artifactId>
  196. <version>2.0</version>
  197. </dependency>
  198. <dependency>
  199. <groupId>javax.servlet</groupId>
  200. <artifactId>servlet-api</artifactId>
  201. <version>2.5</version>
  202. </dependency>
  203. <dependency>
  204. <groupId>jstl</groupId>
  205. <artifactId>jstl</artifactId>
  206. <version>1.2</version>
  207. </dependency>
  208. <!--jstl-->
  209. <dependency>
  210. <groupId>org.apache.taglibs</groupId>
  211. <artifactId>taglibs-standard-spec</artifactId>
  212. <version>1.2.5</version>
  213. </dependency>
  214. <dependency>
  215. <groupId>org.apache.taglibs</groupId>
  216. <artifactId>taglibs-standard-impl</artifactId>
  217. <version>1.2.5</version>
  218. </dependency>
  219. <!--引入pagehelper分页插件-->
  220. <dependency>
  221. <groupId>com.github.pagehelper</groupId>
  222. <artifactId>pagehelper</artifactId>
  223. <version>5.1.10</version>
  224. </dependency>
  225. <!--对象转json-->
  226. <dependency>
  227. <groupId>com.fasterxml.jackson.core</groupId>
  228. <artifactId>jackson-databind</artifactId>
  229. <version>2.9.9</version>
  230. </dependency>
  231. <!--JSR303数据校验-->
  232. <dependency>
  233. <groupId>org.hibernate.validator</groupId>
  234. <artifactId>hibernate-validator</artifactId>
  235. <version>6.0.17.Final</version>
  236. </dependency>
  237. <!--引入阿里的druid数据源-->
  238. <dependency>
  239. <groupId>com.alibaba</groupId>
  240. <artifactId>druid</artifactId>
  241. <version>1.1.19</version>
  242. </dependency>
  243. </dependencies>
  244. <build>
  245. <finalName>Spring</finalName>
  246. <plugins>
  247. <!--Mybatis逆向工程的插件-->
  248. <plugin>
  249. <groupId>org.mybatis.generator</groupId>
  250. <artifactId>mybatis-generator-maven-plugin</artifactId>
  251. <version>1.3.7</version>
  252. <configuration>
  253. <!--指定本地的逆向工程设置文件-->
  254. <configurationFile>src/main/resources/generator.xml</configurationFile>
  255. <verbose>true</verbose>
  256. <overwrite>true</overwrite>
  257. </configuration>
  258. <!--在插件配置数据库驱动包以防止插件运行找不到驱动包-->
  259. <dependencies>
  260. <dependency>
  261. <groupId>mysql</groupId>
  262. <artifactId>mysql-connector-java</artifactId>
  263. <version>8.0.13</version>
  264. </dependency>
  265. </dependencies>
  266. </plugin>
  267. <plugin>
  268. <artifactId>maven-clean-plugin</artifactId>
  269. <version>3.1.0</version>
  270. </plugin>
  271. <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
  272. <plugin>
  273. <artifactId>maven-resources-plugin</artifactId>
  274. <version>3.1.0</version>
  275. </plugin>
  276. <plugin>
  277. <artifactId>maven-compiler-plugin</artifactId>
  278. <version>3.7.0</version>
  279. </plugin>
  280. <plugin>
  281. <artifactId>maven-surefire-plugin</artifactId>
  282. <version>2.22.2</version>
  283. </plugin>
  284. <plugin>
  285. <artifactId>maven-war-plugin</artifactId>
  286. <version>3.2.3</version>
  287. </plugin>
  288. <plugin>
  289. <artifactId>maven-install-plugin</artifactId>
  290. <version>2.5.2</version>
  291. </plugin>
  292. <plugin>
  293. <artifactId>maven-deploy-plugin</artifactId>
  294. <version>2.8.2</version>
  295. </plugin>
  296. </plugins>
  297. </build>

B.项目配置的jar包下载

lib.zip

3、在项目模块中添加Web模块

A.创建Web模块

I.管理项目模块

image.png

II.添加web模块

image.png

III.配置WebRoot和web.xml文件

image.png

IV.配置WebRoot

image.png

V.配置web.xml-将web.xml文件放到刚才的WebRoot下面

image.png
image.png

B.部署Web应用

VI.创建部署包

image.png
image.png

VII.将Maven管理的jar包添加至WEB-INF下的lib中

image.png

VIII.配置Tomcat

image.png
image.png
image.png
image.png

IVV.将应用部署包添加至Tomcat下

image.png
image.png

VV.如果没有部署包添加部署包的工程

image.png

4、SpringMVC相关配置

A.新建文件目录结构如下

image.png

I.新建spring-mvc.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"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx.xsd
  16. http://www.springframework.org/schema/mvc
  17. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  18. <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
  19. <context:component-scan base-package="com.fcant.ssmcom.controller">
  20. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
  21. </context:component-scan>
  22. <!-- 注解驱动,以使得访问路径与方法的匹配可以通过注解配置 -->
  23. <mvc:annotation-driven >
  24. <mvc:message-converters register-defaults="true">
  25. <bean class="org.springframework.http.converter.StringHttpMessageConverter">
  26. <property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
  27. </bean>
  28. </mvc:message-converters>
  29. </mvc:annotation-driven>
  30. <!-- 静态页面,如html,css,js,images可以访问 -->
  31. <mvc:default-servlet-handler />
  32. <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
  33. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  34. <property name="prefix" value="/WEB-INF/jsp/" />
  35. <property name="suffix" value=".jsp" />
  36. </bean>
  37. <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
  38. <bean id="multipartResolver"
  39. class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  40. <!-- 默认编码 -->
  41. <property name="defaultEncoding" value="utf-8" />
  42. <!-- 文件大小最大值 -->
  43. <property name="maxUploadSize" value="10485760000" />
  44. <!-- 内存中的最大值 -->
  45. <property name="maxInMemorySize" value="40960" />
  46. <!-- 启用是为了推迟文件解析,以便捕获文件大小异常 -->
  47. <property name="resolveLazily" value="true"/>
  48. </bean>
  49. <!--两个标准配置-->
  50. <!--将springMVC不能处理的请求交给tomcat-->
  51. <mvc:default-servlet-handler/>
  52. <!--能支持springMVC更高级的一些功能,JSR303校验。快捷的ajax...映射动态请求-->
  53. <mvc:annotation-driven/>
  54. </beans>

II.配置web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <welcome-file-list>
  7. <welcome-file>index.jsp</welcome-file>
  8. </welcome-file-list>
  9. <!-- 编码过滤器 -->
  10. <filter>
  11. <filter-name>encodingFilter</filter-name>
  12. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  13. <async-supported>true</async-supported>
  14. <init-param>
  15. <param-name>encoding</param-name>
  16. <param-value>UTF-8</param-value>
  17. </init-param>
  18. </filter>
  19. <filter-mapping>
  20. <filter-name>encodingFilter</filter-name>
  21. <url-pattern>/*</url-pattern>
  22. </filter-mapping>
  23. <!-- Spring MVC servlet -->
  24. <servlet>
  25. <servlet-name>SpringMVC</servlet-name>
  26. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  27. <init-param>
  28. <param-name>contextConfigLocation</param-name>
  29. <param-value>classpath:spring-mvc.xml</param-value>
  30. </init-param>
  31. <load-on-startup>1</load-on-startup>
  32. <async-supported>true</async-supported>
  33. </servlet>
  34. <servlet-mapping>
  35. <servlet-name>SpringMVC</servlet-name>
  36. <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
  37. <url-pattern>/</url-pattern>
  38. </servlet-mapping>
  39. </web-app>

III.新建index.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  5. %>
  6. <html>
  7. <head>
  8. <base href="<%=basePath%>">
  9. <title>Welcome</title>
  10. </head>
  11. <body>
  12. <h3>欢迎</h3>
  13. </body>
  14. </html>

IV.(EX)遇到ClassNotFound时将相关jar放到WEB-INF下的lib中,并将该文件目录添加至项目的Libraries

image.png
image.png

V.启动Tomcat并运行显示

image.png

5、日志配置

I.日志文件存放位置

image.png

II.新建log4j.properties

  1. log4j.rootLogger=DEBUG,STDOUT
  2. log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
  3. log4j.appender.STDOUT.Target=System.out
  4. log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
  5. log4j.appender.STDOUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}%m%n

III.配置log4j2.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration status="warn">
  3. <Appenders>
  4. <Console name="Console" target="SYSTEM_OUT">
  5. <PatternLayout pattern="[%-5p] %d %c - %m%n" />
  6. </Console>
  7. <File name="File" fileName="dist/my.log">
  8. <PatternLayout pattern="%m%n" />
  9. </File>
  10. </Appenders>
  11. <Loggers>
  12. <Logger name="mh.sample2.Log4jTest2" level="INFO">
  13. <AppenderRef ref="File" />
  14. </Logger>
  15. <Root level="INFO">
  16. <AppenderRef ref="Console" />
  17. </Root>
  18. </Loggers>
  19. </Configuration>

6、Spring和Mybatis整合

A.配置数据源

I.新建jdbc.properties

  1. driver=com.mysql.cj.jdbc.Driver
  2. url=jdbc:mysql:///ssm?serverTimezone=UTC
  3. username=root
  4. password=root
  5. #定义初始连接数
  6. initialSize=0
  7. #定义最大连接数
  8. maxActive=20
  9. #定义最大空闲
  10. maxIdle=20
  11. #定义最小空闲
  12. minIdle=1
  13. #定义最长等待时间
  14. maxWait=60000

image.png

II.新建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"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx.xsd
  16. http://www.springframework.org/schema/mvc
  17. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  18. <!-- 自动扫描 -->
  19. <context:component-scan base-package="com.fcant.ssmcom" />
  20. <!-- 引入配置文件 -->
  21. <bean id="propertyConfigurer"
  22. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  23. <property name="location" value="classpath:jdbc.properties" />
  24. </bean>
  25. <!-- 配置数据源 -->
  26. <!-- <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">-->
  27. <!-- MySQL数据库驱动 -->
  28. <!-- <property value="${driver}" name="driverClassName"/>-->
  29. <!-- 连接数据库的URL -->
  30. <!-- <property value="${url}" name="url"/>-->
  31. <!-- 连接数据库的用户名 -->
  32. <!-- <property value="${username}" name="username"/>-->
  33. <!-- 连接数据库的密码 -->
  34. <!-- <property value="${password}" name="password"/>-->
  35. <!-- </bean>-->
  36. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  37. <!-- 基本属性 url、user、password -->
  38. <property name="url" value="${url}" />
  39. <property name="username" value="${username}" />
  40. <property name="password" value="${password}" />
  41. <property name="driverClassName" value="${driver}" />
  42. <!-- 配置初始化大小、最小、最大 -->
  43. <property name="initialSize" value="3" />
  44. <property name="minIdle" value="3" />
  45. <property name="maxActive" value="20" />
  46. <!-- 配置获取连接等待超时的时间 -->
  47. <property name="maxWait" value="60000" />
  48. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  49. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  50. <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  51. <property name="minEvictableIdleTimeMillis" value="300000" />
  52. <property name="validationQuery" value="SELECT 1" />
  53. <property name="testWhileIdle" value="true" />
  54. <property name="testOnBorrow" value="false" />
  55. <property name="testOnReturn" value="false" />
  56. <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
  57. <property name="poolPreparedStatements" value="true" />
  58. <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
  59. </bean>
  60. <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
  61. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  62. <property name="dataSource" ref="dataSource" />
  63. <!-- 自动扫描mapping.xml文件 -->
  64. <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
  65. <!-- PageHelper插件配置 -->
  66. <property name="plugins">
  67. <array>
  68. <bean class="com.github.pagehelper.PageInterceptor">
  69. <property name="properties">
  70. <value></value>
  71. </property>
  72. </bean>
  73. </array>
  74. </property>
  75. </bean>
  76. <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
  77. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  78. <property name="basePackage" value="com.fcant.ssmcom.mapper" />
  79. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  80. </bean>
  81. <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
  82. <bean id="transactionManager"
  83. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  84. <property name="dataSource" ref="dataSource" />
  85. </bean>
  86. <!-- 定义遇到异常就回滚 -->
  87. <tx:advice id="txadvice" transaction-manager="transactionManager">
  88. <tx:attributes>
  89. <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
  90. <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>
  91. <tx:method name="edit*" propagation="REQUIRED" rollback-for="Exception" />
  92. <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
  93. <tx:method name="list*" propagation="REQUIRED" rollback-for="Exception"/>
  94. </tx:attributes>
  95. </tx:advice>
  96. <aop:config>
  97. <!-- 切入点的包名要与自己的service所在包一样 -->
  98. <aop:pointcut id="serviceMethod" expression="execution(* com.fcant.ssmcom.service.*.*(..))"/>
  99. <aop:advisor pointcut-ref="serviceMethod" advice-ref="txadvice"/>
  100. </aop:config>
  101. </beans>

image.png
image.png

III.将spring-mybatis配置至web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <welcome-file-list>
  7. <welcome-file>index.jsp</welcome-file>
  8. </welcome-file-list>
  9. <!-- Spring和mybatis的配置文件 -->
  10. <context-param>
  11. <param-name>contextConfigLocation</param-name>
  12. <param-value>classpath:spring-mybatis.xml</param-value>
  13. </context-param>
  14. <!-- 编码过滤器 -->
  15. <!-- 编码过滤器 -->
  16. <filter>
  17. <filter-name>encodingFilter</filter-name>
  18. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  19. <async-supported>true</async-supported>
  20. <init-param>
  21. <param-name>encoding</param-name>
  22. <param-value>UTF-8</param-value>
  23. </init-param>
  24. </filter>
  25. <filter-mapping>
  26. <filter-name>encodingFilter</filter-name>
  27. <url-pattern>/*</url-pattern>
  28. </filter-mapping>
  29. <!-- Spring监听器 -->
  30. <listener>
  31. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  32. </listener>
  33. <!-- 防止Spring内存溢出监听器 -->
  34. <listener>
  35. <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  36. </listener>
  37. <!-- Spring MVC servlet -->
  38. <servlet>
  39. <servlet-name>SpringMVC</servlet-name>
  40. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  41. <init-param>
  42. <param-name>contextConfigLocation</param-name>
  43. <param-value>classpath:spring-mvc.xml</param-value>
  44. </init-param>
  45. <load-on-startup>1</load-on-startup>
  46. <async-supported>true</async-supported>
  47. </servlet>
  48. <servlet-mapping>
  49. <servlet-name>SpringMVC</servlet-name>
  50. <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
  51. <url-pattern>/</url-pattern>
  52. </servlet-mapping>
  53. </web-app>

7、编写JavaBean以及Mapper接口和映射文件

避免配置在启动时找不到相应的文件而报错-也可以使用Mybatis的逆向工程生成

A.自定义JavaBean以及Mapper接口和映射文件

image.png

B.逆向工程生成JavaBean以及Mapper接口和映射文件

Mybatis连接MySQL数据库进行逆向工程 · 语雀

8、更改配置文件相应的包

image.png
image.png

9、项目结构

image.png

10、项目地址

github

11、项目拓展-拦截器配置

SpringMVC中拦截器的配置 · 语雀