本文章知道简单的能够搭建Sping MVC环境进行代码基础开发。
根据Spring的体系结构图画出Spring开发过程中涉及的文件和交流细节:
Spring MVC流程说明:
用户请求http://127.0.0.1:8080/user/login
根据org.springframework.web.servlet.DispatcherServlet配置的xml文件找到相应处理器
根据映射到Contorller处理器的用户请求访问业务Service和数据访问DAO层处理得出返回结果到前端JSP页面
-
用户通过返回结果得到相应的信息
在开发工具中新增Java Web项目,项目结构分布说明图如下:
lib列表说明如下图:
项目核心文件列表说明如下:
(1)jdbc-context.xml spring的jdbc配置
(2)springmvc-servlet.xml sping mvc相关的配置文件
(3)web.xml
(4)LoginController.java 登录功能的控制器
(5)LoginService.java 登录功能业务接口
(6)LoginServiceImpl.java 登录功能业务实现层
(7)BaseDao.java 登录功能的数据访问接口
(8)BaseDaoImpl.java 登录功能的数据访问层
(9)DBUtil.java 数据操作公共类
(10)login.jsp 登录页面
(11)database.properties 数据库访问配置
(12)User.java 实体
源码访问如下:(1)jdbc-context.xml spring的jdbc配置
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">
<context:property-placeholder location="classpath:database.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${mysql.driverclass}"></property>
<property name="jdbcUrl" value="${mysql.jdbcurl}"></property>
<property name="user" value="${mysql.user}"></property>
<property name="password" value="${mysql.password}"></property>
<property name="acquireIncrement" value="5"></property> <!-- 当连接池中的连接用完时,C3P0一次性创建新连接的数目2 -->
<property name="initialPoolSize" value="10"></property> <!-- 初始化时创建的连接数,必须在minPoolSize和maxPoolSize之间 -->
<property name="minPoolSize" value="5"></property>
<property name="maxPoolSize" value="20"></property>
<!-- 最大空闲时间,超过空闲时间的连接将被丢弃
[需要注意:mysql默认的连接时长为8小时(28800)【可在my.ini中添加 wait_timeout=30(单位秒)设置连接超时】,这里设置c3p0的超时必须<28800]
-->
<property name="maxIdleTime" value="300"></property>
<property name="idleConnectionTestPeriod" value="60"></property> <!-- 每60秒检查连接池中的空闲连接 -->
<property name="maxStatements" value="20"></property> <!-- jdbc的标准参数 用以控制数据源内加载的PreparedStatement数量,但由于预缓存的Statement属 于单个Connection而不是整个连接 -->
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 声明式事务管理 -->
<aop:config>
<aop:advisor pointcut="execution(* com.springTest.service.impl.*ServiceImpl.*(..))" advice-ref="myAdvice"/>
</aop:config>
<tx:advice id="myAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true" rollback-for="com.springTest.util.DaoException"/>
</tx:attributes>
</tx:advice>
<!-- 自动扫描组件,需要把controller去掉,否则影响事务管理 -->
<context:component-scan base-package="com.springTest">
<context:exclude-filter type="regex" expression="com.springTest.web.*"/>
</context:component-scan>
</beans>
(2)springmvc-servlet.xml sping mvc相关的配置文件
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 启动扫描所有的controller -->
<context:component-scan base-package="com.springTest.web"/>
<!-- 主要作用于@Controller,激活该模式
下面是一种简写形式,完全可以手动配置替代这种简写形式;
它会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,
是spring MVC为@Controllers分发请求所必须的
-->
<mvc:annotation-driven/>
<!-- 这里拦截器还有一种配置方法【针对路径进行配置】 推荐使用这个,方便直观-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/user/MyHome"/>
<mvc:mapping path="/usermanager/*"/>
<bean class="com.springTest.web.interceptor.MyInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- 全局配置
<mvc:interceptors>
<bean class="com.springTest.web.interceptor.MyInterceptor"></bean>
</mvc:interceptors>
-->
<!-- 配置js,css等静态文件直接映射到对应的文件夹,不被DispatcherServlet处理 -->
<mvc:resources location="/WEB-INF/resources/**" mapping="/resources"/>
<!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在/WEB-INF/views/目录下,查找XXX.jsp文件-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
(3)web.xml 配置spring过滤器和编码
<?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>classpath:/META-INF/jdbc-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring处理器 -->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/META-INF/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
(4)LoginController.java 登录功能的控制器
/**
*
*/
package com.springTest.web.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.springTest.po.User;
import com.springTest.service.LoginService;
import com.springTest.web.BaseController;
/**
* 登陆方法
* @author tushen
* @date Nov 4, 2011
*/
@Controller
public class LoginController extends BaseController{
@Autowired
private LoginService loginService;
@RequestMapping(value="/user/login",method=RequestMethod.GET)
public String login(){
return "login";
}
@RequestMapping(value="/user/login",method=RequestMethod.POST)
public String logon(String userName,String passWord){
User user = null;
try {
user = loginService.getUser(userName, passWord);
} catch (RuntimeException e) {
}
if(user!=null){
return "MyHome";
}else{
return "login";
}
}
@RequestMapping(value="/user/register",method=RequestMethod.POST)
public String register(User user){
loginService.addUser(user);
return "login";
}
@RequestMapping(value="/user/toRegister",method=RequestMethod.GET)
public String toRegister(){
return "register";
}
}
(5)LoginService.java 登录功能业务接口
/**
*
*/
package com.springTest.service;
import com.springTest.po.User;
/**
* @author tushen
* @date Nov 4, 2011
*/
public interface LoginService {
public User getUser(String userName,String password);
public void addUser(User user);
}
(6)LoginServiceImpl.java 登录功能业务实现层
/**
*
*/
package com.springTest.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springTest.dao.BaseDao;
import com.springTest.po.User;
import com.springTest.service.LoginService;
/**
* @author tushen
* @date Nov 4, 2011
*/
@Service
public class LoginServiceImpl implements LoginService {
@Autowired
private BaseDao baseDao;
static String addUser = "insert into user(username,password,first_name,last_name,birthday,age) values(:userName,:passWord,:firstName,:lastName,:birthday,:age);";
static String getUser = "select * from user where username = ? and password = ?";
@Override
public User getUser(String userName, String password) {
User user = baseDao.getObject(getUser, User.class, new Object[]{userName,password});
return user;
}
@Override
public void addUser(User user) {
baseDao.saveOrUpdateObject(addUser, user);
}
}
(7)BaseDao.java 登录功能的数据访问接口
/**
*
*/
package com.springTest.dao;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
/**
* @author tushen
* @date Nov 5, 2011
*/
public interface BaseDao {
/**
* 保存或者更新实体
* @param sql
* @param entry
*/
<T extends Serializable> void saveOrUpdateObject(String sql,T entry);
/**
* 查询实体列表
* @param sql
* @param className
* @param obj
* @return
*/
<T extends Serializable> List<T> getObjList(String sql,Class<T> className,Object[] objs);
/**
* 查询实体
* @param <T>
* @param sql
* @param objs
* @return
*/
<T extends Serializable> T getObject(String sql,Class<T> clazz,Object[] objs);
/**
* 查询一个Map集合
* @param sql
* @param objs
* @return
*/
Map<String,?> find(String sql,Object[] objs);
/**
* 批量操作
* @param sql
* @param objLs
*/
void batchOperate(String sql,List<?> objLs);
/**
* 判断实体是否存在
* @param sql
* @param obj
* @return
*/
int isExist(String sql,Object[] obj);
/**
* 编辑操作
* @param sql
* @param obj
* @return
*/
int editObject(String sql,Object[] obj);
}
(8)BaseDaoImpl.java 登录功能的数据访问层
/**
*
*/
package com.springTest.dao.impl;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.springTest.dao.BaseDao;
import com.springTest.util.DBUtil;
/**
* @author tushen
* @date Nov 5, 2011
*/
@Repository
public class BaseDaoImpl implements BaseDao {
@Autowired
private DBUtil util;
@Override
public void batchOperate(String sql, List<?> objs) {
util.batchOperate(sql, objs);
}
@Override
public int editObject(String sql, Object[] obj) {
return util.editObject(sql, obj);
}
@Override
public Map<String, ?> find(String sql, Object[] objs) {
return util.getMap(sql, objs);
}
@Override
public <T extends Serializable> List<T> getObjList(String sql,
Class<T> className, Object[] objs) {
return (List<T>)util.getObjList(sql, className, objs);
}
@Override
public int isExist(String sql, Object[] obj) {
return util.isExist(sql, obj);
}
@Override
public <T extends Serializable> void saveOrUpdateObject(String sql, T entry) {
util.addOrUpdate(sql, entry);
}
@Override
public <T extends Serializable> T getObject(String sql,Class<T> clazz,Object[] objs) {
return (T)util.getObject(sql, clazz, objs);
}
}
(9)DBUtil.java 数据操作公共类
/**
*
*/
package com.springTest.util;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Repository;
/**
* @author tushen
* @date Nov 5, 2011
*/
@Repository(value="util")
public class DBUtil{
private Log log = LogFactory.getLog(DBUtil.class);
private SimpleJdbcTemplate simpleJdbcTemplate;
/**
* 添加或者更新数据
* @param sql
* @param obj
* @return
*/
public Integer addOrUpdate(String sql,Object obj){
Integer id = 0;
try {
id = simpleJdbcTemplate.update(sql, new BeanPropertySqlParameterSource(obj));
} catch (Exception e) {
log.info(e);
throw new DaoException("数据库操作失败!",e);
}
return id;
}
/**
* 获取List集合
* @param sql
* @param className
* @param obj
* @return
*/
@SuppressWarnings("unchecked")
public List<?> getObjList(String sql,Class<?> className,Object[] obj){
List<?> array = null;
try {
array = simpleJdbcTemplate.queryForList(sql,
ParameterizedBeanPropertyRowMapper.newInstance(className),
obj);
} catch (Exception e) {
log.info(e);
throw new DaoException("数据库操作失败!",e);
}
return array;
}
/**
* 获取Map集合值
* @param sql
* @param obj
* @return
*/
public Map<String,?> getMap(String sql,Object[] obj){
Map<String, ?> map = null;
try {
map = simpleJdbcTemplate.queryForMap(sql, obj);
} catch (Exception e) {
log.info(e);
throw new DaoException("数据库操作失败!",e);
}
return map;
}
/**
* 获取相应的Object
* @param sql
* @param className
* @param obj
* @return
*/
public Object getObject(String sql,Class<? extends Serializable> className,Object[] obj){
Object object = null;
try {
object = simpleJdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(className), obj);
} catch (DataAccessException e) {
log.info(e);
throw new DaoException("数据库操作失败!",e);
}
return object;
}
/**
* 批量操作
* @param sql
* @param obj
* @return
*/
public int[] batchOperate(String sql,List<?> obj){
int[] a = null;
try {
a = simpleJdbcTemplate.batchUpdate(sql,
SqlParameterSourceUtils.createBatch(obj.toArray()));
} catch (Exception e) {
log.info(e);
throw new DaoException("数据库操作失败!",e);
}
return a;
}
/**
* 检查是否有值
* @param sql
* @param obj
* @return
*/
public int isExist(String sql,Object[] obj){
int index = 0;
try {
index = simpleJdbcTemplate.queryForInt(sql, obj);
} catch (Exception e) {
log.info(e);
throw new DaoException("数据库操作失败!",e);
}
return index;
}
/**
* 编辑操作(增删改查都可以)
* @param sql
* @param obj
* @return
*/
public int editObject(String sql,Object[] obj){
int index = 0;
try {
index = simpleJdbcTemplate.update(sql, obj);
} catch (DataAccessException e) {
log.info(e);
throw new DaoException("数据库操作失败!",e);
}
return index;
}
/**
* @param simpleJdbcTemplate the simpleJdbcTemplate to set
*/
@Resource(name="dataSource")
public void setSimpleJdbcTemplate(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
}
(10)login.jsp 登录页面
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登陆</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>
<form action="${ctx}/user/login" method="post">
userName:<input type="text" name="userName"><br/>
passWord:<input type="text" name="passWord"><br/>
<input type="submit" value="登陆">
</form>
</body>
</html>
(11)database.properties 数据库访问配置
mysql.driverclass=com.mysql.jdbc.Driver
mysql.jdbcurl=jdbc:mysql://127.0.0.1:3306/test
mysql.user=root
mysql.password=admin
(12)User.java 实体
/**
*
*/
package com.springTest.po;
import java.io.Serializable;
/**
* @author tushen
* @date Nov 4, 2011
*/
public class User implements Serializable{
private Integer id;
private String userName; //用户名
private String passWord; //密码
private String firstName;
private String lastName;
private String birthday; //生日
private Integer age;
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the age
*/
public Integer getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(Integer age) {
this.age = age;
}
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the passWord
*/
public String getPassWord() {
return passWord;
}
/**
* @param passWord the passWord to set
*/
public void setPassWord(String passWord) {
this.passWord = passWord;
}
/**
* @return the birthday
*/
public String getBirthday() {
return birthday;
}
/**
* @param birthday the birthday to set
*/
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
附录数据库脚步如下:
/*
SQLyog v10.2
MySQL - 5.0.87-community-nt : Database - test
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`test` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `test`;
/*Table structure for table `user` */
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`username` varchar(64) default NULL,
`password` varchar(32) default NULL,
`first_name` varchar(32) default NULL,
`last_name` varchar(32) default NULL,
`birthday` date default NULL,
`age` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `user` */
LOCK TABLES `user` WRITE;
insert into `user`(`username`,`password`,`first_name`,`last_name`,`birthday`,`age`)
values ('admin','123456',NULL,NULL,NULL,NULL),('liaoxj','123456','liao','xiongjian','1988-06-21',25),('changjiang','123456','zhang','liang','2013-01-01',25);
UNLOCK TABLES;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;