SpringMVC框架簡介②
SSH整合(SpringMVC/Spring/Hibernate三大框架的整合):
引包: 連結:https://pan.baidu.com/s/1i5Y6DZeNm5pVJHrm2R7LbA 密碼:uue3
dao實現類:
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.Person;
@Repository
public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {
@Autowired
public void setMySessionFactory(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}
@Override
public void savePerson(Person p) {
this.getHibernateTemplate().save(p);
}
}
service實現類:
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.Person;
import com.rl.service.PersonService;
@Service
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonDao personDao;
@Override
public void savePerson(Person p) {
personDao.savePerson(p);
}
}
實體類Person:
package com.rl.model;
// Generated 2018-8-12 21:26:01 by Hibernate Tools 3.6.0.Final
import java.util.Date;
/**
* Person generated by hbm2java
*/
public class Person implements java.io.Serializable {
private Integer personId;
private String name;
private Integer gender;
private String address;
private Date birthday;
public Person() {
}
public Person(String name, Integer gender, String address, Date birthday) {
this.name = name;
this.gender = gender;
this.address = address;
this.birthday = birthday;
}
public Integer getPersonId() {
return this.personId;
}
public void setPersonId(Integer personId) {
this.personId = personId;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Integer getGender() {
return this.gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getBirthday() {
return this.birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
控制類Controller:
package com.rl.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.rl.model.Person;
import com.rl.service.PersonService;
@Controller
@RequestMapping("/person")
public class PersonController {
@Autowired
private PersonService personService;
@RequestMapping("/save.do")
public String save(Person person){
personService.savePerson(person);
return "success";
}
@RequestMapping("/toForm.do")
public String toForm(){
return "form";
}
}
對映檔案:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-8-12 21:26:01 by Hibernate Tools 3.6.0.Final -->
<hibernate-mapping>
<class name="com.rl.model.Person" table="person" catalog="springmvc">
<id name="personId" type="java.lang.Integer">
<column name="PERSON_ID" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="NAME" length="10" />
</property>
<property name="gender" type="java.lang.Integer">
<column name="GENDER" />
</property>
<property name="address" type="string">
<column name="ADDRESS" length="50" />
</property>
<property name="birthday" type="date">
<column name="BIRTHDAY" length="0" />
</property>
</class>
</hibernate-mapping>
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: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-scan base-package="com.rl"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/springmvc"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingDirectoryLocations" value="classpath:com/rl/mapping"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.Dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl">update</prop>
</props>
</property>
</bean>
<!-- <bean class="com.rl.dao.impl.PersonDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="select*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.rl.service..*.*(..))"/>
</aop:config>
</beans>
SpringMVC配置檔案:
<?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.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 ">
<!--
註解形式開發springmvc要配置的元件:
ViewResovler, interceptor
-->
<context:component-scan base-package="com.rl.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--
檢視解析器字首:從根目錄開始到一個公共的目錄
-->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!--
檢視的字尾
-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml配置檔案:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<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>
<!--
區分於struts2/*, 要用*.xxx,按規範*.do
-->
<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>
表單頁面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</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">
</head>
<body>
<form action="person/save.do" method="post">
姓名:<input type="text" name="name"><br>
地址:<input type="text" name="address"><br>
性別:<input type="text" name="gender"><br>
生日:<input type="text" name="birthday"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
資料庫結果:
相關文章
- SpringMVC框架簡介①SpringMVC框架
- SpringMVC簡介SpringMVC
- springmvc簡介和快速搭建SpringMVC
- 【SpringMVC】RESTFul簡介以及案例實現SpringMVCREST
- 【SpringMVC】SpringMVC搭建框架SpringMVC框架
- HTML 框架簡介HTML框架
- Scrapy框架簡介框架
- Hibernate框架簡介⑤框架
- Hibernate框架簡介④框架
- Hibernate框架簡介③框架
- Hibernate框架簡介②框架
- Hibernate框架簡介①框架
- Spring框架簡介⑩Spring框架
- Spring框架簡介⑨Spring框架
- Spring框架簡介⑧Spring框架
- Spring框架簡介⑦Spring框架
- Spring框架簡介⑥Spring框架
- Spring框架簡介⑤Spring框架
- Spring框架簡介④Spring框架
- Spring框架簡介③Spring框架
- Spring框架簡介②Spring框架
- Spring框架簡介①Spring框架
- 自己手寫一個SpringMVC框架(簡化)SpringMVC框架
- [轉]SSH框架簡介框架
- uni-app 框架簡介APP框架
- Flutter路由框架Fluro簡介Flutter路由框架
- wsgiref模組、web框架、django框架簡介Web框架Django
- 簡單介紹SpringMVC RESTFul實現列表功能SpringMVCREST
- SpringMVC框架入門SpringMVC框架
- 工作流框架Flowable 簡介框架
- OSX 攻擊框架Empyre簡介框架
- 大資料框架原理簡介大資料框架
- 前端框架 Quasar-Framework 簡介前端框架Framework
- SpringMVC 簡單SpringMVC
- SpringMVC框架搭建環境SpringMVC框架
- 簡單介紹SpringSecurity框架簡介及與shiro特點對比SpringGse框架
- ToplingDB 的序列化框架:簡介框架
- Android Media Framework(一)OpenMAX 框架簡介AndroidFramework框架