最近刚把spring的源码过了一遍,但总感觉看完不能就这么结束了,得做点什么留下个脚印,所以自己基于maven+springMVC+mybatis+velocity+mysql+junit搭建个工程项目,底下有具体代码下载地址,并且做了对应框架的代码生成工具,代码生成工具单独介绍。
对于springmvc的流程借用网上的流图:
Spring MVC工作流程图
图一
图二
Spring工作流程描述
1. 用户向服务器发送请求,请求被Spring 前端控制Servelt DispatcherServlet捕获;
2.
DispatcherServlet对请求URL进行解析,得到请求资源标识符(URI)。然后根据该URI,调用HandlerMapping获得该Handler配置的所有相关的对象(包括Handler对象以及Handler对象对应的拦截器),最后以HandlerExecutionChain对象的形式返回;
3.
DispatcherServlet 根据获得的Handler,选择一个合适的HandlerAdapter。(附注:如果成功获得HandlerAdapter后,此时将开始执行拦截器的preHandler(...)方法)
4. 提取Request中的模型数据,填充Handler入参,开始执行Handler(Controller)。 在填充Handler的入参过程中,根据你的配置,Spring将帮你做一些额外的工作:
HttpMessageConveter: 将请求消息(如Json、xml等数据)转换成一个对象,将对象转换为指定的响应信息
数据转换:对请求消息进行数据转换。如String转换成Integer、Double等
数据根式化:对请求消息进行数据格式化。 如将字符串转换成格式化数字或格式化日期等
数据验证: 验证数据的有效性(长度、格式等),验证结果存储到BindingResult或Error中
5. Handler
执行完成后,向DispatcherServlet 返回一个ModelAndView对象;
6. 根据返回的ModelAndView,选择一个适合的ViewResolver(必须是已经注册到Spring容器中的
ViewResolver)返回给DispatcherServlet ;
7.
ViewResolver 结合Model和View,来渲染视图
8. 将渲染结果返回给客户端。
这次搭建了两个工程项目,一个是生成jar的,一个是生成web工程的,两个工程底层区别不大,只是一个生成jar一个生成war的区别,下面一一做个搭建简单的介绍(spring具有很大的灵活性,并且项目工程框架有多种模式,这里不做讨论):
1、jar工程:
1.1工程目录结构
工程目录结构如下:
1.2 dao层介绍
这里dao包是映射数据库操作,具体的实现基于mybatis
实现了基本的操作功能,例如:
package com.spring.mybatis.user.web.dao;
import com.spring.mybatis.user.web.entity.DemoEntity;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.annotations.Param;
/**
* ClassName:DemoDao<br>
* Description:数据库中 t_demo表对应的实体类 .<br>
* @author auto
* @Date 20161106115159
* @since JRE 1.6.0_22 or higher
*/
public interface DemoDao {
/**
* 根据主键id删除数据
* @param id
* @return
*/
int deleteByPrimaryKey(Long id);
/**
* 插入数据
* @param entity
* @return
*/
int insert(DemoEntity entity);
/**
* 根据已有数据的列插入数据
* @param record
* @return
*/
int insertSelective(DemoEntity entity);
/**
* 根据主键id获取实体
* @param id
* @return
*/
DemoEntity selectByPrimaryKey(Long id);
/**
* 根据已有数据的列更新数据
* @param entity
* @return
*/
int updateByPrimaryKeySelective(DemoEntity entity);
/**
* 更新实体信息
* @param entity
* @return
*/
int updateByPrimaryKey(DemoEntity entity);
/**
* 根据条件进行查询,此处只是做一个demo
* @param offset
* @param size
* @return
*/
List<DemoEntity> getListByPage(@Param(value="offset")int offset,@Param(value="size")int size);
}
1.3service层
service层是业务逻辑层,这里映射了数据库dao层的操作,具体可参看代码
package com.spring.mybatis.user.web.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.spring.mybatis.user.web.dao.DemoDao;
import com.spring.mybatis.user.web.entity.DemoEntity;
import com.spring.mybatis.user.web.service.DemoService;
/**
* @author Administrator
*
*/
@Service
public class DemoServiceImpl implements DemoService {
@Resource
private DemoDao demoDao;
@Override
public int deleteByPrimaryKey(Long id) {
// TODO Auto-generated method stub
return demoDao.deleteByPrimaryKey(id);
}
@Override
public int insert(DemoEntity record) {
// TODO Auto-generated method stub
return demoDao.insert(record);
}
@Override
public int insertSelective(DemoEntity record) {
// TODO Auto-generated method stub
return demoDao.insertSelective(record);
}
@Override
public DemoEntity selectByPrimaryKey(Long id) {
// TODO Auto-generated method stub
return demoDao.selectByPrimaryKey(id);
}
@Override
public int updateByPrimaryKeySelective(DemoEntity record) {
// TODO Auto-generated method stub
return demoDao.updateByPrimaryKeySelective(record);
}
@Override
public int updateByPrimaryKey(DemoEntity record) {
// TODO Auto-generated method stub
return demoDao.updateByPrimaryKey(record);
}
@Override
public List<DemoEntity> getListByPage(int offset, int size) {
// TODO Auto-generated method stub
return demoDao.getListByPage(offset, size);
}
}
1.4数据库连接配置
对于数据库的连接基于properties配置文件,同时基于spring实现连接池
1.5代码测试配置了基础的实现,并配置日志实现打印sql语句
package com.spring.mybatis.user.web.service;
import static org.junit.Assert.fail;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.spring.mybatis.user.web.entity.DemoEntity;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"})
public class DemoServiceTest {
@Resource
DemoService demoService;
@Test
public void testDeleteByPrimaryKey() {
fail("Not yet implemented");
}
@Test
public void testInsert() {
fail("Not yet implemented");
}
@Test
public void testInsertSelective() {
fail("Not yet implemented");
}
@Test
public void testSelectByPrimaryKey() {
demoService.selectByPrimaryKey(1l);
List<DemoEntity> demoEntities =demoService.getListByPage(0,3);
// List<DemoEntity> demoEntities =demoService.getListByWhere(null);
System.out.println(demoEntities.size());
}
@Test
public void testUpdateByPrimaryKeySelective() {
fail("Not yet implemented");
}
@Test
public void testUpdateByPrimaryKey() {
fail("Not yet implemented");
}
}
2016-11-06 13:55:03,045 DEBUG [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
2016-11-06 13:55:03,045 DEBUG [org.mybatis.spring.SqlSessionUtils] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@42858616] was not registered for synchronization because synchronization is not active
2016-11-06 13:55:03,045 DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
2016-11-06 13:55:03,046 DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [ProxyConnection[PooledConnection[com.mysql.jdbc.JDBC4Connection@5f383d56]]] will not be managed by Spring
2016-11-06 13:55:03,046 DEBUG [com.spring.mybatis.user.web.dao.DemoDao.selectByPrimaryKey] - ==> Preparing: select id,name,type from t_demo where id = ?
2016-11-06 13:55:03,046 DEBUG [com.spring.mybatis.user.web.dao.DemoDao.selectByPrimaryKey] - ==> Parameters: 1(Long)
2016-11-06 13:55:03,048 DEBUG [com.spring.mybatis.user.web.dao.DemoDao.selectByPrimaryKey] - <== Total: 1
2016-11-06 13:55:03,049 DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@42858616]
2016-11-06 13:55:03,049 DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
2016-11-06 13:55:03,049 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Rendering view [org.springframework.web.servlet.view.velocity.VelocityLayoutView: name 'demo'; URL [demo.vm]] in DispatcherServlet with name 'springMvc'
2、web工程介绍
对于web工程的介绍简化下
2.1web工程框架
2.1controller层
controller层进行请求的拦截处理
项目中做了个详细的demo
package com.spring.mybatis.user.web.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.spring.mybatis.user.web.entity.DemoEntity;
import com.spring.mybatis.user.web.service.DemoService;
@Controller
@RequestMapping("/demo")
public class DemoController {
@Resource
private DemoService demoService;
@ResponseBody
@RequestMapping("/info")
public String getInfo(HttpServletRequest request,
HttpServletResponse response) {
String username = request.getParameter("username");
return "fjsh web";
}
// http://localhost:8080/com_spring_mybatis_user_web/demo/basicjsoninfo
@ResponseBody
@RequestMapping("/basicjsoninfo")
public String getbasicInfo(HttpServletRequest request,
HttpServletResponse response) {
DemoEntity demoEntity = demoService.selectByPrimaryKey(1l);
return JSON.toJSONString(demoEntity);
}
// http://localhost:8080/com_spring_mybatis_user_web/demo/basicjson
@ResponseBody
@RequestMapping(value = "/basicjson", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
public String getbasicjsonInfo(HttpServletRequest request,
HttpServletResponse response) {
DemoEntity demoEntity = demoService.selectByPrimaryKey(1l);
return JSON.toJSONString(demoEntity);
}
// http://localhost:8080/com_spring_mybatis_user_web/demo/velocity
@RequestMapping("/velocity")
public String showUser(ModelMap modelMap, HttpServletRequest request) {
modelMap.put("user", "fjsh");
return "demo";
}
// http://localhost:8080/com_spring_mybatis_user_web/demo/showinfbyid?id=2
@RequestMapping("showinfbyid")
public String showUserEntity(String id, ModelMap modelMap,
HttpServletRequest request) {
DemoEntity demoEntity = demoService
.selectByPrimaryKey(Long.valueOf(id));
modelMap.put("user", demoEntity.getName());
return "demo";
}
// http://localhost:8080/com_spring_mybatis_user_web/demo/demoredir
@RequestMapping("/demoredir")
public String showUsers(ModelMap modelMap, HttpServletRequest request) {
return "redirect:/demo/velocity";
}
// http://localhost:8080/com_spring_mybatis_user_web/demo/demolist
@RequestMapping("/demolist")
public String list(ModelMap model, HttpServletRequest request) {
model.put("list", demoService.getListByPage(0, 10));
return "list";
}
// http://localhost:8080/com_spring_mybatis_user_web/demo/1
@RequestMapping("/{id}")
public String detail(@PathVariable(value = "id") String id, ModelMap model) {
DemoEntity demoEntity = demoService
.selectByPrimaryKey(Long.valueOf(id));
model.put("user", demoEntity.getName());
return "demo";
}
}
具体请求路径已经加载了代码注释中,自行配置tomcat跑起来进行验证
2.2 spring配置文件结构
对于spring的配置文件结构做了切分处理,在公司工作留下的习惯
spring-mvc是用来springmvc环境配置,将其配置在web.xml中,web工程启动时进行加载
spring-dao是对数据库连接进行配置,包括事务操作也在这进行配置
srping-service 是通过配置方式指定代码中具体的service实现类,即为所谓的ioc。
spring.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:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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"
default-autowire="byName">
<context:property-placeholder location="classpath*:*.properties" ignore-unresolvable="true"/>
<context:component-scan base-package="com.spring.mybatis.user.web.*" />
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
</beans>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 p:prefix中模板放置路径 -->
<bean id="velocityConfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/view/" />
<property name="velocityProperties">
<props>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="" />
<property name="layoutUrl" value="layout.vm" />
<property name="suffix" value=".vm" />
<property name="contentType"><value>text/html;charset=UTF-8</value></property>
</bean>
2.4页面浏览效果
通过tomcat启动后效果例子如下:
项目工程的下载地址如下:
项目是全新搭建,过程遇到了很多坑,包括基本的中文传输,mybatis参数映射,多条件查询等,欢迎相互讨论
项目工程介绍比较粗糙,后期慢慢完善