springmvc + spring + mybatis ssm
项目架构
springmvc-homework
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── yqlzmzr
│ │ │ ├── controller
│ │ │ │ └── BookController.java
│ │ │ ├── interceptor
│ │ │ │ └── MyInterceptor.java
│ │ │ ├── mapper
│ │ │ │ └── BooksMapper.java
│ │ │ ├── pojo
│ │ │ │ └── Books.java
│ │ │ └── service
│ │ │ ├── BooksService.java
│ │ │ └── impl
│ │ │ └── BooksServiceImpl.java
│ │ └── resources
│ │ ├── applicationContext.xml
│ │ ├── db.properties
│ │ ├── mapper
│ │ │ └── BooksMapper.xml
│ │ ├── mybatis-config.xml
│ │ ├── spring-dao.xml
│ │ ├── spring-service.xml
│ │ └── spring-servlet.xml
└── web
├── index.jsp
└── WEB-INF
└── web.xml
依赖
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.10</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<!---- 关闭静态资源过滤 ---->
<build>
<resources>
<resource>
<directory>src/mian/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
mybtis-config
书写一些mybatis的 setting,日志之类
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 启用日志,并指定为 LOG4J -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<!-- 包别名 -->
<typeAliases>
<package name="com.yqlzmzr.pojo"/>
</typeAliases>
<!-- 这个指定mapper。xml文件可以在这里做也可以在后面的 spring-dao.xml文件做 -->
<!-- <mappers>-->
<!-- <mapper resource="mapper/BooksMapper.xml"/>-->
<!-- </mappers>-->
</configuration>
db.properties
数据源连接池 druid 的配置参数
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springmvc
jdbc.username=root
jdbc.password=123456
jdbc.maxActive=100
jdbc.initialSize=5
jdbc.maxWait=60000
jdbc.minIdle=5
jdbc.testWhileIdle=true
jdbc.testOnBorrow=false
jdbc.testOnReturn=false
log4j.properties
#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
log4j.rootLogger=DEBUG,console,file
#控制台输出的相关设置
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n
#文件输出的相关设置
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/rzp.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sq1.PreparedStatement=DEBUG
spring-dao
spring 和 mybatis的整合
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 1,加载数据源配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 2,连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--启动程序时,在连接池中初始化多少个连接-->
<property name="initialSize" value="${jdbc.initialSize}"/>
<!-- 连接池中最多支持多少个活动会话-->
<property name="maxActive" value="${jdbc.maxActive}"/>
<!--程序向连接池中请求连接时,超过maxWait的值后,认为本次请求失败,即连接池没有可用连接,
单位毫秒,设置-1时表示无限等待-->
<property name="maxWait" value="${jdbc.maxWait}"/>
<!-- 回收空闲连接时,将保证至少有minIdle个连接. 与initialSize相同-->
<property name="minIdle" value="${jdbc.minIdle}"/>
<!-- 当程序请求连接,池在分配连接时,是否先检查该连接是否有效。(高效)-->
<property name="testWhileIdle" value="${jdbc.testWhileIdle}"/>
<!-- 程序 申请 连接时,进行连接有效性检查(低效,影响性能)-->
<property name="testOnBorrow" value="${jdbc.testOnBorrow}"/>
<!-- 程序 返还 连接时,进行连接有效性检查(低效,影响性能)-->
<property name="testOnReturn" value="${jdbc.testOnReturn}"/>
</bean>
<!-- 3, sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- 扫描mapperxml文件 这里能用通配符 * -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!--加载mybatis配置文件 通过mybatis的配置文件配置 这个功能多一点,还能设置二级缓存,日志啊啥额 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 也可以在在这里直接配置别名-->
<!-- <property name="typeAliasesPackage" value="com.yqlzmzr.pojo"/>-->
</bean>
<!-- 配置dao接口扫描包,动态实现了Dao 接口可以注入到spring容器中-->
<!-- sqlSessionFactoryBeanName 指定在 spring 上下文中有多个的情况下使用哪个SqlSessionFactory 。 通常只有当您有多个数据源时才需要这样做。
注意使用的是 bean 名称,而不是 bean 引用。 这是因为scanner在启动过程中加载较早,构建mybatis对象实例为时尚早。-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="scannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.yqlzmzr.mapper"/>
</bean>
</beans>
spring-service
业务层配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 扫描业务层组件 -->
<context:component-scan base-package="com.yqlzmzr.service"/>
<!-- 新建事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 这里的 dataSource 关联着spring-dao里面的 dataSource-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启事务注解 针对不同的方法开启不同的事务类型可以在这里设置,具体百度-->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
配置了如上的事务配置以后需要在service使用事务注解
spring-servlet
mvc的自身的配置,也就是controller的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描包:controller-->
<context:component-scan base-package="com.yqlzmzr.controller"/>
<!--注解加载映射器、适配器,解决Json数据中文乱码问题 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 静态资源过滤 -->
<mvc:default-servlet-handler/>
<!-- 注意∶
/ 与 /* 与 /** 的区别
/ 拦截所有,但不包括html、jsp.等。
/* 真正的拦截所有,包括所有后缀。但只匹配一级,即/add , /query等
/** 拦截所有,包括所有后缀。但匹配多级,即/add/user...等
-->
<!-- 拦截器配置-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.yqlzmzr.interceptor.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
拦截器 书写
实现 HandlerInterceptor 接口即可
拦截器不同于过滤器 ,拦截器 基于 AOP 思想
applicationContext.xml
整合spring相关的配置文件,不包含mvc配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
</beans>
web.xml
通常,您将在DispatcherServlet上下文中定义所有与MVC相关的bean(控制器和视图等),并在根上下文中定义所有交叉bean,例如安全性,事务,服务等ContextLoaderListener。
通常,此设置工作正常,因为您很少需要访问任何MVC bean(从子上下文)到安全相关的类(从根上下文)。大多数情况下,我们在MVC类上使用安全bean,他们可以通过上面的设置访问它。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>