一,Struts2框架部署
1,搭建Struts2的jar包

1.1,Struts2所需各個jar包的作用
asm-3.3.jar 操作java字節碼的類庫
asm-commons-3.3 提供了基於事件的表現形式
commons-fileupload-1.3.2.jar , commons-io.jar struts的上傳下載 兩者缺一不可
commons-lang-3-3.1.jar 為java.lang包提供擴展
commons-logging-1.0.4.jar Jakarta的通用日志記錄包
freemarker-2.3.15.jar FreeMarker是一個模板引擎,一個基於模板生成文本輸出的通用工具
javassist-3.20.0.GA.jar 使Java字節碼操縱簡單。這是一個編輯Java字節碼的類庫。
ognl-2.1.10.jar 是支持ognl表達式
struts2-core-2.5.2.jar struts2的核心jar包,不可缺少的
xwork-core-2.3.30.jar xwork的核心包,由於Struts2是由xwork的延伸 有些類依然關聯着 xwork的類
1.2,將Struts2的jar包導入WebContent-->WEB-INF-->lib

2,配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ssh1</display-name>
<!-- 默認跳轉的頁面-->
<welcome-file-list>
<welcome-file>xiao.jsp</welcome-file>
</welcome-file-list>
<filter>
<!-- 配置Struts2核心Filter的名字 -->
<filter-name>struts2</filter-name>
<!-- 配置Struts2核心Filter的實現類 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 配置Filter攔截的URL -->
<filter-mapping>
<!-- 配置Struts2的核心FilterDispatcher攔截所有用戶請求 -->
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3,創建ssh.service包
3,1在ssh.service包中創建一個接口IndexService,實現以下兩個方法
package ssh.service;
import java.util.List;
import ssh.dao.IndexDao;
import ssh.entity.BookCard;
public interface IndexService {
public List<BookCard> getAllBookCard();
public void setId(IndexDao id);
}
3,2在ssh.service包在創建IndexServiceImpl類繼承IndexService接口
package ssh.service;
import java.util.List;
import ssh.dao.IndexDao;
import ssh.dao.IndexDaoImpl;
import ssh.entity.BookCard;
public class IndexServiceImpl implements IndexService {
private IndexDao id;
//用於注入使用
public void setId(IndexDao id) {
this.id = id;
}
@Override
public List<BookCard> getAllBookCard() {
//本類應該編寫業務邏輯的代碼,
//但本例沒有業務邏輯,就不用寫。
//訪問數據庫的代碼,不會出現在service這一層
//交給dao來操作數據庫
List<BookCard> myBookCardList = id.getAllBookCard();
return myBookCardList;
}
}
4,創建一個IndexAction類繼承ActionSupport
4.1單擊Browse

4.2在Choose a type 框中輸入ActionSupport 然后單擊Ok

4.3單擊Finish

4.4,IndexAction類
public class IndexAction extends ActionSupport {
//聲明service,但不給它創建具體的實現類的實例,
//因為:action不應該關注具體是誰來實現service
//具體service實現類是誰,我們使用spring注入進來
private IndexService is;
//**********定義1個List對象,保存BookCard結果,傳遞給jsp顯示******
//*******原來Servlet 需要request.setAttribute("bookCardList", bookCardList);
private List<BookCard> myBookCardList = null;
public List<BookCard> getMyBookCardList() {
return myBookCardList;
}
public void setMyBookCardList(List<BookCard> myBookCardList) {
this.myBookCardList = myBookCardList;
}
//******end 定義1個List對象,保存BookCard結果,傳遞給jsp顯示****
// 模擬IndexServlet的doGet方法
// 跟Servlet最大的差異:沒了request,也沒了response
public String execute1() {
// 第一步:獲取客戶端傳遞過來的參數(本例不需要)
// 第二步:對傳遞參數進行各種校驗、轉換、分析等等
// 本例不存在
//這段代碼,加入srping后,也要刪除
is = new IndexServiceImpl();
//以下2行代碼應該交給spring去做
IndexDao id = new IndexDaoImpl();
is.setId(id);
myBookCardList = is.getAllBookCard();
System.out.println("結果集:"+myBookCardList.size());
return "success";
}
public String formatDouble(double s){
DecimalFormat fmat=new DecimalFormat("\u00A4##.0");
return fmat.format(s);
}
}
5,創建struts.xml配置文件
5,1 struts.xml配置文件要在src目錄下

5,2 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">
<!-- 上面的頭,注意版本,從樣例里復制過來 showcase.war\WEB-INF\src\java\struts.xml -->
<struts>
<!-- 第1步:先定義一個包 -->
<package name="mypck001" extends="struts-default">
<!-- 第2步:定義一個action,配置跳轉信息 name 類似於Servlet @WebServlet("/IndexServlet")
http://xxxx/xxx/Index.action http://xxxx/xxx/Index
class 對應於自己寫的Action類
當不寫method屬性時,默認調用的是execute -->
<action name="Index" class="ssh1.IndexAction" method="execute">
<!--
跳轉是forward
/WEB-INF/是防止jsp不經過action就可以訪問
-->
<result name="success">/WEB-INF/jsp/index2.jsp</result>
</action>
</package>
</struts>
6,index.jsp
6,1 聲明struts2標簽

6,2調用struts2標簽的iterator標簽,循環輸出變量 myBookCardList

二,spring框架的搭建
1,在剛才搭建struts2框架的基礎上搭建spring
1,1,spring所需的jar包

1,2 spring 的各個jar包的作用
2,1 在IndexDaoImpl類中給MyConnection接口定義set 方法

2,2 在IndexAction類中給IndexService定義set方法

2,3 配置applicationContext.xml文件
applicationContext.xml文件必須配置在src目錄下

2,1applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 類似於財務部門一樣,類就是錢,所有需要類的實例都由srping去管理 -->
<bean id="myIndexAction" class="ssh1.IndexAction" scope="prototype">
<!-- setIs(myIndexService) -->
<property name="is" ref="myIndexService"/>
</bean>
<!-- myIndexService = new ssh.service.IndexServiceImpl() -->
<bean id="myIndexService" class="ssh.service.IndexServiceImpl" scope="prototype">
<property name="id" ref="myIndexDao"/>
</bean>
<bean id="myIndexDao" class="ssh.dao.IndexDaoImpl" scope="prototype">
<property name="c" ref="myConnection"></property>
</bean>
<bean id="myConnection" class="ssh.util.MyConnectionImpl_SQLSERVER" scope="prototype">
</bean>
</beans>
三,hibernate框架
1,1 hibernate所需的jar包

1,2 hibernate 所需jar包的作用


1,2 第1步:我們依舊需要引入hibernate的jar包
找到hibernate解壓好的文件目錄,在該目錄下的lib目錄下有一個required目錄,將該目錄下的所有jar包引入到我們項目的lib目錄下。

2,2 配置一個在src目錄下的一個實體映射文件BookCard.hbm.xml

2,3 BookCard.hbm.xml

2,4
解析:1>頭信息就不多說了,首先寫好一個hibernate-mapping首尾標記,在標記內映射我們相對應的實體類。
2>在class標記的name屬性的值對應我們的實體類(全路徑),table屬性對應我們在數據庫的表名
3>id標記為我們數據庫的主鍵配置相關信息,同理name屬性中的屬性值為我們實體類對象的屬性,column為數據庫對應表的字段。
4>property標記中type屬性為該屬性對應的java數據類型,length為數據庫中指定長度這里相當於varchar(50),not-null屬性為指定該字段是否可以為空,默認可以為空。
2,5 配置連接數據庫相關信息的hibernate.cfg.xml文件

2,6
解析:1>配置一個hibernate-configuration首尾標記,在其內部插入session-factory首尾標記。
2>在property標記中name屬性中指定dialect屬性值配置數據庫方言(數據庫不同以及版本不同方言也不同,查閱相關資料可知)
3>在property標記中name屬性中指定connection.pool屬性值配置連接池大小(依據訪問頻率配置)
4>在property標記中name屬性中指定show_sql屬性值配置是否顯示sql語句在控制台(開發過程中建議為true,易於調試)
5>在property標記中name屬性中指定format_sql屬性值配置sql語句是否需要格式化(廢話,當然要)
* 6>在property標記中name屬性中指定hbm2ddl.auto屬性值配置創表信息。值為update時,若數據庫沒有我們實體對應的表將自動為我們創建,若存在則會更新我的表。 值為create時,每次執行時會先查找數據庫是否存在這張表存在即先刪除在創建,不存在即直接創建。
7>連接數據庫的驅動以及制定數據庫URL,用戶名和密碼相信大家都懂,我就不一一介紹了。
* 8>我們的這份文件還需映射一個資源便是我們的實體映射文件,在mapping標記中指定。
2,7 配置hibernate,編寫我們的dao來操作數據庫
package dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import entity.BookCard;
/*
* 作者:我是碼農
* 看需求文檔8個小時(看得比經理更仔細)
* 抽象的需求(方法)經理已經給了, 編寫時間530分鍾
*/
public class BookCardDaoImpl implements BookCardDao {
private SessionFactory sessionFactory;
public void setSessionFactory1(SessionFactory sessionFactory1) {
this.sessionFactory1 = sessionFactory1;
}
public void setSessionFactory2(SessionFactory sessionFactory2) {
this.sessionFactory2 = sessionFactory2;
}
@Override
public List<BookCard> getAllCardInfo() {
/*Configuration cfg =new Configuration().configure();
sessionFactory=cfg.buildSessionFactory();*/
Session session1=sessionFactory1.openSession();
Session session2=sessionFactory2.openSession();
/*Transaction tx=session.beginTransaction();
BookCard bookcard=new BookCard();
session.save(bookcard);
tx.commit();*/
/*BookCard bc=new BookCard();*/
/*bc.setName("張三");
bc.setSex("男");
bc.setCardDate(new java.util.Date());
bc.setDeposit(66.4);
session1.save(bc);*/
@SuppressWarnings("unchecked")
Query<BookCard> query =session1.createQuery("from BookCard");
List<BookCard> list =query.getResultList();
for(BookCard data:list){
session2.save(data);
}
@SuppressWarnings("unchecked")
Query<BookCard> query2 =session2.createQuery("from BookCard");
List<BookCard> list2 =query2.getResultList();
session1.close();
session2.close();
sessionFactory1.close();
sessionFactory2.close();
return list2;
}
@Override
public List<BookCard> getQueryCardInfo() throws Exception {
throw new Exception("本方法未實現,請聯系XXXX,更新最新版本");
}
@Override
public boolean addCardInfo(BookCard bc) {
return false;
}
@Override
public boolean updateCardInfo(BookCard bc) {
return false;
}
@Override
public boolean updateMoreCardInfo(List<BookCard> list) {
return false;
}
}
2,6 在頁面輸出

第一次寫博客 ,有多不足,和不好的地方 請諒解!
