spring mvc 集成

Spring 集成 SSM整合

1.SSM集成包括?
1.1.Spring:基于IOC和AOP的容器框架,用来整合其他框架
1.2.MyBatis:基于ORM的数据库持久化框架
1.3.SpringMVC:表现层【controller + 页面】的框架
2.Spring集成思想?
2.1.把当前的框架类交给Spring来进行管理
3.Spring + Mybatis集成步骤
1.集成准备
	1.1.创建web项目,把项目部署到serlet中  (注意classes在WEB-INF下,修改编译路径)
	1.2.创建测试类
	1.3.导包(查看项目day041中的lib)
2.整合Spring和MyBatis
	2.1.创建数据库ssm
	2.2.导入ssm.sql文件【课件 - resources - 随堂资源 - ssm.sql】
3.创建资源文件夹(resources)文件夹并在其中编写jdbc.properties数据库配置文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///ssm
jdbc.username=root
jdbc.password=123456
4.在资源文件夹创建spring核心配置文件applicationContext.xml
	(1)将jdbc.propertie交给spring进行管理(以前是MyBatis管理)
	①解析jdbc.propertie文件
<!-- 解析jdbc.properties配置文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
5.Spring管理BasicDataSource
	(1)将连接池交个Spring去管理
<!-- 配置连接池  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>
6.测试BasicDataSource对象
@Autowired
		private DataSource dataSource;
7.编写实体类Emp(创建cn.itsource.domain包)
8.创建mapper接口(创建:cn.itsource.mapper包)
	(1)查询所有    List<Emp> findAll();
9.创建mapper映射文件
<mapper namespace="cn.itsource.mapper.EmpMapper">
	//查询所有
	<select id="findAll" resultType="cn.itsource.Emp">
		select * from emp
	</select>
</mapper>
10.Spring管理SqlSessionFactory
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 获取数据源 -->
		<property name="dataSource" ref="dataSource"></property>		
		<!-- 配置别名 -->
		<property name="typeAliasesPackage" value="cn.itsource.domain"></property>
		<!-- 加载mapper文件 -->
		<propertyname="mapperLocations"value="classpath:cn/itsource/mapper/*Mapper.xml"></property>
</bean>
11.测试sqlSessionFactory
@Autowired
	private SqlSessionFactory Session;
10.Spring管理Mapper接口
<!-- 配置mapper接口 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.itsource.mapper"></property>
	</bean>
11.测试Mapper
//EmpMapper 是接口不能创建对象  所只能使用代理来创建
@Autowired
	private EmpMapper mapper;
12.写service层
13.测试
4.Spring+Mybatis+SpringMvc集成
1.配置web.xml
	(1)前端控制器
	(2)过滤器,解决中文乱码问题
	(3)监听器
	(4)核心配置文件路径

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<!-- 监听器 配置Spring和Tomcat一起启动 -->
	<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>
		<!-- 加载spring-mvc配置文件 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.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>
2.配置spring-mvc.xml文件
(1)静态资源放行
(2)扫描注解
(3)注解生效
(4)视图解析器

spring-mvc.xml

<?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
 						http://www.springframework.org/schema/beans/spring-beans.xsd
 						http://www.springframework.org/schema/context
 						http://www.springframework.org/schema/context/spring-context.xsd
 						http://www.springframework.org/schema/mvc
 						http://www.springframework.org/schema/mvc/spring-mvc.xsd
 	">
	<!-- 注解扫描 -->
	<context:component-scan base-package="cn.itsource"></context:component-scan>
	<!-- 静态资源放行 -->
	<mvc:default-servlet-handler/>
	<!-- 注解生效 -->
	<mvc:annotation-driven />
	<!-- 试图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>	
</beans>

3.编写Controller

@Controller
@RequestMapping("/emp")
public class EmpController {
	@Autowired
	private IEmpService empService;
	@RequestMapping("/emp_edit")
	public String findOne(Integer id,Map<String, Object> map) {
		Emp emp = empService.findOne(id);
		map.put("emp", emp);
		return "emp_edit";
	}
5.动态Sql
	1.特殊字符
	①在xml的转义字符:
1)符号:<【&lt;】、<=【&lt;=】、>【&gt;】、>=【&gt;=】、&【&amp;】、'【&apos;】 、 【&quot;】  
2)【gt -> greater than(大于 )lt -> less than(小于)】   
2.模糊查询 if标签、where标签 、Choose标签、when标签使用
	(1)用mysql中字符串拼接函数concat,测试成功,也不会出现SQL注入,建议使用
	(2)If标签:做判断使用  满足条拼接  不满足不拼接
	(3)Where标签:做条件使用 拼接了一个where条件
	(4)Choose标签:多层判断,满足一个的拼接
	(5)When标签:Choose中的一个小条件
<select id="findAll" resultType="cn.itsource.domian.Emp">
		select * from emp
		<where>
			<if test="ename !=null and ''!=ename.trim()">
				and ename like concat('%',trim(#{ename}),'%')
			</if>
			<if test="address !=null and !''.equals(address.trim())">
				and address like concat('%',trim(#{address}),'%')
			</if>
			<if test="deptno !=null">
				and deptno=#{deptno}
			</if>
			<if test="sal">
				<choose>
					<when test="sal==3000.0">
						and sal &lt; 3000.0
					</when>
					<when test="sal==5000.0">
						and sal &gt;= 3000.0 and sal &lt; 5000.0
					</when>
					<when test="sal==8000.0">
						and sal &gt;= 5000.0 and sal &lt; 8000.0
					</when>
					<when test="sal==8001.0">
						and sal &gt;= 8000.0
					</when>
				</choose>
			</if>
		</where>
	</select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值