Spring MVC是一个优秀的Web框架,MyBatis是一个ORM数据持久化层框架,它们是两个独立的框架,之间没有直接的联系。由于Spring框架提供了loC 和AOP等相当实用的功能.若把Spring MVC和MyBatis的对象交给Spring 容器进行解耦合管理,不仅能大大增强系统的灵活性,便于功能扩展,还能通过Spring提供的服务简化编码,减少开发工作量,提高开发效率。SSM框架整合其实就是分别实现Spring与Spring MVC、Spring 与MyBais的整合,而实现整合的主要工作就是把Spring MVC、MyBatis 中的对象配置到Spring容器中,交给Spring来管理。当然对于Spring MVC框架来说,它本身就是Spring为展现层提供的MVC框架.所以在进行框架整合时,可以说Spring MVC与Spring是无缝集成,性能优越。
这次先简单整合一下,仅展示部分代码:
1、jar包
2、映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xyj.dao.EmpDao">
<select id="findAll" resultType="emp">
select * from emp
</select>
<delete id="delete">
delete from emp where eid=#{eid}
</delete>
<insert id="insert">
insert into emp values(default,#{ename},#{epwd},#{money})
</insert>
<select id="findById" resultType="emp">
select * from emp where eid=#{eid}
</select>
<update id="update">
update emp set ename=#{ename},epwd=#{epwd},money=#{money} where eid=#{eid}
</update>
</mapper>
3、Spring MVC的配置文件
<?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-4.0.xsd">
<!--自定义扫描包 -->
<context:component-scan base-package="com.xyj"></context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
4、Spring的配置文件
<?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-4.0.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///jdbcdemo20190804"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 创建会话工厂SqlSessionFactory -->
<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置别名 -->
<property name="typeAliasesPackage" value="com.xyj.entity"></property>
<!-- 设置映射文件路径 -->
<property name="mapperLocations" value="classpath*:mapper/*Mapper.xml"></property>
</bean>
<!-- 构建自动映射关系 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定会话工厂 -->
<property name="sqlSessionFactoryBeanName" value="SqlSessionFactory"></property>
<!-- 指定Dao接口扫描包 -->
<property name="basePackage" value="com.xyj.dao"></property>
</bean>
</beans>
5、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" version="3.1">
<display-name>SpringSpringMVCMyBatisDemo</display-name>
<!-- 字符集过滤器 -->
<filter>
<description>字符集过滤器</description>
<filter-name>encodingFilter</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>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置Spring的监听器 -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置Spring MVC的核心的核心控制器 DispatcherServlet -->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化参数 -->
<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>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
6、Dao层Service相似,仅是方法的声明,不再展示
7、Service的实现类:
package com.xyj.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.xyj.dao.EmpDao;
import com.xyj.entity.Emp;
@Service("empservice")
public class EmpServiceImpl implements EmpService{
@Resource
private EmpDao ed;
@Override
public List<Emp> findAll() {
return ed.findAll();
}
@Override
public String delete(int eid) {
int i = ed.delete(eid);
String result=i>0?"删除成功":"删除失败";
return result;
}
@Override
public String insert(Emp emp) {
int i = ed.insert(emp);
String result=i>0?"新增成功":"新增失败";
return result;
}
@Override
public Emp findById(int eid) {
Emp emp = ed.findById(eid);
return emp;
}
@Override
public String update(Emp emp) {
int i = ed.update(emp);
String result=i>0?"修改成功":"修改失败";
return result;
}
}
8、控制器层
package com.xyj.handler;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.xyj.entity.Emp;
import com.xyj.service.EmpService;
@Controller //标记当前类为控制器
public class TestHandler {
@Resource
private EmpService es;
@RequestMapping("/findAll")
public String findAll(Map<String,Object> map) {
map.put("emps", es.findAll());
return "findAll";
}
@RequestMapping("delete")
public void delete(int eid,HttpServletResponse res) {
myout(es.delete(eid), res);
}
@RequestMapping("toInsert")
public String toInsert() {
return "insert";
}
@RequestMapping("insert")
public void insert(Emp emp,HttpServletResponse res) {
myout(es.insert(emp), res);
}
@RequestMapping("findById")
public String findById(int eid,Map<String,Object> map) {
Emp emp = es.findById(eid);
map.put("emp",emp);
return "update";
}
@RequestMapping("update")
public void update(Emp emp,HttpServletResponse res) {
myout(es.update(emp), res);
}
public void myout(String msg,HttpServletResponse res) {
try {
res.setContentType("text/html;charset=utf-8");
res.getWriter().print("<script>alert('"+msg+"');location.href='findAll';</script>");
} catch (IOException e) {
e.printStackTrace();
}
}
}
9、jsp页面不再展示