SSM整合思路
- spring : 業(yè)務(wù)層萧锉,管理service柿隙、dao禀崖、工具類對象
- springmvc:視圖層波附,界面層掸屡,負(fù)責(zé)接受請求仅财,顯示處理結(jié)果的满着。
- mybatis:持久層,訪問數(shù)據(jù)庫的缕探。
用戶發(fā)起請求-- springmvc接收---spring中的service對象---MaBatis處理數(shù)據(jù)
SSM整合也叫作SSI(IBatis也就是mybatis的前身)爹耗,整合中有容器潭兽。
- 第一個容器springmvc容器山卦,管理controller控制器對象 - 第二個容器spring容器账蓉,管理service逾一,dao工具類對象的
我們要做的把使用的對象交給合適的容器對象創(chuàng)建铸本,管理。把Controller還有web開發(fā)的相關(guān)對象遵堵,交給springmvc容器箱玷,這些web用的對象寫在springmvc配置文件中。
service陌宿,到定義在spring的配置文件中锡足,讓spring管理這些對象。
springmvc容器和spring容器是有關(guān)系的舱污,關(guān)系已經(jīng)確定好了呀舔。
springmvc容器是spring容器的子容器弥虐,類似java中的繼承。字可以訪問付的內(nèi)容媚赖。
在子容器中的Controler可以訪問父容器中的service對象霜瘪,就可以實現(xiàn)controller使用service對象、
- 實現(xiàn)步驟
- 使用springdb的mysql庫
- 新建maven web項目
- 加入依賴:
springmvc惧磺、spring颖对、mybatis、jackson磨隘、mysql缤底、druid顾患、jsp、servlet- 寫web.xml:
注冊DispathcherServlet: 創(chuàng)建springmvc容器對象个唧,才能創(chuàng)建Controller類對象江解,創(chuàng)建servlet,才能接受用戶的請求徙歼。
注冊spring監(jiān)聽器:ContextLoaderListener犁河,目的:創(chuàng)建spring容器對象,次啊能創(chuàng)建service魄梯,到等對象桨螺。
注冊字符集過濾器,解決post請求亂碼的問題酿秸。- 創(chuàng)建包灭翔,Controller包,service辣苏,dao缠局,實體類包名創(chuàng)建號
- 寫springmvc,spring考润,mabatis的配置文件
springmvc配置文件
spring配置文件
mybatis主配置文件
數(shù)據(jù)庫屬性的配置文件- 寫代碼 狭园,dao接口 mapper文件,service和實現(xiàn)類糊治,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>
<!--事務(wù)相關(guān)-->
<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">
<!-- 中央調(diào)度器-->
<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監(jiān)聽器-->
<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井辜, 訪問靜態(tài)資源-->
<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"/>
<!-- 聲明數(shù)據(jù)源绎谦,鏈接數(shù)據(jù)庫-->
<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>
<!-- 創(chuàng)建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掃描器,創(chuàng)建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>
<!-- 設(shè)置別名-->
<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 接口 實現(xiàn)類
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;
//注冊學(xué)生
@RequestMapping("/addStudent.do")
public ModelAndView addStudent(Student student){
ModelAndView view = new ModelAndView();
String tips;
// 調(diào)用service處理student
int i = service.addStudent(student);
if (i > 0){
// 注冊成功
tips = "學(xué)生{" + student.getName()+"}注冊成功";
}else {
tips = "注冊識別";
}
view.addObject("tips", tips);
view.setViewName("result");
return view;
}
// 處理查詢 窃肠,響應(yīng)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>注冊學(xué)生</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">注冊學(xué)生</a></td>
</tr>
<tr>
<td><a href="listStudent.jsp">瀏覽學(xué)生</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>查詢學(xué)生</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) {
//清楚舊數(shù)據(jù)
$("#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>學(xué)號</td>
<td>姓名</td>
<td>年齡</td>
</tr>
</thead>
<tbody id="info">
</tbody>
</table>
</div>
</body>
</html>