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;
}
}

时间: 2024-08-03 11:18:58

6.Spring+Struts+Hibernat注解方式整合的相关文章

springmvc3.2+spring+hibernate4全注解方式整合(四)

以上是工程文件,下面开始测试 package test.testservice; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.t

springmvc3.2+spring+hibernate4全注解方式整合(一)

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

springmvc3.2+spring+hibernate4全注解方式整合(三)

service接口 package com.fangjian.core.platform.service; import com.fangjian.core.platform.po.User; public interface UserService { void saveUser(User user); } service实现 package com.fangjian.core.platform.service.impl; import org.springframework.beans.fa

springmvc3.2+spring+hibernate4全注解方式整合(二)

jdbc.properties #hibernate settings hibernate.show_sql=true hibernate.format_sql=true hibernate.cache.use_query_cache=true hibernate.cache.provider_class=net.sf.ehcache.hibernate.SingletonEhCacheProvider #mysql version database setting jdbc.driver=co

spring AOP自定义注解方式实现日志管理

转:spring AOP自定义注解方式实现日志管理 今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在applicationContext-mvc.xml中要添加的 <mvc:annotation-driven />     <!-- 激活组件扫描功能,在包com.gcx及其子包下面自动扫描通过注解配置的组件 -->     <conte

一个简单的Spring定时器例子 注解方式

首先在applicationContext.xml中增加 文件头中增加一条 xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation 中增加一条 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd <beans xmlns:task=&quo

跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Respository:标识持久层组件 3.@Service:标识业务层组件 4.@Controller:标识表现层组件 Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称 当在组件类上使用了特定的注解之后, 还需要在 Spring 的配置文件

Spring笔记(五)--注解方式实现AOP

包:aspectjrt.jar.aspectjweaver.jar AOP:面向切面的编程 1.XML配置: 2.注解. 一.注解方式: 打开注解处理器: <aop:aspectj-autoproxy/> 接口: 1 package com.dwr.spring.proxy; 2 3 public interface UserManager { 4 public void addUser(String username,String password); 5 public void delet

Spring基于纯注解方式的使用

经过上篇xml与注解混合方式,对注解有了简单额了解,上篇的配置方式极大地简化了xml中配置,但仍有部分配置在xml中进行,接下来我们就通过注解的方式将xml中的配置用注解的方式实现,并最终去掉xml配置. 一.xml中遗留配置 注解扫描 <!-- 开启注解并扫描指定包中带有注解的类 --> <context:component-scan base-package="com.kkb.spring.service"/> 非自定义bean,如sqlsessionFac