首页 > 代码库 > 6.Spring+Struts+Hibernat注解方式整合

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="http://www.mamicode.com/true"/>
<constant name="struts.enable.DynamicMethodInvocation" value="http://www.mamicode.com/true"/>
<constant name="struts.objectFactory" value="http://www.mamicode.com/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="http://www.mamicode.com/${jdbc.driverClassName}"/>
<property name="url" value="http://www.mamicode.com/${jdbc.url}"/>
<property name="username" value="http://www.mamicode.com/${jdbc.username}"/>
<property name="password" value="http://www.mamicode.com/${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 = "http://www.mamicode.com/prototype")
@ParentPackage(value = "http://www.mamicode.com/struts-default")
public class UserController extends ActionSupport {

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

@Action(value = "http://www.mamicode.com/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注解方式整合