SSH的搭建
来源:互联网 发布:企业所得税申报软件 编辑:程序博客网 时间:2024/06/07 08:32
1、
(1)、struts的jar包:
(2)Spring的jar包:
(3)hibernate(jar)包:
2、在配置文件web.xml配置一个Struts过滤器和spring监听器。
<span style="font-family:Microsoft YaHei;font-size:24px;"><!-- struts2的过滤器 --> 9 <filter>10 <filter-name>struts2</filter-name>11 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>12 </filter>13 14 <filter-mapping>15 <filter-name>struts2</filter-name>16 <url-pattern>/*</url-pattern>17 </filter-mapping></span>
<!-- spring的监听器配置开始 -->21 <context-param> 22 <param-name>contextConfigLocation</param-name> 23 <param-value>classpath:applicationContext.xml</param-value> 24 </context-param>25 <listener>26 <listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>27 </listener>
3、在Java Resources下的src目录下创建四个包(package)进行分层。
(1)实体类entity
<span style="font-family:Microsoft YaHei;font-size:24px;"><span style="font-size:24px;">public class BookCard {private int cid ;private String name;private String sex ;private Date cardDate;private Double deposit;public int getCid() {return cid;}public void setCid(int cid) {this.cid = cid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Date getCardDate() {return cardDate;}public void setCardDate(Date cardDate) {this.cardDate = cardDate;}public Double getDeposit() {return deposit;}public void setDeposit(Double deposit) {this.deposit = deposit;}</span></span>(2)BookCard.hbm.xml(映射文件)
<span style="font-family:Microsoft YaHei;font-size:24px;"><?xml version="1.0" encoding="UTF-8"?><hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping"> <class name="ssh.entity.BookCard" table="BookCard"> <id name="cid" column="cid"> <generator class="native"></generator> </id> <property name="name" type="string" length="50" column="name" not-null="true"></property> <property name="sex" type="string" length="2" column="sex"></property> <property name="cardDate" type="date" column="cardDate"></property> <property name="deposit" type="double" column="deposit"></property> </class></hibernate-mapping></span>
(3)IndexService(接口类):
<span style="font-family:Microsoft YaHei;font-size:24px;">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);}</span>
(4)IndexServiceImpl(实现类):
<span style="font-family:Microsoft YaHei;font-size:24px;">package ssh.service;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import ssh.dao.IndexDao;import ssh.dao.IndexDaoImpl;import ssh.entity.BookCard;import ssh.util.MyConnection;public class IndexServiceImpl implements IndexService {private IndexDao id;//用于注入使用public void setId(IndexDao id) {System.out.println("有人给我注入了一个dao实例:"+id);this.id = id;}@Overridepublic List<BookCard> getAllBookCard() {//本类应该编写业务逻辑的代码,//但本例没有业务逻辑,就不用写。//访问数据库的代码,不会出现在service这一层//交给dao来操作数据库List<BookCard> myBookCardList = id.getAllBookCard();//进行其它的业务逻辑操作,比如增加多一个选项,是否过期//本例不需要//....return myBookCardList;}}</span>
(5)IndexDao(接口类):
<span style="font-family:Microsoft YaHei;font-size:24px;">package ssh.dao;import java.util.List;import ssh.entity.BookCard;public interface IndexDao {public List<BookCard> getAllBookCard();}</span>(6)IndexDaoImpl(实现类):
<span style="font-family:Microsoft YaHei;font-size:24px;">package ssh.dao;import java.math.BigDecimal;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.hibernate.query.Query;import ssh.entity.BookCard;import ssh.util.MyConnection;public class IndexDaoImpl implements IndexDao {//在SSH的设计理念:要使用某个实例,那么就定义声明一个对象,然后//给它添加set方法(用于spring注入进来),实现不要关注这个实例//来自于那里,以及怎么创建,或者它是谁private SessionFactory sessionFactory;public void setSessionFactory(SessionFactory sf) {this.sessionFactory = sf;}@Overridepublic List<BookCard> getAllBookCard() {//sessionFactory这个实例可以自己按常规的hibernate传统写法创建//也可以交给spring去托管//sessionFactory = new Configuration().configure().buildSessionFactory();Session session = sessionFactory.openSession();System.out.println("sessionFactory:"+sessionFactory);System.out.println("session:"+session);//执行查询之前,先插入2条数据BookCard bc1 = new BookCard();//bc1.setCid(cid);bc1.setName("小红");bc1.setSex("妖");bc1.setCardDate(new java.util.Date());bc1.setDeposit(55.8);session.save(bc1);////后面当使用JPA的时候,EntityManager 类似于 SessionQuery query = session.createQuery("from BookCard");////System.out.println("query:"+query);////这2行代码,会交给spring的AOP帮忙处理List<BookCard> list = query.getResultList();return list;}}</span>7、Action类:
<span style="font-family:Microsoft YaHei;font-size:24px;">package ssh.action;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.text.DecimalFormat;import java.util.ArrayList;import java.util.List;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import ssh.dao.IndexDao;import ssh.dao.IndexDaoImpl;import ssh.entity.BookCard;import ssh.service.IndexService;import ssh.service.IndexServiceImpl;import ssh.util.MyConnection;public class IndexAction extends ActionSupport {//声明service,但不给它创建具体的实现类的实例,private IndexService is = null;public void setIs(IndexService is) {this.is = is;}public String execute1() {List<BookCard> myBookCardList = is.getAllBookCard();System.out.println("结果集:"+myBookCardList.size());ActionContext ac = ActionContext.getContext();ac.put("myBookCardList", myBookCardList);return "success";}public String formatDouble(double s){DecimalFormat fmat=new DecimalFormat("\u00A4##.0"); return fmat.format(s);}}</span>
8、编写struts.xml(struts配置文件)、applicationContext.xml
(1)、struts:
<span style="font-family:Microsoft YaHei;font-size:24px;"><?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <!-- 上面的头,注意版本,从样例里复制过来 showcase.war\WEB-INF\src\java\struts.xml --> 6 7 <struts> 8 <!-- 告知Struts2运行时使用Spring来创建对象 --> 9 <constant name="struts.objectFactory" value="spring" />10 11 <!-- 第1步:先定义一个包 -->12 <package name="mypck001" extends="struts-default">13 <!-- 第2步:定义一个action,配置跳转信息 name 类似于Servlet @WebServlet("/IndexServlet") 14 http://xxxx/xxx/Index.action http://xxxx/xxx/Index class 对应于自己写的Action类 当不写method属性时,默认调用的是execute15 class="ssh.action.IndexAction" ** new ssh.action.IndexAction()16 设计思想:关心了具体的实现类必须改为不要关注那个实现类 加入spring后,struts的action节点的class属性意义发生变化,直接引用spring帮忙创建的实例 17 -->18 <action name="Index" class="myIndexAction">19 <!-- 跳转是forward/WEB-INF/是防止jsp不经过action就可以访问-->20 <!-- result接收返回的字符串,然后做对应的事情 -->21 <result name="success">/WEB-INF/jsp/index.jsp</result>22 </action>23 </package>24 </struts></span>
(2)applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" <span style="white-space:pre"></span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <span style="white-space:pre"></span>xmlns:p="http://www.springframework.org/schema/p" <span style="white-space:pre"></span>xmlns:aop="http://www.springframework.org/schema/aop" <span style="white-space:pre"></span>xmlns:context="http://www.springframework.org/schema/context" <span style="white-space:pre"></span>xmlns:jee="http://www.springframework.org/schema/jee" <span style="white-space:pre"></span>xmlns:tx="http://www.springframework.org/schema/tx" <span style="white-space:pre"></span>xsi:schemaLocation=" <span style="white-space:pre"></span>http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd <span style="white-space:pre"></span>http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd <span style="white-space:pre"></span>http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd<span style="white-space:pre"></span>http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd <span style="white-space:pre"></span>http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"><span style="white-space:pre"></span><!-- 类似于财务部门一样,类就是钱,所有需要类的实例都由srping去管理 --><span style="white-space:pre"></span><bean id="myIndexAction" class="ssh.action.IndexAction" scope="prototype"><span style="white-space:pre"></span><!-- setIs(myIndexService) --><span style="white-space:pre"></span><property name="is" ref="myIndexService"/><span style="white-space:pre"></span></bean><span style="white-space:pre"></span><span style="white-space:pre"></span><!-- myIndexService = new ssh.service.IndexServiceImpl() --><span style="white-space:pre"></span><bean id="myIndexService" class="ssh.service.IndexServiceImpl" scope="prototype"><span style="white-space:pre"></span><property name="id" ref="myIndexDao"/><span style="white-space:pre"></span></bean><span style="white-space:pre"></span><span style="white-space:pre"></span><bean id="myIndexDao" class="ssh.dao.IndexDaoImpl" scope="prototype"><span style="white-space:pre"></span><!-- 晚点再注入能用的seesionFactory --><span style="white-space:pre"></span><property name="sessionFactory" ref="mySessionFactory"></property><span style="white-space:pre"></span></bean><span style="white-space:pre"></span><span style="white-space:pre"></span><!-- <span style="white-space:pre"></span>错误的做法,new org.hibernate.internal.SessionFactoryImpl()<span style="white-space:pre"></span>不可以,需要configuration来创建<span style="white-space:pre"></span>bean id="mySessionFactory" class="org.hibernate.internal.SessionFactoryImpl"></bean<span style="white-space:pre"></span><span style="white-space:pre"></span> --><span style="white-space:pre"></span> <bean id="mySessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"><span style="white-space:pre"></span><!-- 注入连接池,包含了数据库用户名,密码等等信息 --><span style="white-space:pre"></span><property name="dataSource" ref="myDataSource"/><span style="white-space:pre"></span>
<pre name="code" class="html" style="font-size: 13.3333px;"><!-- 配置Hibernate的其他的属性 --><span></span><property name="hibernateProperties"><span></span><props><span></span><prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop><span></span><prop key="hibernate.show_sql">true</prop><span></span><prop key="hibernate.format_sql">true</prop><span></span><prop key="hibernate.connection.autocommit">false</prop><span></span><!-- 开机自动生成表 --><span></span><prop key="hibernate.hbm2ddl.auto">update</prop><span></span></props><span></span></property><span></span><property name="mappingResources"><span></span><list><span></span><value>ssh/entity/BookCard.hbm.xml</value><span></span></list><span></span></property><span></span><span></span></bean><span></span><span></span> <span></span><bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><span></span><property name="driverClass" value="com.mysql.jdbc.Driver"/><span></span><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/CardDB"/><span></span><property name="user" value="root"/><span></span><property name="password" value="123456"/><span></span><!-- 每300秒检查所有连接池中的空闲连接 --><span></span><property name="idleConnectionTestPeriod" value="300"></property><span></span><!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 --><span></span><property name="maxIdleTime" value="900"></property><span></span><!-- 最大连接数 --><span></span><property name="maxPoolSize" value="2"></property><span></span><span></span></bean></beans>
</pre><span style="font-family:Microsoft YaHei; font-size:24px">8、<span style="line-height:25px; background-color:rgb(254,254,242)">创建一个index.jsp页面将所有数据取出来显示到页面上。</span></span><p></p><p><span style="line-height:25px; background-color:rgb(254,254,242)"><span style="line-height:25px; background-color:rgb(254,254,242)"><span style="line-height:25px; background-color:rgb(254,254,242)"><span style="margin:0px; padding:0px; line-height:25px; background-color:rgb(254,254,242)"><span style="font-family:Microsoft YaHei; font-size:24px"></span></span></span></span></span></p><pre name="code" class="html"><span style="font-family:Microsoft YaHei;font-size:24px;"><%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri="/struts-tags" prefix="s" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head>10 <body>11 12 <table border="1">13 <tr> 14 <td>卡号</td>15 <td>姓名</td>16 <td>性别</td>17 <td>办卡日期</td>18 <td>押金</td>19 </tr>20 <!-- 使用struts2标签库中的iterator将所有数据遍历循环显示出来 -->21 <s:iterator value="#myBookCardList" status="bcs">22 <tr> 23 <td><s:property value="cid"></s:property></td>24 <td><s:property value="name"></s:property></td>25 <td><s:property value="sex"></s:property></td>26 <td><s:date name="cardDate" format="yyyy年MM月dd日"></s:date></td>27 <td><s:property value="%{formatDouble(deposit)}"></s:property></td>28 </tr>29 </s:iterator>30 <!-- 判断查询出来等于0,就显示“没有查找到数据” -->31 <s:if test="myBookCardList.size()==0">32 <tr> 33 <td colspan="7">没有查找到数据</td>34 </tr>35 </s:if>36 </table>37 </body>38 </html></span>
在ssh中使用Struts作为系统的整体基础架构,负责MVC的分离,在Struts框架的模型部分,控制业务跳转,利用Hibernate框架对持久层提供支持,Spring做管理,管理struts和hibernate。具体做法是:用面向对象的分析方法根据需求提出一些模型,将这些模型实现为基本的Java对象,然后编写基本的DAO接口,并给出Hibernate的DAO实现,采用Hibernate架构实现的DAO类来实现Java类与数据库之间的转换和访问,最后由Spring做管理,管理struts和hibernate。其实ssh框架最主要的本质是:“高内聚、低耦合”。
0 0
- eclipse搭建ssh的
- SSH框架的搭建
- SSH环境的搭建
- SSH框架的搭建
- SSH框架的搭建
- SSH框架的搭建
- ssh项目的搭建
- SSH框架的搭建
- SSH的搭建
- SSH环境的搭建
- SSH框架的搭建
- SSH框架的搭建
- SSH框架环境的搭建
- ssh搭建时的具体步骤
- 自己搭建的ssh xfire
- eclipse搭建ssh的全过程
- SSH框架环境的搭建
- SSH框架环境的搭建
- TCP协议中的三次握手和四次挥手
- iOS Keychain钥匙串,应用间数据共享打造iOS上的全家桶
- 浅谈Hybrid技术的设计与实现第第2章
- TCP滑动窗口协议
- 链表反转
- SSH的搭建
- 一个台湾佬的ble mesh组网
- 为什么用session和cookie
- 第二周-项目3 体验复杂度-汉诺塔问题
- 图像特征提取参考网页:
- myeclipse修改工程名包名,web.xml报org.xml.sax.SAXParseException
- 延时执行
- IOS上线流程
- Android事件分发机制完全解析,带你从源码的角度彻底理解