問題:
既然開始學(xué)習(xí)搭建SSH框架了,就必須對(duì)其3個(gè)框架起到的作用有一定的了解椭住。
1.struts2------替代了原來servlet的作用,對(duì)Http的請(qǐng)求進(jìn)行處理和邏輯驗(yàn)證。
2.spring-------相當(dāng)于中間商的作用剖淀,它把hibernate的配置文件并把一些依賴屬性進(jìn)行賦值,實(shí)現(xiàn)了很大的分層作用节猿,讓耦合性大大降低骂铁。
3.Hibernate的作用替代了原來JDBC的編寫,他的優(yōu)勢就在于能夠進(jìn)行映射的處理罩抗,從而大大優(yōu)化了與數(shù)據(jù)庫的連接拉庵。
這是我現(xiàn)在所了解的SSH里的3大技術(shù),因?yàn)槭菍W(xué)習(xí)第一遍所以還不夠深刻套蒂,現(xiàn)在我在做SSH框架塔的唐詩搜索網(wǎng)站钞支,先對(duì)原來做的Hibernate技術(shù) 和 Struts2技術(shù)實(shí)現(xiàn)的唐詩查找系統(tǒng)進(jìn)行總結(jié)。
工程名PoatQueryStruts(Struts2+JDBC技術(shù))
包:com.wyt.action
```
package com.wyt.action;import java.util.List;import javax.servlet.ServletRequest;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;import com.wyt.factory.DAOFactory;import com.wyt.vo.Poetries;public class QueryAction extends ActionSupport {/** *? */private static final long serialVersionUID = 1L;private int queryType;private String queryContent;public String execute() throws Exception {// 用工廠類進(jìn)行數(shù)據(jù)庫連接還有數(shù)據(jù)庫搜索操作,返回結(jié)果儲(chǔ)存在迭代器里L(fēng)istall = DAOFactory.getIPoetriesDaoInstance().findPoertries(queryType, queryContent);
// 將迭代器存進(jìn)Session里面,進(jìn)行數(shù)據(jù)共享
ServletRequest request = ServletActionContext.getRequest();
HttpServletRequest req = (HttpServletRequest) request;
req.setAttribute("queryType", queryType);
HttpSession session = req.getSession();
session.setAttribute("result", all);
if (all.isEmpty()) {
return "fail";
} else {
return "success";
}
}
public int getQueryType() {
return queryType;
}
public void setQueryType(int queryType) {
this.queryType = queryType;
}
public String getQueryContent() {
return queryContent;
}
public void setQueryContent(String queryContent) {
this.queryContent = queryContent;
}
}
```
包:com.wyt.dao
```
package com.wyt.dao;import java.util.List;import com.wyt.vo.Poetries;public interface IPoetriesDao {public ListfindPoertries(int querType,String queryContent)throws Exception;
}
```
包c(diǎn)om.wyt.dao.impl
```
package com.wyt.dao.impl;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import com.wyt.dao.IPoetriesDao;import com.wyt.vo.Poetries;public class PoetriesDaoImpl implements IPoetriesDao {private Connection conn = null;private PreparedStatement pstmt = null;public PoetriesDaoImpl(Connection conn) {this.conn = conn;}public ListfindPoertries(int queryType,String queryContent) throws Exception {Listall = new ArrayList();
String sql = "SELECT name,title,content "
+ "from poetries pt LEFT JOIN poets p on pt.poet_id = p.id where ";
//String hql = "select SUBSTRING(p.content,1,15) from Poetries p where p.poets.name=:poetName";
switch (queryType) {
case 1:
sql = sql+" name = ?";
break;
case 2:
sql = sql+"title LIKE ?";
queryContent = "%"+queryContent+"%";
break;
default:
sql = sql+"content LIKE ?";
queryContent = "%"+queryContent+"%";
break;
}
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, queryContent);
ResultSet rs = pstmt.executeQuery();
Poetries poetries? = null;
while(rs.next()){
poetries = new Poetries();
poetries.setName(rs.getString(1));
poetries.setTitle(rs.getString(2));
poetries.setContent(rs.getString(3));
all.add(poetries);
}
pstmt.close();
return all;
}
}
```
包:com.wyt.dao.proxy
```
package com.wyt.dao.proxy;import java.sql.Connection;import java.util.List;import com.wyt.dao.IPoetriesDao;import com.wyt.dao.impl.PoetriesDaoImpl;import com.wyt.util.ConnectionFactory;import com.wyt.vo.Poetries;public class PoetriesDaoProxy implements IPoetriesDao {private Connection conn = null;private IPoetriesDao dao = null;public PoetriesDaoProxy() {this.conn = ConnectionFactory.getInstance().makeConnection();this.dao = new PoetriesDaoImpl(this.conn);}public ListfindPoertries(int querType, String queryContent) throws Exception {Listall = null;
try{
all = this.dao.findPoertries(querType, queryContent);
}catch(Exception e){
throw e;
}finally{
this.conn.close();
}
return all;
}
}
```
包:com.wyt.factory
```
package com.wyt.factory;
import com.wyt.dao.IPoetriesDao;
import com.wyt.dao.proxy.PoetriesDaoProxy;
public class DAOFactory {
public static IPoetriesDao getIPoetriesDaoInstance()throws Exception{
return new PoetriesDaoProxy();
}
}
```
包名:com.wyt.util
```
package com.wyt.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class ConnectionFactory {
private static String driver;
private static String dburl;
private static String user;
private static String password;
private Connection conn = null;
private static final ConnectionFactory FACTORY =new ConnectionFactory();
static{
Properties prop= new Properties();
try {
InputStream in = ConnectionFactory.class.getClassLoader()
.getResourceAsStream("dbConfig.properties");
prop.load(in);
} catch (Exception e) {
System.out.println("配置文件讀取錯(cuò)誤操刀!");
}
driver = prop.getProperty("driver");
dburl = prop.getProperty("dburl");
System.out.println(dburl);
user = prop.getProperty("user");
password = prop.getProperty("password");
}
public Connection makeConnection(){
try {
Class.forName(driver);
conn = DriverManager.getConnection(dburl,user,password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static ConnectionFactory getInstance(){
return FACTORY;
}
public void close() throws Exception{
if(this.conn != null)
try {
this.conn.close();
} catch (Exception e) {
throw e;
}
}
}
```
包名:com.wyt.vo
```
package com.wyt.vo;
public class Poetries {
private String title;
private String name;
private String content;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
```
struts.xml配置文件