简介
SSM(Spring+SpringMVC+MyBatis)整合是将这三个流行的Java框架组合在一起,以创建一个高效、灵活且易于维护的web应用程序。
步骤
-
创建maven的web项目
-
导入相关依赖
-
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>5.2.15.RELEASE</spring.version> </properties> <dependencies> <!--添加Spring-mvc的依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!--aop--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.30</version> </dependency> <!--c3p0连接池--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--log4j--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.36</version> </dependency> <!--jackson: json工具jar--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.3</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> </dependency> </dependencies>
导入配置文件
-
数据库参数配置文件
-
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:8888/userdb?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC jdbc.username=root jdbc.password=root jdbc.acquireIncrement=5 jdbc.initialPoolSize=5 jdbc.minPoolSize=3 jdbc.maxPoolSize=10
-
log4j的配置文件
-
log4j.rootCategory=debug, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
-
mybatis的全局配置文件(可以省略)
-
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> </configuration>
-
sql映射文件(多个)
-
如
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.fs.mapper.UserMapper"> <select id="queryById" resultType="user" parameterType="int"> select * from tb_user where id = #{id} </select> <select id="count" parameterType="Userpageinfo" resultType="long"> select count(1) from tb_user <include refid="where_sql"/> </select> <sql id="where_sql"> <where> <if test="userpageinfo != null"> <if test="userpageinfo.name != null and userpageinfo.name != ''"> and name like '%${userpageinfo.name}%' </if> <if test="userpageinfo.gender != null and userpageinfo.gender != ''"> and gender = #{userpageinfo.gender} </if> <if test="userpageinfo.startAge != null"> and age >= #{userpageinfo.startAge} </if> <if test="userpageinfo.endAge != null"> and age <= #{userpageinfo.endAge} </if> </if> </where> </sql> <select id="queryByPage" parameterType="userpageinfo" resultType="User"> select * from tb_user <include refid="where_sql"/> limit #{userpageinfo.startno},#{userpageinfo.pageSize} </select> </mapper>
-
Spring配置文件
-
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd "> <import resource="beans-mybtais.xml"/> <import resource="beans-tx.xml"/> <!--扫描service层注解--> <context:component-scan base-package="com.fs.service"/> </beans>
-
springmvc的配置文件
-
Mybatis 与Spring整合
1.Mybatis 与Spring整合: DataSource的Bean (使用第三方的连接池), SqlSessionFactoryBean, Mapper扫描器(扫描指定包下Mapper接口, 生成代理对象,保存Spring容器)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--数据源-->
<!--加载properties
如果是多个properties文件, 文件路径与文件路径使用逗号分割 classpath:db.properties,classpath:log4j.properties
-->
<context:property-placeholder location="classpath:db.properties" file-encoding="UTF-8"/>
<!--配置一个C3p0数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--数据库四大参数-->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--连接池相关的参数-->
<!--初始连接数-->
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
<!--增量, 每次创建几个连接-->
<property name="acquireIncrement" value="${jdbc.acquireIncrement}"/>
<!--最大连接数-->
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
<!--最小连接数 默认值等于初始连接数-->
<property name="minPoolSize" value="${jdbc.minPoolSize}"/>
</bean>
<!--配置SqlSessionFactoryBean-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!--配置别名-->
<property name="typeAliasesPackage" value="com.fs.entity"/>
<!--加载sql映射文件 * 任意 一定要注意: classpath*: 如果不写, 后面*无效-->
<property name="mapperLocations" value="classpath*:mapper/*Mapper.xml"/>
</bean>
<!--配置mapper扫描器 扫描指定包下所有的Mapper接口
创建Mapper接口代理对象, sqlSession.getMapper()并且保存到Spring容器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描那个包下Mapper接口-->
<property name="basePackage" value="com.fs.mapper"/>
<!--注入SqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
</bean>
</beans>
业务层与Spring整合
2.业务层与Spring整合: 声明式事务配置(AOP)
<bean id="logadvice" class="com.advice.LogAdvice"></bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice">
<!--事务定义-->
<tx:attributes>
<!--tx:method 针对于切入点匹配的方法, 进行分别定义事务
name: 方法名, 可以* 作为通配符: 表示任意
CURD: 增删改查,
query*: 对方法是以query开头的
isolation: 配置事务隔离级别, 使用默认值
propagation: 配置事务传播机制
read-only: 是否只读: 默认值false, 查询中设置为只读
timeout: 超时时间: 默认 -1 永不超时
rollback-for: 什么异常下回滚, 默认值: RuntimeException 增删改
no-rollback-for: 什么异常不回滚, 默认是空 增删改
-->
<tx:method name="querry*" propagation="SUPPORTS" read-only="true"/>
<!--业务方法抛出任何异常,都回滚-->
<tx:method name="*" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="point1" expression="execution(* com.service..*.*(..))"/>
<!--织入-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="point1"></aop:advisor>
</aop:config>
Spring 与web整合
3.Spring 与web整合: 配置一个监听器, 监听tomcat启动的时候,创建Spring容器
<?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>
<!--指定spring的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
</web-app>
Spring 与SpringMVC
4.Spring 与SpringMVC不需要整合(SpringMVC 是Spring子模块)
SpringMVC的配置:
前端控制器 web.xml文件
<?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">
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
视图解析器,配置mvc注解驱动
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>
<!--mvc注解驱动-->
<mvc:annotation-driven/>
<context:component-scan base-package="com.fs.controller"/>
</beans>