在eclipse上邊搭建一個最最最最最最最最最最最最最最最最最最最最最最最最簡單的ssh框架spring4+struts2+hibernate5,然後實現從資料庫中查詢一個數據,在前端頁面中顯示出來
在eclipse上邊搭建一個最最最簡單的ssh框架spring4+struts2+hibernate5,然後實現從資料庫中查詢一個數據,在前端頁面中顯示出來
需要的包和工具連結如下:
jar包:https://download.csdn.net/download/handsome2013/10911122
tomcat:https://download.csdn.net/download/handsome2013/10430635
mysql+eclipse+navicate等:https://download.csdn.net/download/handsome2013/10747713
一:資料庫
我這裡使用了mysql5.7,以及Navicate軟體工具
資料庫和資料表設計如下:
新增資料如下:
二,ssh三層的介紹:直接百度百科如下
三.搭建ssh環境,
spring4+struts2+hibernate5
然後是導包:包括了spring4+struts2+hibernate5各自的包以及整合包。把包放入到WebContent/WEB-INF/lib下所有的包到https://download.csdn.net/download/handsome2013/10911122下載
然後建立包名和配置檔名(根據不同的功能,分不同的包,同時三個配置檔案對應三層的一個配置applicationContext.xml對應的是spring, hibernate.cfg.xml對應的是hibernate層。 struts.xml對應的就是struts2的配置)
四:建立 資料表對應的一個實體類,以及對映配置
首先的Students.java如下:
package cqupt.ssh.entity; public class Students { //和資料庫對應的實體類 Integer s_id; String s_name; Integer s_age; //get和set一波 public Integer getS_id() { return s_id; } public void setS_id(Integer s_id) { this.s_id = s_id; } public String getS_name() { return s_name; } public void setS_name(String s_name) { this.s_name = s_name; } public Integer getS_age() { return s_age; } public void setS_age(Integer s_age) { this.s_age = s_age; } @Override public String toString() { return "Students [s_id=" + s_id + ", s_name=" + s_name + ", s_age=" + s_age + "]"; } }
然後是實體類和資料庫的對映配置Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cqupt.ssh.entity" >
<class name="Students" table="students" ><!-- 類和表的對應 -->
<id name="s_id" ><!-- 主鍵 -->
<generator class="assigned"></generator><!-- 這裡是主鍵生成策略 -->
</id>
<!-- 下邊是除主鍵外的其他屬性,下邊是由於我資料表和實體類的屬性名字一樣,所以寫的少點,要是資料庫和實體的屬性寫的不一樣,比如,name和s_name,就需要做一些其他處理 -->
<property name="s_name" ></property>
<property name="s_age" ></property>
</class>
</hibernate-mapping>
五:然後是開始dao層
在dao層中需要一個工具類,來獲取session,簡便了dao中的程式碼
工具類如下:
工具類程式碼:
package cqupt.ssh.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static SessionFactory sf;
// static程式碼塊裡面是用類載入器來讀取,類載入器裡面有個緩衝區,會把讀取到的內容快取起來
static {// 這個static塊裡面的兩條語句,一個web專案只執行一次,不會因為多次呼叫程式據多次執行
// 1 建立,呼叫空參構造
Configuration conf = new Configuration().configure();
// 2 根據配置資訊,建立 SessionFactory物件
sf = conf.buildSessionFactory();
}
// 獲得session => 獲得全新session
public static Session openSession() {
// 3 獲得session
Session session = sf.openSession();
return session;
}
// 獲得session => 獲得與執行緒繫結的session
public static Session getCurrentSession() {
// 3 獲得session
Session session = sf.getCurrentSession();
return session;
}
public static void main(String[] args) {
System.out.println(HibernateUtils.openSession());
}
}
首先是介面SSHDao.java
package cqupt.ssh.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
import cqupt.ssh.entity.Students;
public interface SSHDao {
List<Students> getStudents();
}
然後是實現類
package cqupt.ssh.daoImpl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import cqupt.ssh.dao.SSHDao;
import cqupt.ssh.entity.Students;
import cqupt.ssh.utils.HibernateUtils;
public class SSHDaoImpl implements SSHDao {
@Override
public List<Students> getStudents() {
// 1獲得session
Session session = HibernateUtils.openSession();
// 2控制事務
Transaction tx = session.beginTransaction();
// 3.執行操作
// 書寫hql
String hql = "from Students";
Query query = session.createQuery(hql);
List<Students> list = query.list();
// System.out.println(list);
// 4.提交事務,關閉資源
tx.commit();
session.close();
return list;
}
}
六:開始Service層
首先是介面:
package cqupt.ssh.service;
import java.util.List;
import org.springframework.stereotype.Service;
import cqupt.ssh.entity.Students;
public interface SSHService {
public List<Students> getStudents();
}
然後是實現類
package cqupt.ssh.serviceImpl;
import java.util.List;
import cqupt.ssh.dao.SSHDao;
import cqupt.ssh.entity.Students;
import cqupt.ssh.service.SSHService;
public class SSHServiceImpl implements SSHService {
// 下邊查詢方法要呼叫dao層,這裡聲明瞭dao後,還需要一個set方法,這樣和applicationContext中的配置結合,才可以把這個類交給spring管理,使用了這set方法,就把這個的實現類注入到service中
private SSHDao sSHDao ;
public void setsSHDao(SSHDao sSHDao) {
this.sSHDao = sSHDao;
}
@Override
public List<Students> getStudents() {
// TODO Auto-generated method stub
return sSHDao.getStudents();
}
}
七:開始web層
(web)action層的程式碼如下:
package cqupt.ssh.action;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import cqupt.ssh.entity.Students;
import cqupt.ssh.service.SSHService;
import cqupt.ssh.serviceImpl.SSHServiceImpl;
public class SSHAction {//其實為了更加規範這裡需要繼承ActionSupport類,然後實現一個execute方法,來作為主方法。
//下邊需要使用到service層,這裡宣告過後也需要是喲個set方法,來配合applicationContext中的配置,通過這個方法把service層注入到action(web)層
private SSHService sSHService ;
public void setsSHService(SSHService sSHService) {
this.sSHService = sSHService;
}
public String getStudents() {
List<Students> list = sSHService.getStudents();
/*通過ActionContext.getContext().put("message",message);
* 直接放入將資料request域中,不獲取request本身,獲取其 中的Map;*/
ActionContext.getContext().put("message", list.get(0).getS_name());
return "success";//根據這個返回的”success"到struts配置中可以找到對應的頁面。
}
}
八:開始配置檔案:
第一個是applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<!-- 配置Dao -->
<bean name="sSHDao" class="cqupt.ssh.daoImpl.SSHDaoImpl"></bean>
<!-- 配置Service -->
<bean name="sSHService" class="cqupt.ssh.serviceImpl.SSHServiceImpl">
<!-- 下邊要注入set方法,要求在CustomerServiceImpl中新增setter方法之後,下邊的才可以進行 -->
<property name="sSHDao" ref="sSHDao"></property>
</bean>
<bean name="sSHAction" class="cqupt.ssh.action.SSHAction">
<!-- 下邊要注入set方法,要求在CustomerServiceImpl中新增setter方法之後,下邊的才可以進行 -->
<property name="sSHService" ref="sSHService"></property>
</bean>
</beans>
第二個是hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 資料庫驅動 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 資料庫url -->
<property name="hibernate.connection.url">jdbc:mysql:///ssh_students</property>
<!-- 資料庫連線使用者名稱 -->
<property name="hibernate.connection.username">root</property>
<!-- 資料庫連線密碼 -->
<property name="hibernate.connection.password">mysql</property>
<!-- 資料庫方言 不同的資料庫中,sql語法略有區別. 指定方言可以讓hibernate框架在生成sql語句時.針對資料庫的方言生成. sql99標準:
DDL 定義語言 庫表的增刪改查 DCL 控制語言 事務 許可權 DML 操縱語言 增刪改查 注意: MYSQL在選擇方言時,請選擇最短的方言. -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- #hibernate.show_sql true #hibernate.format_sql true -->
<!-- 將hibernate生成的sql語句列印到控制檯 -->
<property name="hibernate.show_sql">true</property>
<!-- 將hibernate生成的sql語句格式化(語法縮排) -->
<property name="hibernate.format_sql">true</property>
<!-- ## auto schema export 自動匯出表結構. 自動建表 #hibernate.hbm2ddl.auto create
自動建表.每次框架執行都會建立新的表.以前表將會被覆蓋,表資料會丟失.(開發環境中測試使用) #hibernate.hbm2ddl.auto create-drop
自動建表.每次框架執行結束都會將所有表刪除.(開發環境中測試使用) #hibernate.hbm2ddl.auto update(推薦使用) 自動生成表.如果已經存在不會再生成.如果表有變動.自動更新表(不會刪除任何資料).
#hibernate.hbm2ddl.auto validate 校驗.不自動生成表.每次啟動會校驗資料庫中表是否正確.校驗失敗. -->
<!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
<!-- 引入orm元資料 路徑書寫: 填寫src下的路徑 -->
<!-- 指定hibernate操作資料庫時的隔離級別 #hibernate.connection.isolation 1|2|4|8 0001
1 讀未提交 0010 2 讀已提交 0100 4 可重複讀 1000 8 序列化 -->
<property name="hibernate.connection.isolation">4</property>
<!-- 指定session與當前執行緒繫結 -->
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="cqupt/ssh/entity/Students.hbm.xml" />
</session-factory>
</hibernate-configuration>
第三個配置是struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 指定struts2是否以開發模式執行 1.熱載入主配置.(不需要重啟即可生效) 2.提供更多錯誤資訊輸出,方便開發時的除錯 -->
<constant name="struts.devMode" value="true"></constant>
<package name="cqupt" namespace="/" extends="struts-default"><!--是一個斜槓"/"表示後邊訪問CustomerAction的時候網址上不需要加中間名稱空間。 -->
<action name="sSHAction_*" class="cqupt.ssh.action.SSHAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
九:補充兩個前端頁面jsp,一個是用於訪問的頁面,一個跳轉頁面。
首先是開始訪問的頁面index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/sSHAction_getStudents"
method="post">
<input type="submit" value="查詢" />
</form>
</body>
</html>
然後是成功訪問資料庫後,獲取資料之後,把資料放入到另一個頁面success.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<tr>
<td>${message}<td>
</tr>
</body>
</html>
10,開始啟動專案後然後訪問網頁:
十一:注意事項:
a.實體類中的型別只能是類型別,不能是簡單型別,比如我裡面使用Integer,而不是int,如果那個地方是int,執行 的時候就會報錯should be mapped with insert="false" update="false"