SSM整合步骤
这里写目录标题
1.创建maven工程引入相关依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.15</version>
</dependency>
<!-- Spring JDBC -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.15</version>
</dependency>
<!--Spring 面向切面-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.15</version>
</dependency>
<!-- Spring的单元测试 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.15</version>
<scope>test</scope>
</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.28</version>
</dependency>
<!-- jstl -->
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</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.4.0</version>
</dependency>
</dependencies>
2.引入前端框架bootstrap
3 配置web.xml
启动Spring容器
使项目一启动加载某处的spring配置文件
在类路径下创建Spring配置文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
类路径下的Spring配置文件
4.在web.xml配置SpringMVC前段控制器拦截所有请求
1)若不指定SpringMVC配置文件需要删掉中间的< init-parm>标签并在web-inf 下新建一个web.xml命名为XXX-servlet.xml
xxx使你servlet标签中的名字
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
2)顺手配置上字符过滤器 放在所有过滤器之前‘
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<!-- encoding 指定解决POST请求乱码-->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<!-- forceEncoding 顺手解决相应乱码-->
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
3)配置Rest 风格
<!-- Rest风格的配置 -->
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4.配置SpringMVC
头部标签
<?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
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">
1)SpringMVC不能扫描所有组件让其只扫描前段的控制器
<!-- 扫描所有组件 -->
<context:component-scan base-package="com.ycx" use-default-filters="false">
<!-- 只扫描控制器 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
2)配置视图解析器
<!-- 配置一个视图解析器,能帮我们拼接页面地址 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
3).两个标准配置
1.将springmvc不能处理的请求交给tomcat
2.支持SpringMVC更高级的一些功能 JSR303校验, 快捷的ajax 映射动态请求功能
<mvc:default-servlet-handler/>
<!-- 支持SpringMVC更高级的一些功能 JSR303校验, 快捷的ajax 映射动态请求 -->
<mvc:annotation-driven></mvc:annotation-driven>
5.配置Spring
1)让其开启扫描 除了SprngMVC扫描的Controller控制器其它都扫描
<context:component-scan base-package="com.ycx">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller">
</context:excludefilter>
</context:component-scan>
2)配置数据源 (数据库连接池)
类路径下创建一个jdbc.properties
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 数据源 -->
<bean id ="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
3)配置和MyBatis的整合
类路径下新建mybatis-config.xml (mybatis的核心配置文件)
<bean id="sqlSessionFactory" 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>
4)配置扫描器,将mybatis接口的实现加入到ioc中
<!-- 配置扫描器,将mybatis接口的实现加入ioc容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描所有dao接口的实现 加入到ioc容器中 -->
<property name="basePackage" value="com.ycx.dao"></property>
</bean>
5)事物控制的配置
1)控制数据源 pooledDataSource也是上面数据源的id
<!-- 控制住数据源 -->
<bean id ="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 控制住数据源 -->
<property name="dataSource" ref="pooledDataSource" ></property>
</bean>
2)
<!-- 开启基于注解的事物,使用xml配置形式的事物 (主要是使用配置式) -->
<aop:config>
<aop:pointcut expression="execution(* com.ycx.services..*(..))" id="txPomnit"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPomnit"/>
</aop:config>
<!-- 配置事物增强,事物如何切入 -->
<tx:advice id ="txAdvice" transaction-manager="transactionManager">
<!-- 所有方法都是事物方法 -->
<tx:attributes>
<tx:method name="*"/>
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
6.mybatis-config.xml的配置
可在applicationContext中配置也可在mybatis-config.xml中配置
<?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>
<!-- 驼峰命名 -->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- 设置类型别名类型别名所在的包 -->
<typeAliases>
<package name="com.ycx.bean"/>
</typeAliases>
<!--引入映射文件映射文件所在的包 -->
</configuration>
7.使用mybatis的逆向工程
导入jar包
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
和pow.xml同级下创建配置文件 并配置 看看各个包的路径
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--设置不生成注释-->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssm_crud?useSSL=false&serverTimezone=UTC"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 指定javaBean生成位置 -->
<javaModelGenerator targetPackage="com.ycx.bean" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 指定sql映射文件生成位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 指定dao接口生成位置 mapper -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.ycx.dao" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 指定每个表的生成策略 -->
<table tableName="tbl_emp" domainObjectName="Employee"></table>
<table tableName="tbl_dept" domainObjectName="Dept"></table>
</context>
</generatorConfiguration>
2)测试生成类
package com.ycx.test;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
public class MBGTest {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mbg.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
运行完成后注意要去实体类中生成构造器 和toString方法
8 、搭建Spring单元测试
1)导依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.15</version>
<scope>test</scope>
</dependency>
2)使用@ContextConfiguration指定Spring配置文件位置
@ContextConfiguration(locations={“classpath:applicationContext.xml”})
指定用哪个Test进行单元测试
@RunWith(SpringJUnit4ClassRunner.class)
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.ycx.bean.Dept;
import com.ycx.bean.Employee;
import com.ycx.dao.DeptMapper;
//推荐Spring的项目可以使用Spring的单元测试 可以自动注入我们需要的组件
//导入SpringTest模块
//使用@ContextConfiguration指定Spring配置文件的位置
//直接autowired要使用的组件
import com.ycx.dao.EmployeeMapper;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class MapperTest {
@Autowired
DeptMapper d;
@Autowired
EmployeeMapper e;
@Test
public void testCRUD(){
// d.insertSelective(new Dept(null, "测试部"));
// d.insertSelective(new Dept(null, "电竞部"));
// e.insertSelective(new Employee(null, "Jerry", "m", "123@qq.com", 1));
//3批量插入
System.out.println(d);
}
}