SpringMVC+Spring+HIbernate 簡單增刪改查例項
阿新 • • 發佈:2018-11-09
SpringMVC+Spring+HIbernate 簡單增刪改查例項
HIbernate配置mysql資料庫的方式 和 Structs+spring+HIbernate 是一樣的。
可以理解為SpringMVC 把 Structs2 替代了,SpringMVC 中的 Controller 對應 Structs2 中的 Action,相對來說更加的簡單,畢竟少了一個配置檔案struts.xml,這個例項中連xx.hbm.xml都用註解的方式代替了
對應頁面回顯資料,Struts2中,用值棧、session、Request等,SpringMVC中也可以使用Servlet API,session等都可以用,可以用 Map、Model等來回顯資料
頁面上可以使用JSTL,Struts的 s標籤就沒了
<s:iterator value="list" var="d">
- 1
<c:forEach var="p" items="${requestScope.personlist }">
- 1
刪除和修改都是url中傳入id的引數
http://localhost:8080/../doupdate?id=402881e958de29980158de2aa5440000
- 1
專案結構
除了spring和HIbernate基本包還需要加 jstl標籤庫的jar包
jstl.jar standard.jar
HIbernate c3p0 jar包,
c3p0-0.9.2.1.jar hibernate-c3p0-4.3.11.Final.jar mchange-commons-java-0.2.3.4.jar 3個
對應在hibernate-release-4.3.11.Final\lib\optional\c3p0
目錄下mysql資料庫連線的jar包
mysql-connector-java-5.1.40-bin.jar
jar包在原始碼裡有
沒有使用各層的介面
版本資訊
-
Eclipse版本
Neon
.1
a
Release (
4.6
.1)
-
Spring
4.3
.4
-
Hibernate
4.3
.11
-
Tomcat
7.0
-
JDK
1.8
-
資料庫
MySQL5
.7
.12
- 1
- 2
- 3
- 4
- 5
- 6
部分原始碼
業務層和Dao層沒有使用介面(解耦等)
web.xml
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
-
<display-name>SpringMVC_Spring_Hibernate
</display-name>
-
-
-
<!-- 防止中文引數亂碼 放在前面 -->
-
<filter>
-
<filter-name>SetCharacterEncoding
</filter-name>
-
<filter-class>org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
-
<init-param>
-
<param-name>encoding
</param-name>
-
<param-value>UTF-8
</param-value>
-
</init-param>
-
<init-param>
-
<param-name>forceEncoding
</param-name>
-
<param-value>true
</param-value>
-
<!-- 強制進行轉碼 -->
-
</init-param>
-
</filter>
-
<filter-mapping>
-
<filter-name>SetCharacterEncoding
</filter-name>
-
<url-pattern>/*
</url-pattern>
-
</filter-mapping>
-
-
<!-- spring 配置Listener-->
-
-
<!-- needed for ContextLoaderListener -->
-
<context-param>
-
<param-name>contextConfigLocation
</param-name>
-
<param-value>classpath:/resources/beans.xml
</param-value>
-
</context-param>
-
-
<!-- Bootstraps the root web application context before servlet initialization -->
-
<listener>
-
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
-
</listener>
-
-
<!-- springmvc配置dispatcherServlet -->
-
-
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
-
<servlet>
-
<servlet-name>springDispatcherServlet
</servlet-name>
-
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
-
<init-param>
-
<param-name>contextConfigLocation
</param-name>
-
<param-value>classpath:/resources/springmvc.xml
</param-value>
-
</init-param>
-
<load-on-startup>1
</load-on-startup>
-
</servlet>
-
-
<!-- Map all requests to the DispatcherServlet for handling -->
-
<servlet-mapping>
-
<servlet-name>springDispatcherServlet
</servlet-name>
-
<url-pattern>/
</url-pattern>
-
</servlet-mapping>
-
</web-app>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
實體類
Person.java
-
package com.jxust.svsh.entity;
-
-
import javax.persistence.Column;
-
import javax.persistence.Entity;
-
import javax.persistence.GeneratedValue;
-
import javax.persistence.Id;
-
import javax.persistence.Table;
-
-
import org.hibernate.annotations.GenericGenerator;
-
-
/**
-
* 個人資訊實體類
-
* 註解方式配置對應資料表
-
*
@author
Peng
-
*
@Date
2016年12月8日下午2:54:52
-
*/
-
@Entity
-
@Table(name =
"person")
-
public
class Person {
-
-
private String id;
// 主鍵id
-
private String name;
// 姓名
-
private String idCard;
// 身份證號
-
private String phone;
// 手機號
-
private String address;
// 地址
-
-
public
Person
() {
-
super();
-
}
-
-
public
Person
(String name, String idCard, String phone, String address) {
-
super();
-
this.name = name;
-
this.idCard = idCard;
-
this.phone = phone;
-
this.address = address;
-
}
-
-
@Id
-
@Column(name =
"id", nullable =
false, unique =
true)
-
@GenericGenerator(name =
"generator", strategy =
"uuid")
-
@GeneratedValue(generator =
"generator")
-
public
String
getId
() {
-
return id;
-
}
-
-
public
void
setId
(String id) {
-
this.id = id;
-
}
-
-
@Column(name =
"name", nullable =
false, length =
32)
-
public
String
getName
() {
-
return name;
-
}
-
-
public
void
setName
(String name) {
-
this.name = name;
-
}
-
-
@Column(name =
"idCard", nullable =
false, length =
32)
-
public
String
getIdCard
() {
-
return idCard;
-
}
-
-
public
void
setIdCard
(String idCard) {
-
this.idCard = idCard;
-
}
-
-
@Column(name =
"phone", nullable =
false, length =
32)
-
public
String
getPhone
() {
-
return phone;
-
}
-
-
public
void
setPhone
(String phone) {
-
this.phone = phone;
-
}
-
-
@Column(name =
"address", nullable =
false, length =
32)
-
public
String
getAddress
() {
-