SSM集成
1.SSM集成 = Spring+SpringMvc+Mybatis集成
2.框架集成核心,如果你的项目中,用到了Spring框架,那么其他框架主要 就是和Spring集成;
3.和Spring集成的核心思路:
①把当前框架的核心类,交给Spring管理(IOC)
②如果框架有事务,那么事务也要统一交给Spring管理(AOP)
4.步骤:
①Spring
②Spring+Mybatis+管理事务
③Spring+Mybatis+SpringMVC
5.导包
Spring
SpringMVC
Mybatis
Spring+Mybatis+事务
集成包+数据库驱动包
6单独集成Spring
6.1.创建javaweb项目
6.2.测试项目首页index.jsp
6.3.导入spring的jar包
6.4.准备配置文件applicationContext.xml,并且配置对象
参考官方指南获取:
6.5.测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class BaseTest{
@Autowired
private Date myDate;
@Test
public void test()throws Exception{
System.out.println(myDate);
}
}
7.Spring+Mybatis
7.1.applicationContext.xml
配置扫描包路径:
<context:component-scan base-package=“cn.itsource.service”> </context:component-scan>
7.2.Spring管理连接池对象
①配置BasicDataSource对象
<?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.xsd">
<!-- 查找当前src下查找jdbc.properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
②jdbc.properties文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/user
jdbc.username=root
jdbc.password=root
注意事项:username一定要加前缀【jdbc.】,不然会出现问题。因为username已经被Spring在使用,会导致咱们的username失效,产生错误!当然也可以通过配置的形式解决该错误:
<context:property-placeholder location=“classpath:jdbc.properties” system-properties-mode=“NEVER” />建议:使用带前缀的key形式
7.3.集成Mybatis
①Mybatis入口:SqlSessionFactory(注入DataSource)
②配置Mapper代理类或扫描Mapper接口包(自动注入SqlSessionFactory)
③事务管理器,开启注解事务(注入DataSource)
<!-- 1.扫描包路径 -->
<context:component-scan base-package="cn.itsource.service"></context:component-scan>
<!-- 2.加载属性文件:连接数据库的参数。使用system-properties-mode="NEVER"忽略前缀 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 3.Spring管理连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 4.Spring管理SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 配置mybatis (mapper)映射器路径 -->
<property name="mapperLocations" value="classpath:cn/itsource/mapper/*Mapper.xml" />
<!-- 配置别名 -->
<property name="typeAliasesPackage" value="cn.itsource.domain" ></property>
</bean>
<!-- 5.配置扫描Mapper:动态创建mapper包中所有接口的动态实例 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 这句可以省略,自动注册 -->
<!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> -->
<property name="basePackage" value="cn.itsource.mapper"></property>
</bean>
<!-- 6. 定义事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 7. 注解驱动,启动事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
7.4.测试
①引入log4j的日志配置放在resources下:log4j.properties【参考以前工程mybatis】
②引入资源ssm.sql【参考课件】
③根据ssm数据库的表emp创建包cn.itsource.domain和实体类Emp
④修改jdbc.properties属性文件中的数据库为ssm
⑤创建包cn.itsource.mapper和接口EmpMapper
public interface EmpMapper {
List<Emp> findAll();
}
⑥在包cn.itsource.mapper下创建mapper映射文件EmpMapper.xml
<mapper namespace="cn.itsource.mapper.EmpMapper">
<select id="findAll" resultType="emp">
select * from emp
</select>
</mapper>
⑦测试EmpMapper代理对象
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class BaseTest{
@Autowired
private Date myDate;
@Autowired
private EmpMapper mapper;
@Autowired
private BasicDataSource dataSource;
@Test
public void test() throws Exception{
System.out.println(myDate);
System.out.println(dataSource);
System.out.println(dataSource.getConnection());
System.out.println(mapper);
}
}
⑧按照三层架构规范编写service规范,然后进行Spring测试
8.Spring+Mybatis+SpringMVC
8.1.导入SpringMVC的jar包
8.2.配置web.xml
①字符编码过滤器;
②SpringMVC前端控制器,加载SpringMVC的配置文件(子容器:applicationContext-mvc.xml);
③监听器,加载Spring的配置文件(父容器:applicationContext.xml);
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<!-- 方案1:独立容器方案,Spring和SpringMVC都有自己配置文件,实现责任分离 -->
<!-- 1.1 Spring容器初始化 -->
<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>
<!-- 1.2 配置SpringMVC前端控制器 -->
<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:applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- /*会拦截所有包括jsp -->
<!-- /不会拦截jsp,但是会拦截静态资源html、css、js、图片等 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 1.3 解决post请求参数中文乱码问题 -->
<!--
post请求乱码解决
get请求实在tomcat的server.xml中配置URIEncoding="UTF-8"
-->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!--指定字符编码-->
<init-param>
<!--因为当前成员变量为encoding,所以param-name必须为encoding-->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!--强制指定字符编码,即使request或response设置了字符编码,也会强制使用当前设置的,任何情况下强制使用此编码-->
<init-param>
<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>
</web-app>
8.3.applicationContext-mvc.xml
①配置扫描包路径;
②配置静态资源放行;
③配置开启Spring对mvc的支持,支持@RequestMapping注解;
④配置视图解析器;
⑤配置文件上传、拦截器;
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1. 配置扫描包路径子容器只扫描controller包 -->
<context:component-scan base-package="cn.itsource.controller"></context:component-scan>
<!-- 2. 配置静态资源放行 -->
<mvc:default-servlet-handler/>
<!-- 3. 配置开启Spring对mvc的支持,支持@RequestMapping注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 4.配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
8.4.测试
①创建包cn.itsource.controller和类UserController
②将show.jsp放在web根目录WebContext下【参考课件】
③编写UserController
@Controller
@RequestMapping("/emp")
public class EmpController {
@Autowired
private IEmpService service;//给字段赋值 = 注入
@RequestMapping("/show")
public String show(Model model){
//第一大职责:匹配请求 + 获取请求参数
//第二大职责:调用业务代码
List<Emp> emps = service.findAll();
//第三大职责:响应跳转
model.addAttribute("emps", emps);
return "show";
}
}
④show.jsp展示数据
⑤测试请求:user/show