SSM整合思路
- spring : 业务层,管理service、dao、工具类对象
- springmvc:视图层,界面层,负责接受请求,显示处理结果的。
- mybatis:持久层,访问数据库的。
用户发起请求-- springmvc接收---spring中的service对象---MaBatis处理数据
SSM整合也叫作SSI(IBatis也就是mybatis的前身),整合中有容器。
- 第一个容器springmvc容器,管理controller控制器对象 - 第二个容器spring容器,管理service,dao工具类对象的
我们要做的把使用的对象交给合适的容器对象创建,管理。把Controller还有web开发的相关对象,交给springmvc容器,这些web用的对象写在springmvc配置文件中。
service,到定义在spring的配置文件中,让spring管理这些对象。
springmvc容器和spring容器是有关系的,关系已经确定好了。
springmvc容器是spring容器的子容器,类似java中的继承。字可以访问付的内容。
在子容器中的Controler可以访问父容器中的service对象,就可以实现controller使用service对象、
- 实现步骤
- 使用springdb的mysql库
- 新建maven web项目
- 加入依赖:
springmvc、spring、mybatis、jackson、mysql、druid、jsp、servlet- 写web.xml:
注册DispathcherServlet: 创建springmvc容器对象,才能创建Controller类对象,创建servlet,才能接受用户的请求。
注册spring监听器:ContextLoaderListener,目的:创建spring容器对象,次啊能创建service,到等对象。
注册字符集过滤器,解决post请求乱码的问题。- 创建包,Controller包,service,dao,实体类包名创建号
- 写springmvc,spring,mabatis的配置文件
springmvc配置文件
spring配置文件
mybatis主配置文件
数据库属性的配置文件- 写代码 ,dao接口 mapper文件,service和实现类,controller,实体类
- 写jsp页面
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lz</groupId>
<artifactId>ssm01-lz</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--jsp-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<!--springmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--事务相关-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
</dependencies>
<build>
<!--将src下以及resources目录下的properties、xml文件编译后写出到target目录-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
- web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 中央调度器-->
<servlet>
<servlet-name>myWeb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/dispatcherServlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myWeb</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 注册spring监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 注册字符集过滤器-->
<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>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
- springmvc.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- springmvc配置文件 , 声明组件扫描器-->
<context:component-scan base-package="com.lz.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 返回json, 访问静态资源-->
<mvc:annotation-driven/>
</beans>
- jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/door
jdbc.username=root
jdbc.password=root
- spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- spring配置文件 声明service dao 工具类对象-->
<context:property-placeholder location="classpath:conf/jdbc.properties"/>
<!-- 声明数据源,链接数据库-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 创建mybatis sqlsessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:conf/mybatis.xml"/>
</bean>
<!-- 声明mybatis扫描器,创建dao对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.lz.dao"/>
</bean>
<!-- 声明service注解 所在的包名位置-->
<context:component-scan base-package="com.lz.service"/>
<!-- 事物配置:注解配置,aspectj的配置-->
</beans>
- mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 设置别名-->
<typeAliases>
<!-- 实体类包名-->
<package name="com.lz.domain"/>
</typeAliases>
<!-- sql mapper映射位置-->
<mappers>
<!-- 使用package要求: mapper文件和dao接口名完全一样-->
<!-- 或者 mapper文件和dao接口必须在同一目录-->
<package name="com.lz.dao"/>
</mappers>
</configuration>
- dao层 mapper and StudentDao
public interface StudentDao {
int insertStudent(Student student);
List<Student> selectStudent();
}
<?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.lz.dao.StudentDao">
<select id="selectStudent" resultType="Student">
select id, name, age from student order by id desc
</select>
<insert id="insertStudent" >
insert into student(name, age) values (#{name}, #{age})
</insert>
</mapper>
- service 接口 实现类
public interface StudentService {
int addStudent(Student student);
List<Student> findStudent();
}
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public int addStudent(Student student) {
int nums = studentDao.insertStudent(student);
return nums;
}
@Override
public List<Student> findStudent() {
return studentDao.selectStudent();
}
}
- controller
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService service;
//注册学生
@RequestMapping("/addStudent.do")
public ModelAndView addStudent(Student student){
ModelAndView view = new ModelAndView();
String tips;
// 调用service处理student
int i = service.addStudent(student);
if (i > 0){
// 注册成功
tips = "学生{" + student.getName()+"}注册成功";
}else {
tips = "注册识别";
}
view.addObject("tips", tips);
view.setViewName("result");
return view;
}
// 处理查询 ,响应ajax
@RequestMapping("/queryStudent")
@ResponseBody
public List<Student> queryStudent(){
List<Student> students = service.findStudent();
return students;
}
}
- WEB/INF/jsp/result.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${requestScope.get("tips")}
</body>
</html>
- addStudent.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath = request.getScheme() + "://" +
request.getServerName() + ":" + request.getServerPort() +
request.getContextPath() + "/";
%>
<html>
<head>
<title>注册学生</title>
<base href="<%=basePath%>"/>
</head>
<body>
<div align="center">
<form action="student/addStudent.do">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="注册"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
- index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath = request.getScheme() + "://" +
request.getServerName() + ":" + request.getServerPort() +
request.getContextPath() + "/";
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>"/>
</head>
<body>
<div align="center">
<p>SSM整合</p>
<table>
<tr>
<td><a href="addStudent.jsp">注册学生</a></td>
</tr>
<tr>
<td><a href="listStudent.jsp">浏览学生</a></td>
</tr>
</table>
</div>
</body>
</html>
- listStudent.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath = request.getScheme() + "://" +
request.getServerName() + ":" + request.getServerPort() +
request.getContextPath() + "/";
%>
<html>
<head>
<title>查询学生</title>
<base href="<%=basePath%>">
<script src="js/jquery.min.js"></script>
<script type="text/javascript">
$.ajax({
url: "student/queryStudent.do",
type: "get",
dataType:"json",
success:function (data) {
//清楚旧数据
$("#info").html("")
$.each(data, function (i, n) {
$("#info").append("<tr>" +
"<td>" +n.id+"</td>"+
"<td>" +n.name+"</td>"+
"<td>" +n.age+"</td>"+
"</tr>")
})
}
})
</script>
</head>
<body>
<div align="center">
<table>
<thead>
<tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
</tr>
</thead>
<tbody id="info">
</tbody>
</table>
</div>
</body>
</html>