ssh整合

7 篇文章 0 订阅
5 篇文章 0 订阅

一、创建javaWeb项目

目录结构:

二、加入Spring

1、在pom.xml中加入以下包

 <!-- spring -->

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-core</artifactId>

    <version>3.1.2.RELEASE</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>3.1.2.RELEASE</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-jdbc</artifactId>

    <version>3.1.2.RELEASE</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-beans</artifactId>

    <version>3.1.2.RELEASE</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-web</artifactId>

    <version>3.1.2.RELEASE</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-expression</artifactId>

    <version>3.1.2.RELEASE</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-orm</artifactId>

    <version>3.1.2.RELEASE</version>

    </dependency>

2、把以下3个文件添加到src/main/resources目录下:

config.properties
log4j.properties
spring.xml

其中spring.xml的内容如下:

<!-- 引入属性文件 -->

<context:property-placeholder location="classpath:config.properties" />

<!-- 自动扫描dao和service包(自动注入) -->

<context:component-scan base-package="sy.dao,sy.service" />


3、在web.xml中添加如下配置:

<!-- spring配置文件位置 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring.xml</param-value>

</context-param>


<!-- spring监听器 -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

4、分别添加如下图所示包和类及接口

package sy.service;
接口:public interface UserServiceI {

public void test();

}

实现类:


import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import sy.service.UserServiceI;

@Service("userService")
public class UserServiceImpl implements UserServiceI {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(UserServiceImpl.class);
public void test() {
//System.out.println("OK");
logger.info("=======================");

}
}

测试类:

package sy.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import sy.service.impl.UserServiceImpl;

public class TestSpring {

@Test
public void test()
{
ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"classpath:spring.xml"});
UserServiceImpl serviceImpl=(UserServiceImpl) ac.getBean("userService");
serviceImpl.test();
}
}

三、加入Struts2

1、pom.xml中加入以下内容,及struts2所需要的包

<!-- Struts2 -->

<dependency>

<groupId>org.apache.struts</groupId>

<artifactId>struts2-core</artifactId>

<version>2.3.4.1</version>

</dependency>

<dependency>

<groupId>org.apache.struts</groupId>

<artifactId>struts2-spring-plugin</artifactId>

<version>2.3.4.1</version>

</dependency>

<dependency>

<groupId>org.apache.struts</groupId>

<artifactId>struts2-convention-plugin</artifactId>

<version>2.3.4.1</version>

</dependency>

2、在项目路径下添加struts2.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>

<!-- 指定由spring负责action对象的创建 -->

<constant name="struts.objectFactory" value="spring" />

<!-- 所有匹配*.action的请求都由struts2处理 -->

<constant name="struts.action.extension" value="action" />

<!-- 是否启用开发模式 -->

<constant name="struts.devMode" value="true" />

<!-- struts配置文件改动后,是否重新加载 -->

<constant name="struts.configuration.xml.reload" value="true" />

<!-- 设置浏览器是否缓存静态内容 -->

<constant name="struts.serve.static.browserCache" value="false" />

<!-- 请求参数的编码方式 -->

<constant name="struts.i18n.encoding" value="utf-8" />

<!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 -->

<constant name="struts.i18n.reload" value="true" />

<!-- 文件上传最大值 -->

<constant name="struts.multipart.maxSize" value="104857600" />

<!-- 让struts2支持动态方法调用 -->

<constant name="struts.enable.DynamicMethodInvocation" value="true" />

<!-- Action名称中是否还是用斜线 -->

<constant name="struts.enable.SlashesInActionNames" value="false" />

<!-- 允许标签中使用表达式语法 -->

<constant name="struts.tag.altSyntax" value="true" />

<!-- 对于WebLogic,Orion,OC4J此属性应该设置成true -->

<constant name="struts.dispatcher.parametersWorkaround" value="false" />

<package name="basePackage" extends="struts-default">

</package>

</struts>

3、在web.xml中添加如下配置:

<!-- Struts2配置 -->

<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>*.action</url-pattern>

</filter-mapping>

4、编写Action进行测试:

package sy.action;

import org.apache.log4j.Logger;

import org.apache.struts2.ServletActionContext;

import org.apache.struts2.convention.annotation.Action;

import org.apache.struts2.convention.annotation.Namespace;

import org.apache.struts2.convention.annotation.ParentPackage;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;

import sy.service.UserServiceI;


@ParentPackage("basePackage")

@Namespace("/")

@Action(value="userAction")

public class UserAction {

private static final Logger logger = Logger.getLogger(UserAction.class);

private UserServiceI userServiceI;

public UserServiceI getUserServiceI() {

return userServiceI;

}

@Autowired

public void setUserServiceI(UserServiceI userServiceI) {

this.userServiceI = userServiceI;

}

public void test(){

logger.info("进入Action");

// ApplicationContext ac=WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());

// UserServiceI userServiceI=(UserServiceI) ac.getBean("userService");

userServiceI.test();

}

}

四、Hibernate

1、加入

<!-- Hibernate4 -->

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-core</artifactId>

<version>4.1.7.Final</version>

</dependency>

<!-- fastJson -->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>fastjson</artifactId>

<version>1.1.24</version>

</dependency>

<!-- oracle数据库驱动 -->

<dependency>

<groupId>com.oracle</groupId>

<artifactId>ojdbc14</artifactId>

<version>10.2.0.1.0</version>

</dependency>

<!-- aspectjweaver -->

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

<version>1.7.0</version>

</dependency>

2、添加spring-hibernate.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

">

<!-- JNDI方式配置数据源 -->

<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->


<!-- 配置数据源 -->

<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">

<property name="url" value="${jdbc_url}" />

<property name="username" value="${jdbc_username}" />

<property name="password" value="${jdbc_password}" />

<!-- 初始化连接大小 -->

<property name="initialSize" value="0" />

<!-- 连接池最大使用连接数量 -->

<property name="maxActive" value="20" />

<!-- 连接池最大空闲 -->

<property name="maxIdle" value="20" />

<!-- 连接池最小空闲 -->

<property name="minIdle" value="0" />

<!-- 获取连接最大等待时间 -->

<property name="maxWait" value="60000" />

<!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->

<property name="validationQuery" value="${validationQuery}" />

<property name="testOnBorrow" value="false" />

<property name="testOnReturn" value="false" />

<property name="testWhileIdle" value="true" />

<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->

<property name="timeBetweenEvictionRunsMillis" value="60000" />

<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->

<property name="minEvictableIdleTimeMillis" value="25200000" />

<!-- 打开removeAbandoned功能 -->

<property name="removeAbandoned" value="true" />

<!-- 1800秒,也就是30分钟 -->

<property name="removeAbandonedTimeout" value="1800" />

<!-- 关闭abanded连接时输出错误日志 -->

<property name="logAbandoned" value="true" />

<!-- 监控数据库 -->

<!-- <property name="filters" value="stat" /> -->

<property name="filters" value="mergeStat" />

</bean>

<!-- 配置hibernate session工厂 -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="hibernateProperties">

<props>

<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>

<prop key="hibernate.dialect">${hibernate.dialect}</prop>

<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>

<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>

</props>

</property>

<!-- 自动扫描注解方式配置的hibernate类文件 -->

<property name="packagesToScan">

<list>

<value>sy.model</value>

</list>

</property>


<!-- 自动扫描hbm方式配置的hibernate文件和.hbm文件 -->

<!--

<property name="mappingDirectoryLocations">

<list>

<value>classpath:sy/hbm</value>

</list>

</property>

-->

</bean>

<!-- 配置事务管理器 -->

<bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

<!-- 注解方式配置事物 -->

<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->

<!-- 拦截器方式配置事物 -->

<tx:advice id="transactionAdvice" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="add*" />

<tx:method name="save*" />

<tx:method name="update*" />

<tx:method name="modify*" />

<tx:method name="edit*" />

<tx:method name="delete*" />

<tx:method name="remove*" />

<tx:method name="repair" />

<tx:method name="deleteAndRepair" />

<tx:method name="get*" propagation="SUPPORTS" />

<tx:method name="find*" propagation="SUPPORTS" />

<tx:method name="load*" propagation="SUPPORTS" />

<tx:method name="search*" propagation="SUPPORTS" />

<tx:method name="datagrid*" propagation="SUPPORTS" />


<tx:method name="*" propagation="SUPPORTS" />

</tx:attributes>

</tx:advice>

<aop:config>

<aop:pointcut id="transactionPointcut" expression="execution(* sy.service..*Impl.*(..))" />

<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />

</aop:config>

</beans>

3、编写实体类:

package sy.model;

import java.io.Serializable;

import java.util.Date;



import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;

import javax.persistence.UniqueConstraint;



@Entity

@Table(name="TUSER",schema="orcl",uniqueConstraints=@UniqueConstraint(columnNames="NAME"))

public class Tuser implements Serializable {

/**

*/

private static final long serialVersionUID = 1L;

private String id;

private String name;

private String pwd;

private Date createdatetime;

private Date modifydatetime;

public Tuser() {

// TODO Auto-generated constructor stub

}

public Tuser(String id, String name, String pwd) {

super();

this.id = id;

this.name = name;

this.pwd = pwd;

}

public Tuser(String id, String name, String pwd, Date createdatetime,

Date modifydatetime) {

super();

this.id = id;

this.name = name;

this.pwd = pwd;

this.createdatetime = createdatetime;

this.modifydatetime = modifydatetime;

}

@Temporal(TemporalType.DATE)

@Column(name="CREATEDATETIME",length=7)

public Date getCreatedatetime() {

return createdatetime;

}

@Id

@Column(name="ID",unique=true,nullable=false,length=36)

public String getId() {

return id;

}

@Temporal(TemporalType.DATE)

@Column(name="MODIFYDATETIME",length=7)

public Date getModifydatetime() {

return modifydatetime;

}

@Column(name="NAME",unique=true,nullable=false,length=100)

public String getName() {

return name;

}

@Column(name="PWD",nullable=false,length=32)

public String getPwd() {

return pwd;

}

public void setCreatedatetime(Date createdatetime) {

this.createdatetime = createdatetime;

}

public void setId(String id) {

this.id = id;

}

public void setModifydatetime(Date modifydatetime) {

this.modifydatetime = modifydatetime;

}

public void setName(String name) {

this.name = name;

}

public void setPwd(String pwd) {

this.pwd = pwd;

}

}

4、编写测试类

package sy.test;

import java.util.Date;

import java.util.UUID;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import sy.model.Tuser;

import sy.service.UserServiceI;

public class TestHibernate {

@Test

public void test(){

ApplicationContext ac=new ClassPathXmlApplicationContext(new String[] {"classpath:spring.xml","classpath:spring-hibernate.xml"});

UserServiceI userServiceI=(UserServiceI) ac.getBean("userService");

Tuser user=new Tuser();

user.setId(UUID.randomUUID().toString());

user.setName("张三");

user.setPwd("ltj");

user.setCreatedatetime(new Date());

userServiceI.save(user);

}

}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值