SSM作为当今最流行的项目框架体系之一,必须熟练掌握其整合的过程。
整合过程(其实就是把配置文件写到一起的过程)
步骤:
1 .导入相关jar包
整合jar包我已经在我的资源上面上传,自行下载
2 .编写配置文件
web.xml——配置spring 配置spring mvc
<!-- spring配置文件 -->
<!--listener实例化监听根据键获取context-param的值 ,在web项目完全启动前就完成了
启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点,
它用于向 ServletContext 提供键值对,即应用程序上下文信息。-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<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>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
<!-- servlet中装载servlet的次序 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
mvc.xml ——spring mvc使用注解,如果需要json或其他处理须填写相关配置
<?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"
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">
<!--注解包扫描 -->
<context:component-scan base-package="cn.sxt.controller"/>
</beans>
applicationContext.xml——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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 读取属性文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties"></property>
</bean>
<!--配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!--配置工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.cfg.xml">
</bean>
<!-- 配置声明式事务 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="get" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* cn.sxt.service.impl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
<context:component-scan base-package="cn.sxt"></context:component-scan>
</beans>
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=123456
mybatis.cfg.xml——mybatis
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="cn.sxt.vo"/>
</typeAliases>
<mappers>
<!-- 所有mapper文件填写位置 -->
</mappers>
</configuration>
3 .编码
先建立包package,主要分为四大类
controller层,dao层,service 层,vo层
按我的顺序来
vo层
package cn.sxt.vo;
public class User {
private int id;
private String name;
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
user.mapper.xml(还是vo层下面)
<?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="cn.sxt.vo.user.mapper">
<select id="list" resultType="User">
select * from User
</select>
</mapper>
写完这个别忘记在mybatis.cfg.xml上填上mapper的位置,原来写配置文件的时候还不知道路径,看上述配置文件应该发现我在mybatis.cfg.xml上写了所有mapper文件填写位置 ,如下:
<mappers>
<!-- 所有mapper文件填写位置 -->
<mapper resource="cn/sxt/vo/user.mapper.xml"/>
</mappers>
dao层
package cn.sxt.dao;
import java.util.List;
import cn.sxt.vo.User;
public interface UserDao {
public List<User> list();
}
package cn.sxt.dao.impl;
import java.util.List;
import org.apache.ibatis.session.SqlSessionFactory;
import org.eclipse.jdt.internal.compiler.ast.SuperReference;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import cn.sxt.dao.UserDao;
import cn.sxt.vo.User;
@Repository("userDao")
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{
@Autowired
@Override
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
}
public List<User> list() {
return getSqlSession().selectList("cn.sxt.vo.user.mapper.list");
}
}
service层
package cn.sxt.service;
import java.util.List;
import cn.sxt.vo.User;
public interface UserService {
public List<User> list();
}
package cn.sxt.service.impl;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.sxt.dao.UserDao;
import cn.sxt.service.UserService;
import cn.sxt.vo.User;
@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public List<User> list() {
return userDao.list();
}
}
controller层
package cn.sxt.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.sxt.service.UserService;
@Controller
public class UserController {
private UserService userService;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
@RequestMapping("/list")
public String List(ModelMap map){
map.addAttribute("list", userService.list());
return "/index.jsp";
}
}
4 .写视图层jsp页面 index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.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 width="80%" align="center">
<tr>
<td>编号 </td>
<td>姓名 </td>
<td> 密码</td>
</tr>
<c:forEach items="${list }" var="bean" >
<tr>
<td>${bean.id}</td>
<td>${bean.name}</td>
<td>${bean.pwd}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
这就大功告成,一个简单的SSM整合案例就写好了。
上述用了注解自动装配bean,spring配置文件也就不用写了。
controll层 :@Controller
dao层:@Repository
service层:@Service