6.Spring+Struts+Hibernat注解方式整合

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了6.Spring+Struts+Hibernat注解方式整合相关的知识,希望对你有一定的参考价值。

SSH注解方式实现

1.创建db.properties文件,主要是数据库的连接数据

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/test
jdbc.username=root
jdbc.password=076634

2.创建Struts的配置文件,主要定义struts的常规配置

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

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

</struts>


3.定义的配置文件

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName">

<!--自动扫描包-->
<context:component-scan base-package="cn.yunhe"/>

<!--允许事物注解-->
<tx:annotation-driven/>

<!--读取定义的properties-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:db.properties</value>
</property>
</bean>

<!--兑取配置文件,构建连接池-->
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

<!--配置sessionFactory,扫描entity,以及配置hibernate的相关配置,一定要用annotation包下的类-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--扫描实体类-->
<property name="packagesToScan">
<value>cn.yunhe.model</value>
</property>

<!--hibernate参数配置-->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
</value>
</property>
</bean>

<!--初始化事物-->

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>


5.web.xml层


<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>

<!--Struts的过滤器-->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<!--spring的监听器,在系统启动的时候创建spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

</web-app>

 

 

6..包内类的操作

1:对实体类进行注解
@Entity
@Table(name = "user")
public class User {....


主键:
@Id
@GeneratedValue
@Column(name = "id")
public int getId() {
return id;
}
其他:
@Column(name = "pwd")


2:dao层:


@Repository("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

3:biz层:

@Service("userBiz")
public class UserBizImpl implements UserBiz {
//引入dao
@Resource(name = "userDao")
private UserDao userDao;

4:Struts控制器


//创建Struts的控制器

@Controller("user")
@Scope(value = "prototype")
@ParentPackage(value = "struts-default")
public class UserController extends ActionSupport {

//创建biz
@Resource(name = "userBiz")
private UserBiz userBiz;

@Action(value = "user",results = {
@Result(name = "success",location = "/list.jsp")
})
public String execute() throws Exception {


List<User> list = userBiz.searchAll();
//创建存储的actionContext
Map request = (Map) ActionContext.getContext().get("request");
request.put("list",list);
return SUCCESS;
}

public UserBiz getUserBiz() {
return userBiz;
}

public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}
}











































































































以上是关于6.Spring+Struts+Hibernat注解方式整合的主要内容,如果未能解决你的问题,请参考以下文章

SSH整合(struts2.3.24+hibernate3.6.10+spring4.3.2+mysql5.5+myeclipse8.5+tomcat6+jdk1.6)

spring+struts2+hibernate整合(ssh)

(转)hibernate-5.0.7+struts-2.3.24+spring-4.2.4三大框架整合

struts2学习笔记—— 在用户注冊程序中使用验证框架

SSH框架的搭建+JPA接口

Java--SSH,SSM和Spring Boot框架区别优缺点