自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onLayout源码详尽分析 自定义View系列教程04–Draw源码分析及其实践 自定义View系列教程05–示例分析 自定义View系列教程06–详解View的Touch事件处理 自定义View系列教程07–详解ViewGroup分发Touch事件 自定义View系列教程08–滑动冲突的产生及其处理
探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制
Android多分辨率适配框架(1)— 核心基础 Android多分辨率适配框架(2)— 原理剖析 Android多分辨率适配框架(3)— 使用指南
之前我们学习了Mybatis和SpringMVC,现在我们将它们与Spring进行整合,搭建起SSM框架
整合思路
先来看一下在SSM框架中各部分的作用及其相互衔接
从这幅图中我们可以看出来:利用Spring将各层进行整合
通过Spring管理表现层的controller(handler)通过Spring管理业务层service通过Spring管理持久层的mapper(dao)通过Spring进行事务控制
整合之后,执行流程为: 请求—–>controller—–>service—–>mapper—–>数据库
所以,我们的整合过程可以大概分为三步
第一步:整合mapper(dao) 通过Spring管理mapper接口。比如:可使用mapper扫描器自动扫描mapper接口并在Spring中进行注册。
第二步:整合service 通过Spring管理 service接口。比如:可将service接口配置在Spring配置文件中;并在service层实现事务控制。
第三步:整合SpringMVC SpringMVC是spring的模块,故不需要整合。
环境准备
在此准备搭建SSM框架所需的开发环境
准备Jar包
在SSM框架中用到的jar稍多,但是大部分都是属于SpringMVC或者MyBatis的,其余少数属于整合时所必须的。
准备表
CREATE TABLE student(
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
gender VARCHAR(10),
birthday DATE
);
我们建立数据库mb,并创建一张表student并为其插入数据。
嗯哼,在完成这些最基本的要素之后,我们开始整合SSM框架;最终它所呈现的结构如上图所示。
整合mapper(dao)
Student.java
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
package cn.com.bean;
import java.util.Date;
public class Student {
private int id;
private String name;
private String gender;
private Date birthday;
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
getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date
getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String
toString() {
return "Student [id=" + id +
", name=" + name +
", gender=" + gender
+
", birthday=" + birthday +
"]";
}
}
StudentMapper.java
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
package cn.com.mapper;
import java.util.List;
import cn.com.bean.Student;
public interface StudentMapper {
public Student
findStudentById(
int id);
public List<Student>
findStudentByName(String name);
public void insertStudent(Student student);
public void deleteStudent(
int id);
public void updateStudent(Student student);
}
StudentMapper.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 namespace="cn.com.mapper.StudentMapper">
<select id="findStudentById" parameterType="int" resultType="Student">
SELECT * FROM student WHERE id=#{value}
</select>
<select id="findStudentByName" parameterType="java.lang.String" resultType="Student">
SELECT * FROM student WHERE name LIKE '%${value}%'
</select>
<insert id="insertStudent" parameterType="Student">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO student (name,gender,birthday) value (#{name},#{gender},#{birthday})
</insert>
<delete id="deleteStudent" parameterType="java.lang.Integer">
DELETE FROM student where id=#{id}
</delete>
<update id="updateStudent" parameterType="Student">
UPDATE student set name=#{name},gender=#{gender},birthday=#{birthday} where id=#{id}
</update>
</mapper>
sqlMapConfig.xml
<?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.com.bean" />
</typeAliases>
</configuration>
除了别名,以前我们在配置sqlMapConfig.xml还需要配置mapper。现在使用Spring和MyBatis的整合包进行mapper扫描,故此处不需要再进行额外配置了;但是请注意:mapper.xml和mapper.java的文件名必须一致,并且且在一个目录 下。比如,该示例中的StudentMapper.java和StudentMapper.xml均在cn.com.mapper包下。
db.properties
jdbc
.driver=
com.mysql.jdbc.Driver
jdbc
.url=jdbc:mysql://localhost:
3306/mb?characterEncoding=utf-
8
jdbc
.username=root
jdbc
.password=root
在该文件中配置数据库链接信息。请注意:为了方便整合,每个键请以jdbc.开头
applicationContext-dao.xml
<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 ">
<context:property-placeholder location="classpath:db.properties" />
<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="maxActive" value="30" />
<property name="maxIdle" value="5" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.com.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
在该配置文件中:加载db.properties文件中的内容,配置dbcp数据源,配置sqlSessionFactory,配置mapper扫描器;这些内容都与我们的mapper(dao)紧密相关
整合Service
定义Service接口
/**
* @author 原创作者:谷哥的小弟
* @blog 博客地址:http://blog.csdn.net/lfdfhl
* @time 创建时间:2017年8月4日14:24:58
* @info 描述信息:StudentService
*/
package cn.com.service;
import cn.com.bean.Student;
public interface StudentService {
public void deleteStudentByStudentID(
int id);
public Student
findStudentByStudentID(
int id);
public void insertStudent(Student student);
public void updateStudent(Student student);
}
定义增删改查接口
实现Service接口
/**
* @author 原创作者:谷哥的小弟
* @blog 博客地址:http://blog.csdn.net/lfdfhl
* @time 创建时间:2017年8月4日14:26:26
* @info 描述信息:StudentServiceImpl
*/
package cn.com.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import cn.com.bean.Student;
import cn.com.mapper.StudentMapper;
import cn.com.service.StudentService;
public class StudentServiceImpl implements StudentService{
@Autowired
StudentMapper studentMapper;
@Override
public void deleteStudentByStudentID(
int id) {
studentMapper.deleteStudent(id);
}
@Override
public Student
findStudentByStudentID(
int id) {
Student student=studentMapper.findStudentById(id);
return student;
}
@Override
public void insertStudent(Student student) {
studentMapper.insertStudent(student);
}
@Override
public void updateStudent(Student student) {
studentMapper.updateStudent(student);
}
}
实现增删改查接口
applicationContext-service.xml
<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:
http:
http:
http:
http:
http:
http:
http:
http:
<bean id=
"studentService" class=
"cn.com.service.impl.StudentServiceImpl" />
</beans>
在applicationContext-service.xml文件中配置service。
applicationContext-transaction.xml
<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 id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.com.service.impl.*.*(..))" />
</aop:config>
</beans>
在该配置文件中:配置MyBatis操作数据库的事务管理器,配置通知,配置aop。
整合SpringMVC
嗯哼,之前我们也说了:SpringMVC就是Spring的一部分,所以无需单独整合。故,我们在此完成该部分对应的Controller即可。
StudentController
/**
* @author 原创作者:谷哥的小弟
* @blog 博客地址:http://blog.csdn.net/lfdfhl
* @time 创建时间:2017年8月3日14:31:19
* @info 描述信息:SSM框架整合
*/
package cn.com.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
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.com.bean.Student;
import cn.com.service.StudentService;
@Controller
@RequestMapping(
"/student")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(
"insert")
public String
insertStudent(String name,String gender,String birthday,ModelMap modelMap)
throws Exception{
Student student=
new Student();
student.setName(name);
student.setGender(gender);
SimpleDateFormat simpleDateFormat=
new SimpleDateFormat(
"yyyy-MM-dd");
Date date=simpleDateFormat.parse(birthday);
student.setBirthday(date);
studentService.insertStudent(student);
modelMap.put(
"insertInfo",
"添加成功");
return "result";
}
@RequestMapping(
"delete")
public String
deleteStudent(
int id,ModelMap modelMap){
studentService.deleteStudentByStudentID(id);
modelMap.put(
"deleteInfo",
"删除成功");
return "result";
}
@RequestMapping(
"update")
public String
updateStudent(String id,String name,String gender,String birthday,ModelMap modelMap)
throws Exception{
Student student=
new Student();
student.setId(Integer.valueOf(id));
student.setName(name);
student.setGender(gender);
SimpleDateFormat simpleDateFormat=
new SimpleDateFormat(
"yyyy-MM-dd");
Date date=simpleDateFormat.parse(birthday);
student.setBirthday(date);
studentService.updateStudent(student);
modelMap.put(
"updateInfo",
"更新成功");
return "result";
}
@RequestMapping(
"find")
public String
findStudent(
int id,ModelMap modelMap){
Student student=studentService.findStudentByStudentID(id);
modelMap.put(
"student", student);
return "result";
}
}
在该Controller中实现了对于Student的增删改查
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: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">
<context:component-scan base-package="cn.com"></context:component-scan>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml
嗯哼,经过之前的代码编写,我们已经完成了SSM框架的各主要部分了。现在,我们需要在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">
<context-param>
<param-name>contextConfigLocation
</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext-*.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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:springmvc.xml
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc
</servlet-name>
<url-pattern>*.do
</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp
</welcome-file>
</welcome-file-list>
<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>
</web-app>
在该配置文件中:加载Spring配置文件,配置DispatcherServlet,处理乱码。请注意对于Spring配置文件的加载,在此采用了通配符的方式(第11行代码)加载了三个配置文件:applicationContext-dao.xml,applicationContext-service.xml,applicationContext-transaction.xml从而将mapper、service、controller加载到Spring容器中
部署测试
index.jps
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>springMVC
</title>
<style type="text/css">
p {
font-size: 30px;
font-family: 宋体;
color: red;
background-color: pink;
}
</style>
</head>
<body>
<hr size="2" color="red" />
<b>添加学生
</b><br><br>
<form action="${pageContext.request.contextPath }/student/insert.do" method="post">
姓名:
<input type="text" name="name" id="testName">
性别:
<input type="text" name="gender" id="testGender">
生日:
<input type="text" name="birthday" id="testBirthday"> <br><br>
<input type="submit" value="提交">
</form>
<hr size="2" color="red" />
<b>依据ID删除学生信息
</b><br><br>
<form action="${pageContext.request.contextPath }/student/delete.do" method="post">
ID:
<input type="text" name="id" id="testIntId"> <br><br>
<input type="submit" value="提交">
</form>
<hr size="2" color="red" />
<b>修改学生信息
</b><br><br>
<form action="${pageContext.request.contextPath }/student/update.do" method="post">
ID:
<input type="text" name="id" id="testIntId">
姓名:
<input type="text" name="name" id="testName">
性别:
<input type="text" name="gender" id="testGender">
生日:
<input type="text" name="birthday" id="testBirthday"> <br><br>
<input type="submit" value="提交">
</form>
<hr size="2" color="red" />
<b>依据ID查询学生信息
</b><br><br>
<form action="${pageContext.request.contextPath }/student/find.do" method="post">
ID:
<input type="text" name="id" id="testIntId"> <br><br>
<input type="submit" value="提交">
</form>
</body>
</html>
效果图如下:
result.jps
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SSM框架整合
</title>
<style type="text/css">
p {
font-size: 20px;
font-family: 宋体;
color: red;
background-color: pink;
}
</style>
</head>
<body>
<p>添加学生
</p>
<p>添加结果:${insertInfo}
</p>
<p>依据ID删除学生信息
</p>
<p>删除结果:${deleteInfo}
</p>
<p>修改学生信息
</p>
<p>修改结果:${updateInfo}
</p>
<p>依据ID查询学生信息
</p>
<p>查询结果:${student.id} ${student.name} ${student.gender} ${student.birthday}
</p>
</body>
</html>
效果图如下:
小结
关于SSM框架的整合和搭建,其实各部分的纽带就是各种配置文件;而且每层都有与其对应的配置文件。
mapper(dao):db.properties和sqlMapConfig.xml以及applicationContext-dao.xmlservice:applicationContext-service.xml和applicationContext-transaction.xmlSpringMVC:springmvc.xml
这些配置文件最终会在web.xml中被加载。