SSH搭建

作者: WBert | 来源:发表于2016-11-15 15:41 被阅读0次

问题:

既然开始学习搭建SSH框架了,就必须对其3个框架起到的作用有一定的了解。

1.struts2------替代了原来servlet的作用,对Http的请求进行处理和逻辑验证。

2.spring-------相当于中间商的作用,它把hibernate的配置文件并把一些依赖属性进行赋值,实现了很大的分层作用,让耦合性大大降低。

3.Hibernate的作用替代了原来JDBC的编写,他的优势就在于能够进行映射的处理,从而大大优化了与数据库的连接。

这是我现在所了解的SSH里的3大技术,因为是学习第一遍所以还不够深刻,现在我在做SSH框架塔的唐诗搜索网站,先对原来做的Hibernate技术 和 Struts2技术实现的唐诗查找系统进行总结。


工程名PoatQueryStruts(Struts2+JDBC技术)

包: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 {// 用工厂类进行数据库连接还有数据库搜索操作,返回结果储存在迭代器里Listall = DAOFactory.getIPoetriesDaoInstance().findPoertries(queryType, queryContent);

// 将迭代器存进Session里面,进行数据共享

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;

}

```

包com.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("配置文件读取错误!");

}

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配置文件

相关文章

网友评论

      本文标题:SSH搭建

      本文链接:https://www.haomeiwen.com/subject/addkpttx.html