03SpringMVC,Spring,Hibernate整合(Date时间转换)
项目结构
2 web.xml的配置内容如下:
|
<?xmlversion="1.0"encoding="UTF-8"?> <web-appversion="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"> <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> <!-- struts用/*,springmvc不能/*,语法 <url-pattern>*.do</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> </web-app> |
springmvc.xml的配置内容如下:
|
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <context:component-scanbase-package="com.rl.controller"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <propertyname="prefix"value="/WEB-INF/jsp/"></property> <propertyname="suffix"value=".jsp"></property> </bean> </beans> |
log4j.properties的内容如下:
|
log4j.rootLogger=DEBUG,Console #Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d[%t]%-5p[%c]-%m%n log4j.logger.java.sql.ResultSet=INFO log4j.logger.org.apache=INFO log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG |
beans.xml的配置内容如下:
|
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:component-scanbase-package="com.rl"/> <beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <propertyname="driverClassName"value="com.mysql.jdbc.Driver"></property> <propertyname="url"value="jdbc:mysql://localhost:3306/springmvc"></property> <propertyname="username"value="root"></property> <propertyname="password"value="123456"></property> </bean> <beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <propertyname="dataSource"ref="dataSource"></property> <propertyname="hibernateProperties"> <props> <propkey="hibernate.Dialect">org.hibernate.dialect.MySQL5Dialect</prop> <propkey="hibernate.show_sql">true</prop> <propkey="hibernate.hbm2ddl">update</prop> </props> </property> <propertyname="mappingDirectoryLocations"value="classpath:com/rl/mapping/"></property> </bean> <beanid="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <propertyname="sessionFactory"ref="sessionFactory"></property> </bean> <tx:adviceid="txAdvice"transaction-manager="txManager"> <tx:attributes> <tx:methodname="save*"propagation="REQUIRED"/> <tx:methodname="get*"read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:advisoradvice-ref="txAdvice"pointcut="execution(* </aop:config> </beans> |
Person0420的代码如下:
|
package com.rl.model; import java.util.Date; @SuppressWarnings("serial") publicclass private private private private private /** * */ public } /** *@param *@param *@param *@param *@param */ public String address, Date birthday) { this.personId this.name this.gender this.address this.birthday } /** *@return */ public returnpersonId; } /** *@param */ publicvoid this.personId } /** *@return */ public returnname; } /** *@param */ publicvoid this.name } /** *@return */ public returngender; } /** *@param */ publicvoid this.gender } /** *@return */ public returnaddress; } /** *@param */ publicvoid this.address } /** *@return */ public returnbirthday; } /** *@param */ publicvoid this.birthday } } |
Person0420.hbm.xml的内容如下:
|
<?xmlversion="1.0"encoding="utf-8"?> <!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/Hibernate "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping fileautogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <classname="com.rl.model.Person0420"table="person_0420"catalog="springmvc"> <idname="personId"type="java.lang.Integer"> <columnname="PERSON_ID"/> <generatorclass="identity"/> </id> <propertyname="name"type="java.lang.String"> <columnname="NAME"length="10"/> </property> <propertyname="gender"type="java.lang.String"> <columnname="GENDER"length="1"/> </property> <propertyname="address"type="java.lang.String"> <columnname="ADDRESS"length="50"/> </property> <propertyname="birthday"type="java.util.Date"> <columnname="BIRTHDAY"length="0"/> </property> </class> </hibernate-mapping> |
创建数据库和表所需的SQL语句:
|
DROP DATABASE springmvc; CREATE DATABASE springmvc DEFAULT CHARSET utf8; USE springmvc; CREATE TABLE person_0420( PERSON_ID INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(10) NOT NULL, GENDER VARCHAR(1) NOT NULL, ADDRESS VARCHAR(50) NOT NULL, birthday DATE ); |
PersonDao的代码如下:
|
package com.rl.dao; import com.rl.model.Person0420; publicinterfacePersonDao publicvoid } |
PersonDaoImpl 的内容如下:
|
package com.rl.dao.impl; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; import com.rl.dao.PersonDao; import com.rl.model.Person0420; @Repository public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao { @Autowired public void setMySessionFactory(SessionFactory sessionFactory){ super.setSessionFactory(sessionFactory); } public void save(Person0420 person) { this.getHibernateTemplate().save(person); } } |
PersonService的内容如下:
|
package com.rl.service; import com.rl.model.Person0420; publicinterface publicvoid } |
PersonServiceImpl的内容如下:
|
package com.rl.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.rl.dao.PersonDao; import com.rl.model.Person0420; import com.rl.service.PersonService; @Service public class PersonServiceImpl implements PersonService { @Autowired PersonDao personDao; public void save(Person0420 person) { personDao.save(person); } } |
PersonController的内容如下:
|
package com.rl.controller; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; import com.rl.model.Person0420; import com.rl.service.PersonService; @Controller @RequestMapping("/person") public class PersonController { @Autowired PersonService personService; @RequestMapping("/toForm.do") public String toForm() { return "form"; } @RequestMapping("/save.do") public String save(Person0420 person) { personService.save(person); return "success"; } @InitBinder public void initBinder(ServletRequestDataBinder binder) { binder.registerCustomEditor(Date.class, new CustomDateEditor( new SimpleDateFormat("yyyy-MM-dd"), true)); } } |
14 form.jsp的内容如下:
|
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPEHTMLPUBLIC <html> <head> <basehref="<%=basePath%>"> <title>My <metahttp-equiv="pragma"content="no-cache"> <metahttp-equiv="cache-control"content="no-cache"> <metahttp-equiv="expires"content="0"> <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> <metahttp-equiv="description"content="This <!-- <linkrel="stylesheet" type="text/css" --> </head> <body> <formaction="person/save.do"method="post"> name:<inputname="name"type="text"><br> gender:<inputname="gender"type="text"><br> address:<inputname="address"type="text"><br> birthday:<inputname="birthday"type="text"><br> <inputvalue="submit"type="submit"> </form> </body> </html> |
success.jsp的内容如下:
|
<%@pagelanguage="java"import="java.util.*"pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPEHTMLPUBLIC <html> <head> <basehref="<%=basePath%>"> <title>My <metahttp-equiv="pragma"content="no-cache"> <metahttp-equiv="cache-control"content="no-cache"> <metahttp-equiv="expires"content="0"> <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> <metahttp-equiv="description"content="This <!-- <linkrel="stylesheet" type="text/css" --> </head> <body> success<br> </body> </html> |
浏览器中的访问地址是:http://localhost:8080/ssh/person/toForm.do
03SpringMVC,Spring,Hibernate整合(Date时间转换)的更多相关文章
- SpringMVC+Spring+Hibernate整合开发
最近突然想认真研究下java web常用框架,虽然现在一直在用,但实现的整体流程不是很了解,就在网上搜索资料,尝试自己搭建,以下是自己的搭建及测试过程. 一.准备工作: 1/安装并配置java运行环境 ...
- Spring+Hibernate整合配置 --- 比较完整的spring、hibernate 配置
Spring+Hibernate整合配置 分类: J2EE2010-11-25 17:21 16667人阅读 评论(1) 收藏 举报 springhibernateclassactionservlet ...
- spring+hibernate整合:报错org.hibernate.HibernateException: No Session found for current thread
spring+hibernate整合:报错信息如下 org.hibernate.HibernateException: No Session found for current thread at o ...
- Struts2+Spring+Hibernate整合开发(Maven多模块搭建)
Struts2+Spring+Hibernate整合开发(Maven多模块搭建) 0.项目结构 Struts2:web层 Spring:对象的容器 Hibernate:数据库持久化操作 1.父模块导入 ...
- 笔记58 Spring+Hibernate整合(一)
Spring+Hibernate整合 一.整合思路 使DAO继承HibernateTemplate这个类 HibernateTemplate这个类提供了setSessionFactory()方法用于注 ...
- MyEclipse下Spring+Hibernate整合
目前,SSH(Struts+Spring+Hibernate)是Web开发的一种常用框架组合,Struts实现了MVC,Hibernate实现了关系对象映射,Spring实现了基于Bean的配置管理. ...
- Struts+Spring+Hibernate整合入门详解
Java 5.0 Struts 2.0.9 Spring 2.0.6 Hibernate 3.2.4 作者: Liu Liu 转载请注明出处 基本概念和典型实用例子. 一.基本概念 St ...
- Spring、Struts2+Spring+Hibernate整合步骤
所使用的Jar包: Hibernate: Spring(使用MyEclipse自动导入框架功能) Struts2: 注解包和MySql驱动包: 1.配置Hibernate和Spring: <be ...
- Struts+Spring+Hibernate整合
这段笔记三两年前写的,一直因为一些琐事,或者搞忘记了,没有发.今天偶然翻出了它,就和大家一起分享下吧. 1.导入jar包 Struts的jar包: Spring的jar包: Hibernate的jar ...
随机推荐
- POJ 3294 n个串中至少一半的串共享的最长公共子串
Life Forms Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 12484 Accepted: 3502 Descr ...
- 关于 Cannot forward after response has been committed的错误
这个错误顾名思义:就是在给客户端做出提交响应完毕后,就不能再次使用request的转发. 代码原先是这样的: package com.bdqn.jsp.study.web.filter; import ...
- Mysql--执行计划 Explain
0 介绍 0.1 是什么 使用 Explain 关键字可以模拟优化器执行 Sql 查询语句,从而知道 Mysql 是如何处理 Sql 的. 0.2 用法 Explain + Sql语句 0.3 执行计 ...
- Jenkins简明入门(一) -- 安装
如今Jenkins官网的Guide里使用了Docker,网上很多Jenkins入门教程都已过时了,所以写这一篇入门教程. 官网的Guide Link是:https://jenkins.io/doc/p ...
- PostgreSQL 查看单表大小
1. 在数据库中查单个表 select pg_size_pretty(pg_relation_size('table_name')); 2. 查出并按大小排序 SELECT table_schema ...
- IntelliJ IDEA 14.0.3 实战搭建Spring+SpringMVC+MyBatis组合框架
简介 Spring+SpringMVC+MyBatis框架(SSM)是比较热门的中小型企业级项目开发的框架,对于新手来说也是比较容易学习入门的.虽说容易,但在框架搭建过程中仍然遇到了许多问题,因此用实 ...
- WUOJ-ACM :1003: 零起点学算法78——牛牛
武汉科技大学ACM :1003: 零起点学算法78--牛牛Problem Description牛牛是一种纸牌游戏,总共5张牌,规则如下: 如果找不到3张牌的点数之和是10的倍数,则为没牛: 如果其中 ...
- c++ 深入理解数组
阅读前提:你得知道啥是数组. 本文需要弄清楚的问题如下: 1,数组作为函数参数,传入的是值,还是地址? 2,数组作为函数参数,数组的长度能否确定? 解决如下 1,数组作为函数参数,传入的是地址.因为数 ...
- python学习之路基础篇(第六篇)
一.算法 冒泡排序 两两比较 打的沉下去,小的浮上来 从而把数字从小到大排列出来 选择排序 随机取一个索引作为最大值,然后和列表中的其他索引进行比较,如果l[0]<l[1],则将l[1]修改为 ...
- C语言多维数组的指针传递
在C语言中为了节省空间,提高运行速度经常使用指针来完成数组的传递. 对于一维数组而言可以直接传递首地址 而对于二维数组必须在传递时声明是二维数组的指针,并且调用时也要经过一些运算 首先是定义形参: 函 ...