Eclipse注解搭建Struts2+Spring3+Hibernate3+Oracle11g

Eclipse注解搭建Struts2+Spring3+Hibernate3+Oracle11g

好久没有搞过这玩意了,这个只是实现一个增加的功能。简单,木有拦截,

有 验证, 木有 AOP。就是实现了一个自动创建数据库表插入的功能。但是

花费了很长的时间。


里面有可能有些冗余的东西,当然也包括所用的jar,不过可以将就的看看!



也不全部贴出全部代码了,找几个稍微重要的贴出来。


lib目录的jar文件将会截图

*************************************************************************************************


web.xml (位于WEB-INF目录下)文件配置 


<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

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">

<filter>

<filter-name>struts2</filter-name>

<filterclass>

   org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

               </filter-class>

</filter>


<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>


<!-- spring监听器 -->

<listener>

<listenerclass>

      org.springframework.web.context.ContextLoaderListener

                </listener-class>

</listener>

</web-app>


struts.xml (位于src目录下) 配置:


<?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>

<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false -->

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


<package name="root" namespace="/ssh" extends="struts-default">

<action name="useraction_*" class="com.ssh.action.UserAction"

method="{1}">

<result>/userinfo.jsp</result>

<result name="index">/index.jsp</result>

</action>

</package>


</struts>


applicationContext.xml  (位于WEB-INF目录下) 配置:


<?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:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

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

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

    http://www.springframework.org/schema/context/spring-context-3.1.xsd

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

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

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

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

default-autowire="byName">

<!-- 自动扫描 -->

<context:component-scan base-package="com.ssh.*" />

<!-- AOP 支持 -->

<aop:aspectj-autoproxy />

<!-- 注解事务支持 -->

<tx:annotation-driven />


<!-- 配置数据库连接 -->

<bean name="dataSource"

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="url" value="jdbc:oracle:thin:@192.168.1.108:1521:orcl" />

<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />

<property name="username" value="oracle" />

<property name="password" value="orcl655" />

</bean>


<!-- 集成hibernate的配置文件 -->

<bean name="sessionFactory"

class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

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

<property name="packagesToScan" value="com.ssh.*" />

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>

<prop key="hibernate.show_sql">true</prop>

<prop key="hibernate.hbm2ddl.auto">update</prop>

</props>

</property>

</bean>


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

<bean name="transactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

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

</bean>

</beans>

************************************************************************************************************


实体bean   SshUser  类文件完整代码:

package com.ssh.bean;


import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;


@Entity

public class SshUser {


@Id @GeneratedValue

private int userid;

private String username;


public int getUserid() {

return userid;

}


public void setUserid(int userid) {

this.userid = userid;

}


public String getUsername() {

return username;

}


public void setUsername(String username) {

this.username = username;

}


public SshUser(String username) {

super();

this.username = username;

}


public SshUser(int userid, String username) {

super();

this.userid = userid;

this.username = username;

}

}


Action  UserAction  文件完整代码


package com.ssh.action;


import javax.annotation.Resource;


import org.springframework.stereotype.Controller;


import com.opensymphony.xwork2.ActionSupport;

import com.ssh.bean.SshUser;

import com.ssh.service.ISshUserService;


@Controller

@SuppressWarnings("serial")

public class UserAction extends ActionSupport {

private SshUser sshuser;

@Resource

private ISshUserService sshUserService;


public SshUser getSshuser() {

return sshuser;

}


public void setSshuser(SshUser sshuser) {

this.sshuser = sshuser;

}


public String query() {

sshuser = new SshUser(1, "five");

return SUCCESS;

}


/**

* 添加用户

* @return

*/

public String add() {

sshUserService.save(new SshUser("style"));

return "index";

}

}


Dao实现类 SshUserDaoImpl 完整代码:

package com.ssh.dao.impl;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ssh.bean.SshUser;
import com.ssh.dao.ISshUserDao;

@Repository
public class SshUserDaoImpl extends HibernateDaoSupport implements ISshUserDao {
@Override
@Transactional
public void add(SshUser user) {
this.getSession().persist(user);
}
}

***********************************************************************************************************************



工程截图:




 


lib目录截图





  

 http://five.iteye.com/blog/1721849

猜你喜欢

转载自five.iteye.com/blog/1721849
今日推荐