一:ssm描述
什么是 SSM ?他就是Spring,SpringMVC,Mybatis。
SpringMVC:Controller层,通过实现Model-View-Controller模式来很好地将数据、业务与展现进行分离
Spring:Service层处于中间的位置,其中最出名的ioc控制反转和aop面向切面编程
Mybatis:用于dao层,和数据库交互
二:整合ssm
2.1搭建maven工程(pom.xml导入相关的包)
maven中央仓库:maven中央仓库
这里要在pom.xml导入相关的包。相关注释在代码内
具体配置说明
<dependencies> <!--映入依赖包--> <!--1springmvc的包--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.18</version> </dependency> <!--2spring jdbc--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.18</version> </dependency> <!--spring -aspects面向切面编程--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.3.18</version> </dependency> <!--MYBATIS--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <!--mybatis整合spring--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <!--数据库连接池--> <!-- https://mvnrepository.com/artifact/c3p0/c3p0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> <scope>runtime</scope> </dependency> <!-- Spring5和Thymeleaf整合包 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.12.RELEASE</version> </dependency> <!--其他依赖--> <!-- https://mvnrepository.com/artifact/jstl/jstl --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> </dependencies> <build> <finalName>ssm-crud</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> <!-- 构建过程中用到的插件 --> <plugins> <!-- 具体插件,逆向工程的操作是以构建过程中插件形式出现的 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.0</version> <!-- 插件的依赖 --> <dependencies> <!-- 逆向工程的核心依赖 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <!-- 数据库连接池 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.2</version> </dependency> <!-- MySQL驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> </dependencies> </plugin> </plugins> </build>
2.2配置mybatis-config.xml文件(mybatis)
在resources中创建mybatis-config.xml(名字可以任意)
创建jdbc的资源文件
以后链接数据库都导入这个资源
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm jdbc.username=root jdbc.password=0615
具体配置说明
位置有顺序要求
1.引入properties 就是jdbc的配置资源
2.全局舍子(开启驼峰)设置类型别名
3.链接数据库
4.引入映射文件
<?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> <!--位置有顺序要求--> <!--引入properties文件--> <properties resource="dbconfig.properties"></properties> <!--开启驼峰--> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!--设置类型别名--> <typeAliases> <package name="com.xbfinal.bean"/> </typeAliases> <!--.链接数据库--> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <!--引入映射文件--> <mappers> <!--<mapper resource="mappers/UserMapper.xml"/>--> <package name="com.xbfinal.dao"/> </mappers> </configuration> </configuration>
**************************************
2.3配置applicationContext.xm (Spring)
在resources中创建applicationContext.xm(名字可以任意)
具体配置说明
spring配置文件核心点
0.业务逻辑扫描
1.数据源(c3p0)
2.整合mybatis
SqlSessionFactoryBean
3.事务控制
=======================================
在数据源我们采用的是导入的方式
<?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:aop="http://www.springframework.org/schema/aop" 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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <!--spring配置文件 这里主要配置和业务逻辑有关的--> <!--业务逻辑扫描--> <context:component-scan base-package="com.xbfinal"> <!--不扫描控制器--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--============================== 数据源 ======================================--> <context:property-placeholder location="dbconfig.properties"/> <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="driverClass" value="${jdbc.driver}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--========================== 配置和mybatis整合 =================================================================--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!--指定mybatis全局配置文件的位置--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!--配置数据源 就是上面配置的--> <property name="dataSource" ref="pooledDataSource"></property> <!--指定mybatis的mapper文件的位置--> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean> <!--配置扫描器。将mybatis接口实现加入到ioc容器--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--扫描所有的dao接口的实现加入ioc容器--> <property name="basePackage" value="com.xbfinal.dao"/> </bean> <!--==============================================================================--> <!--============================ 事务控制 =============================================================--> <bean id ="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--控制数据源--> <property name="dataSource" ref="pooledDataSource"></property> </bean> <!--开启注解的事务,使用xml配置形式的事务(都使用配置)--> <aop:config> <!--切入点表达式--> <aop:pointcut id="tePint" expression="execution(public * com.xbfinal.service..*(..))"/> <!--配置事务增强--> <aop:advisor advice-ref="txAdvice" pointcut-ref="tePint"/> </aop:config> <!--配置事务增强,事务如何切入--> <tx:advice id="txAdvice" transaction-manager="TransactionManager"> <tx:attributes> <!--所有方法都是事务方法--> <tx:method name="*"/> <!--以get开始的所有方法--> <tx:method name="get*" read-only="true"/> </tx:attributes> </tx:advice> <!--==============================================================================--> <!--spring配置文件核心点 1.数据源 2.整合mybatis 事务控制 --> </beans>
2.4配置springMVC环境DispatcherServlet
在rWEB-INF下创建DispatcherServlet.xm
详细配置
1.springmvc配置文件 包含网页跳转的控制和配置
2.配置视图解析器 方便页面返回
3.两个标准配置
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://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"> <!--springmvc配置文件 包含网页跳转的控制和配置--> <context:component-scan base-package="com.xbfinal.controller"> </context:component-scan> <!--配置视图解析器 方便页面返回--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <mvc:view-controller path="/" view-name="index"></mvc:view-controller> <!--两个标准配置--> <mvc:default-servlet-handler/><!--将springmvc不能处理的交给tomcat--> <!--支持springmvc更高级的功能:JSR303校验 ajax请求.映射动态请求--> <mvc:annotation-driven/> </beans>
**************************************
2.5配置web.xml环境
配置内容如下
1启动sprin容器 applicationContext.xml
2.配置springMV的前端控制器DispatcherServlet 拦截所有请求
3.字符编码过滤器
4.HHF过滤器: HiddenHttpMethodFilter支持GET、POST、PUT、DELETE请求。
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <!--1启动sprin容器--> <context-param> <param-name>contextConfigLocation</param-name> <!--classpath:spring文件配置路径 项目启动就会加载这个文件 --> <param-value>classpath:applicationContext.xml </param-value> </context-param> <!-- 2.配置springMV的前端控制器DispatcherServlet 拦截所有请求--> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--3字符编码过滤器--> <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> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--HHF过滤器--> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <!-- HHF --> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
总结
以上就是ssm的基本整合内容
先配置mybatis
然后配置 spring
接下来就是springmvc
最后配置一下web.xml
mybatis需要配置那些?
1.引入properties文件
2.全局设置
开启驼峰
3.设置类型别名
4链接数据库.
5.引入映射文件
spring需要配置那些?
1.业务逻辑扫描
2数据源.
3.配置和mybatis整合SqlSessionFactoryBean
org.mybatis.spring.SqlSessionFactoryBean">
4.配置扫描器。将mybatis接口实现加入到ioc容器
5.事务控制TransactionManager
5.1 控制数据源
5.2开启注解的事务,使用xml配置形式的事务(都使用配置)
5.3配置事务增强,事务如何切入
springmvc需要配置那些?
1.网页跳转的控制和配置
package="com.xbfinal.controller">
2.配置视图解析器InternalResourceViewResolver 方便页面返回
3.两个标准配置
web.xml需要配置那些?
1.1启动sprin容器
2.配置springMV的前端控制器DispatcherServlet 拦截所有请求
3字符编码过滤器CharacterEncodingFilter
4.HHF过滤器