SSM框架的简单整合

时间:2023-02-10 15:19:46

抽空搭建了一个SSM的简易框架,目的在于能够描述SSM的搭建流程。

   SSM框架由Spring+SpringMVC+mybatis组成,这三个框架是现在比较流行的框架,Spring配合SpringMVC实现了无缝连接,mybatis相对其他的持久层框架更加简洁,实用。对于一般的项目,mybatis基本上足够了。

   这三个框架以后有精力会写写详细博客,这里就这样简单介绍了。


一.SSM框架所用到的包

1.  Spring的jar

spring-aop;spring-core

2.  Mybatis的jar

dbcp;mysql-connector; spring-pesistence,mybatis

3.  Springmvc

Spring-WEB

SSM框架的简单整合


二.创建一个项目

1.  创建web项目,我用的是MyEclipse,这里是在项目栏中右键-new-web Project,

SSM框架的简单整合

2.  Project name 可以随意定,看个人情况

SSM框架的简单整合

3.  创建完成



三.搭建spring框架

1.  导入spring的jar

为了清楚,这里导包是一个个导的。

此处导入spring-aop,spring-core

2. 写一个简单的bean.

SSM框架的简单整合

3.  编写spring-config.xml

SSM框架的简单整合

4.  Junit4测试(出现了bean中的spring success,说明spring框架已经搭建了最简单的IOC功能)

SSM框架的简单整合


(JUnit4的使用方法:

SSM框架的简单整合

)


5.利用注解进行导包

SSM框架的简单整合

SSM框架的简单整合


spring-config中加入了context:component-scan后会自动扫描路径下用@Controller,@Service等注解标注的bean

四.mybatis框架的搭建

1.  导包

dbcp;mysql;mybatis;spring-peresistence

2.  编写jdbc.properties

SSM框架的简单整合

3.  编写spring-source.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">

<!-- PropertyPlaceholderConfigurer的作用就是导入其他的配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config/jdbc.properties"/>
</bean>

<!-- dataSource 这里使用的是DBCP数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${jdbc.initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${jdbc.maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${jdbc.minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${jdbc.maxWait}"></property>
</bean>

</beans>


4.  编写spring-mybatis.xml

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


<!-- 此处用于配置sqlSessionFactory的配置和依赖 -->
<!-- mapperLocation用于配置mapper.xml的查找路径 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:cn/it/mapper/xml/*.xml"/>
</bean>


<!-- 这里用于扫描mapper,basePackage指定的路径下的包会自动扫描,然后利用动态代理实现mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.it.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>


<!-- 事务管理 -->
<!-- 可以理解为将JDBC的提交事务, 交给了spring来完成-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>

5.  整合(spring-config.xml)

<?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: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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- <bean id="User" class="cn.it.controller.UserController"/> -->
<!-- 自动扫描Bean -->
<context:component-scan base-package="cn.it" />
<!-- 数据库 -->
<import resource="classpath:config/spring-source.xml"/>
<!-- mybatis -->
<import resource="classpath:config/spring-mybatis.xml"/>
</beans>

6.  编写相关的类

1)sql语句

UserSql
//创建表,有id,name,age三个字段
create table user(
id varchar(20),
name varchar(20),
age varchar(20)
)charset=utf8;

//插入值
insert into user (id,name,age) values (1,'小明',20);
insert into user (id,name,age) values (2,'小李',21);
insert into user (id,name,age) values (3,'小张',22);
2)model----user.java(实体类,用于存储数据库中表的数据)

public class User {
private String id;
private String name;
private String age;


public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}


}
3)mapper----usermapper.java

public interface UserMapper {
List<User> getUser();
}
4)mapper.xml---usermapper.xml

<?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类 -->
<mapper namespace="cn.it.mapper.UserMapper" >
<!-- id要与mapper中对应的方法名相同,resultType表示映射到哪个bean中 -->
<select id="getUser" resultType="cn.it.model.User">
select * from user
</select>
</mapper>

5)相关service类(因为只是去测试mybatis是否整合成功,所以service足以)

//这是一个service类,这个类被事务管理
@Service
@Transactional
public class UserService {

@Resource
private UserMapper userMapper;

public List<User> getUser(){
return (List<User>) userMapper.getUser();
}
}

7.  测试


SSM框架的简单整合

五.整合springMVC

1.  整合springMVC包

spring-web.jar

2.  配置spring-mvc.xml

<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
">

<!-- 自动扫描controller层Bean -->
<context:component-scan base-package="cn" />


<!-- 启动SpringMVC的注解功能,@ReuqestMapping等-->
<mvc:annotation-driven />

<!-- 静态资源 ,可以理解为网页的静态资源去哪访问-->
<mvc:resources mapping="/img/**" location="/img/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/html/**" location="/html/" />


<!-- 视图解析,为其加入前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

3.  配置web.xml(web.xml是服务器的进入接口)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

<!-- 上下文配置文件路径 -->
<!-- 此处配置不正确会自动去寻找 ApplicationContext.xml-->
<!-- 然后一般会报错,找不到 ApplicationContext.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-config.xml</param-value>
</context-param>

<!-- springd的监听器 -->
<!-- 随便一提,web中的加载顺序context-param –> listener –> filter –> servlet -->
<!-- 启动Web容器时,自动装配ApplicationContext的配置信息 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 配置DispatcherServlet -->
<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:config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>

4.  编写jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'User.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<table border="1">
<tbody>
<tr>
<th>id</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<!-- 注意:这里使用了标签库,所以在前面要写出来,仔细看上面的,有一个taglib,表示使用标签库 -->
<c:if test="${!empty User }">
<c:forEach items="${User}" var="list">
<tr>
<td>${list.id }</td>
<td>${list.name }</td>
<td>${list.age }</td>

</tr>
</c:forEach>
</c:if>
</tbody>
</table>
</body>
</html>

5.  修改controller

@Controller
@RequestMapping("/user")
public class UserController {

/*
public void test(){
System.out.println("spring success");
}
*/

@Autowired
private UserService userService;

//requestMapping用于指定映射路径,这里可以看成当路径为/user/findUser时将会被映射到此方法
@RequestMapping("findUser")
public String getUser(HttpServletRequest request){
//用List接收结果
List<User> user = userService.getUser();
//把结果放到相应的request域中
request.setAttribute("User", user);
//返回一个视图
return "/User";
}


}


6.  测试

开启Tomcat

在网页上输入

http://localhost:8080/spring_mybatis_springmvc/user/findUser

SSM框架的简单整合

六.总结

SSM框架的简单整合
这个最简单的SSM框架就搭建完毕了,以后我们可以根据这个框架进行各类框架的整合,例如shiro等。个人搭建框架喜欢一步步来,因为出现错误时可以把范围缩小,更好查找。