ssh

  spring4.2 + struts2.3 + hibernate 5 整合方案

1、复制相关的jar文件到 lib目录下
antlr-2.7.7.jar
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
freemarker-2.3.22.jar
struts2-core-2.3.24.1.jar
struts2-spring-plugin-2.3.24.1.jar
xwork-core-2.3.24.1.jar


commons-logging-1.1.3.jar

commons-dbcp2-2.1.1.jar
commons-pool2-2.4.2.jar

dbmysql.jar

dom4j-1.6.1.jar
geronimo-jta_1.1_spec-1.1.1.jar
hibernate-commons-annotations-5.0.1.Final.jar
hibernate-core-5.0.6.Final.jar
hibernate-entitymanager-5.0.6.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-2.0.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.3.0.Final.jar
log4j-api-2.2.jar
log4j-core-2.2.jar
ognl-3.0.6.jar

spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE.jar
spring-context-support-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar
spring-instrument-4.2.4.RELEASE.jar
spring-jdbc-4.2.4.RELEASE.jar
spring-orm-4.2.4.RELEASE.jar
spring-tx-4.2.4.RELEASE.jar
spring-web-4.2.4.RELEASE.jar

2、编辑web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>springs</display-name>

<!-- spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:bean*.xml</param-value>
</context-param>
<!-- spring end -->

<!-- struts2.x -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- struts2.x end -->


<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

3、在src目录下建立bean.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="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/tx 
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/db?useUnicode=true&amp;characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="fengze" />
</bean>


<bean id="dbcpds" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/db?useUnicode=true&amp;characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="fengze" />
</bean>



<bean id="sf" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dbcpds"></property>
<property name="mappingResources">
<list></list>
</property>

<property name="annotatedClasses">
<list>
<value>com.entity.Book</value>
</list>
</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>

<bean id="bookdaoimpl" class="com.db.dao.BookDaoImpl" init-method="init">
<property name="sf" ref="sf"></property>
</bean>


<!-- struts action -->
<bean id="test" class="com.action.Test" scope="prototype"/>

<bean id="book" class="com.action.BookAction" scope="prototype">
<property name="bookdao" ref="bookdaoimpl"></property>
</bean>


</beans>



src 目录下建立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>
   <constant name="struts.i18n.reload" value="true" />
   <constant name="struts.enable.DynamicMethodInvocation" value="true" />
   <constant name="struts.devMode" value="true" />
   <constant name="struts.configuration.xml.reload" value="true" />
   <constant name="struts.action.extension" value="action,," />
   <constant name="struts.objectFactory" value="spring" />
   

   <package name="default" extends="struts-default" namespace="/">
    <action name="t" class="test"/>
   
    <action name="book" class="book">
    <result>/success.jsp</result>
    </action>

    <action name="show" class="book" method="show">
    <result>/show.jsp</result>
    </action>

    <action name="del" class="book" method="del">
    <result type="redirectAction">
<param name="actionName">show</param>
            <param name="namespace">/</param>
</result>
    </action>
   
   
   </package>
   
   <!-- 引入其它配置文件 -->
   <!-- 
   <include file="struts-user.xml" />
    -->
</struts>


如果项目需要使用hibernate.cfg.xml文件,内容如下
<!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.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- mysql建立数据库 create database db default character set utf8; -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db?useUnicode=true&amp;characterEncoding=utf8 </property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">fengze</property>
<property name="hibernate.connection.autocommit">true</property>
   <property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="com.entity.Book" />
</session-factory>
</hibernate-configuration> 




4、编写相关的
com.entity
Book.java
package com.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "book")
public class Book {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(length=30)
private String name;
private double price;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public Book() {
super();
// TODO Auto-generated constructor stub
}

public Book(int id, String name, double price) {
super();
this.id = id;
this.name = name;
this.price = price;
}

}


com.db.dao
BookDaoImpl.java
package com.db.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.entity.Book;

public class BookDaoImpl implements BookDao{
private SessionFactory sf;
private Session session;


public SessionFactory getSf() {
return sf;
}

public void setSf(SessionFactory sf) {
this.sf = sf;
}

public void init(){
this.session = this.sf.openSession();
}

public void save(Book book) {
this.session.save(book);
this.session.beginTransaction().commit();
}

public void delete(int id) {
Query query = this.session.createQuery("delete from Book where id=?");
query.setInteger(0,id);
query.executeUpdate();
this.session.beginTransaction().commit();

}

public void save(String name, double price) {
Book b = new Book();
b.setName(name);
b.setPrice(price);
this.save(b);

}

public List<Book> queryById(int id) {
return null;
}

public List<Book> queryAll() {
return this.session.createQuery("from Book").list();
}

public void update(Book book) {

}

}



BookDao.java
package com.db.dao;
import java.util.List;
import com.entity.Book;
public interface BookDao {
public void save(Book book);
public void delete(int id);
public void save(String name,double price);
public List<Book> queryById(int id);
public List<Book> queryAll();
public void update(Book book);
}



com.action
Test.java

package com.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletResponseAware;
public class Test implements ServletResponseAware{
private HttpServletResponse res;

public void execute() throws IOException {
this.res.setCharacterEncoding("utf-8");
this.res.setContentType("text/html;charset=utf-8");
PrintWriter out = this.res.getWriter();
for(int i=0;i<=10;i++){
out.print("中文效果Hell"+i+"<br>");
}
System.out.println("test........");
out.flush();
out.close();
}

@Override
public void setServletResponse(HttpServletResponse res) {
this.res = res;
}

}

BookAction.java
package com.action;
import org.apache.struts2.ServletActionContext;
import com.db.dao.BookDao;
import com.entity.Book;
public class BookAction {
private Book book;
private BookDao bookdao;
private int id;



public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


public Book getBook() {
return book;
}


public void setBook(Book book) {
this.book = book;
}


public BookDao getBookdao() {
return bookdao;
}


public void setBookdao(BookDao bookdao) {
this.bookdao = bookdao;
}


public String execute(){
bookdao.save(book);
return "success";
}

public String show(){
ServletActionContext.getContext().put("books",bookdao.queryAll());
return "success";
}

public String del(){
bookdao.delete(this.id);
return "success";
}

}

5、测试看效果
http://localhost/ssh/t
http://localhost/ssh/index.jsp


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值