javascript
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不能/*,語法 *.xxx --> ???????<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(* com.rl.service..*.*(..))"/> ???</aop:config> </beans> |
Person0420的代碼如下:
| package com.rl.model; ? import java.util.Date; ? @SuppressWarnings("serial") publicclass Person0420implements java.io.Serializable { ? ???private IntegerpersonId; ???private Stringname; ???private Stringgender; ???private Stringaddress; ???private Datebirthday; ??? ???/** ????* ????*/ ???public Person0420() { ???} ??? ???/** ????*@param personId ????*@param name ????*@param gender ????*@param address ????*@param birthday ????*/ ???public Person0420(Integer personId, String name, String gender, ???????????String address, Date birthday) { ???????this.personId = personId; ???????this.name = name; ???????this.gender = gender; ???????this.address = address; ???????this.birthday = birthday; ???} ??? ???/** ????*@return the personId ????*/ ???public Integer getPersonId() { ???????returnpersonId; ???} ? ???/** ????*@param personId the personId to set ????*/ ???publicvoid setPersonId(Integer personId) { ???????this.personId = personId; ???} ??? ???/** ????*@return the name ????*/ ???public String getName() { ??????returnname; ???} ??? ???/** ????*@param name the name to set ????*/ ???publicvoid setName(String name) { ???????this.name = name; ???} ??? ???/** ????*@return the gender ????*/ ???public String getGender() { ???????returngender; ???} ? ???/** ????*@param gender the gender to set ????*/ ???publicvoid setGender(String gender) { ???????this.gender = gender; ???} ??? ???/** ????*@return the address ????*/ ???public String getAddress() { ???????returnaddress; ???} ??? ???/** ????*@param address the address to set ????*/ ??? ???publicvoid setAddress(String address) { ???????this.address = address; ???} ??? ???/** ????*@return the birthday ????*/ ???public Date getBirthday() { ???????returnbirthday; ???} ??? ???/** ????*@param birthday the birthday to set ????*/ ???publicvoid setBirthday(Date birthday) { ???????this.birthday = birthday; ???} } |
Person0420.hbm.xml的內容如下:
| <?xmlversion="1.0"encoding="utf-8"?> <!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "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 save(Person0420 person); } |
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 PersonService { ??? ???publicvoid save(Person0420 person); } |
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 "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> ?<head> ???<basehref="<%=basePath%>"> ??? ???<title>My JSP 'index.jsp' starting page</title> ???<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 is my page"> ???<!-- ???<linkrel="stylesheet" type="text/css" href="styles.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 "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> ?<head> ???<basehref="<%=basePath%>"> ??? ???<title>My JSP 'index.jsp' starting page</title> ???<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 is my page"> ???<!-- ???<linkrel="stylesheet" type="text/css" href="styles.css"> ???--> ?</head> ? ?<body> ???success<br> ?</body> </html> |
瀏覽器中的訪問地址是:http://localhost:8080/ssh/person/toForm.do
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的03SpringMVC,Spring,Hibernate整合(Date时间转换)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华夏银行是哪里的银行
- 下一篇: 1.Maven+SpringMVC+Ec