ssm三大框架整合

前言

ssm三大框架整合主要指的是spring mvc+mybatis+spring,利用mvc分层思想实现解耦,其中spring mvc负责业务控制和跳转,mybatis负责持久层,spring管理各层,下面将以一个通过id查询用户信息功能搭建整合环境

  1. 建立一个web工程

  2. 导入ssm整合的相关jar包,包括spring(包含springmvc),mybatis,mybatis-spring整合。数据库驱动,jstl,c3p0管理数据源,log4j.由于整合包过多,可以自行到网上下载

  3. 配置核心文件初始化
    Web.xml配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>springmvc19_day01_01</display-name>
//配置中文乱码过滤器
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
//启用spring监听
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
//启动spring mvc
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 默认加载方式
默认加载必须规范:
* 文件命名:servlet-name-servlet.xml====springmvc-servlet.xml
* 路径规范:必须在WEB-INF目录下面
-->
</servlet>
//spring mvc 过滤文件
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
//rest格式拦截,主要用在提交带参数问题
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
  1. 加载springmvc-servlet.xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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:mvc="http://www.springframework.org/schema/mvc"
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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
//自动扫面根路径以下的所有包,就不要显示配置bean了
<context:component-scan base-package="com.zwl"></context:component-scan>
//默认创建注解处理器映射器,注解处理器适配器。提供json格式支持。
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置sprigmvc视图解析器:解析逻辑视图
后台返回逻辑试图:index
视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
  1. 加载spring配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?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:mvc="http://www.springframework.org/schema/mvc"
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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
//自动扫面根路径以下的所有包,就不要显示配置bean了
<context:component-scan base-package="com.zwl"></context:component-scan>
<!-- 第一步:配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:mysql端口/你的项目"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="你的用户名"></property>
<property name="password" value="你的密码"></property>
</bean>
<!-- 第二步:创建sqlSessionFactory。生产sqlSession,即产生会话 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置mybatis接口代理开发
* 接口类名和映射文件必须同名
* 接口类和映射文件必须在同一个目录 下
* 映射文件namespace名字必须是接口的全类路径名
* 接口的方法名必须和映射Statement的id一致
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zwl.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 第三步:事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 配置拦截service -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.zwl.service.*.*(..))"/>
</aop:config>
</beans>

以上三个包都必须在web-inf下

  1. 编写接口代码
    首先是pojo类
1
2
3
4
5
public class User {
private String name; //用户名
private String pwd; //密码
private int uid; //用户id
此处省略set、get方法。}

然后利用mapper代理模式开发dao层
首先是Usermapper.xml

1
2
3
4
5
6
7
8
9
10
11
<?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.zwl.dao.Usermapper">
<!-- 根据用户ID查询用户信息 -->
<select id="findUserById" parameterType="int" resultType="com.zwl.pojo.User">
SELECT
* FROM users WHERE uid =#{uid}
</select>
</mapper>

然后是接口

1
2
3
public interface Usermapper {
public User findUserById(int id) throws Exception;
}
  1. 编写service层
    这里用到spring注解依赖注入
1
2
3
4
5
public interface Userservice {
public User finduserbyid(Integer id) throws Exception;
}

实现类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//声明业务层为注解,为控制层依赖注入服务
@Service
public class Userserviceimpl implements Userservice {
//通过注解注入dao层组件
@Resource
private Usermapper usermapper;
@Override
public User finduserbyid(Integer id) throws Exception {
User user=usermapper.findUserById(id);
return user;
}
}
  1. 控制层
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//注解自动在spring中注入该bean
@Controller
//请求路径,放在开头,请求url,controller前必须加入该映射路径
@RequestMapping("/user")
public class Usercontrol {
//自动注入bean
@Resource
private Userservice userservice;
//请求路径,放在方法前,为请求url,到时根据这个找到类里相应方法
@RequestMapping("toadd")
public String toadd(){
//返回视图,在配置文件中已经配置了前缀和后缀,所以返回到index.jsp
return "index";
}
@RequestMapping("uid")
//这里的参数id必须和前台提交的参数name相同,才能接收到
public void finduserbyid(Integer id)
{
try {
User u=userservice.finduserbyid(id);
System.out.println(u.getName());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
  1. 页面
    index.jsp
1
2
3
4
<form action="${pageContext.request.contextPath }/user/uid.do" method="post">
id:<input type="text" name="id" id="id">
<input type="submit" value="提交">
</form>
  1. 数据库
    里面字段必须为uid,name和password,和pojo类相对应,并且里面有数据

最后我们可以在浏览器输入http://localhost:端口/项目名/user/toadd.do到index.jsp页面,最后通过输入id,后台输出用户信息

至此ssm整合完成,会发现它比ssh2更方便,特别是注解开发

热评文章