- 浏览: 484320 次
- 性别:
- 来自: xiamen
文章分类
最新评论
-
陈乐鹏:
你这个下载要怎么玩?我这里老提示未安装!
[转]使用ScriptX控件进行Web打印总结 -
zqb666kkk:
博主有示例代码吗
cxf ws-security 加密和签名的实现 -
mclin0704:
你好,请问WSDL2Java 可以用java实现吗?
Java2WSDL之java实现 -
hubiao0629:
楼主,这个文件会有不兼容的问题把。
mysql5忘记密码后的处理方法 -
zhaoyubetter:
OK,可以试试!多谢楼主
后台管理界面收集
很久没有写java代码,这次发现Spring2.5+hibernate3.2可以像EJB使用注释
我不是什么告诉,Spring2.0 都没看过 我原来学的是1.2 而且公司都是框架封装好的
也没什么时间可以深究学习,E文有不好 o(∩_∩)o...哈哈
我的注释只是省去了配置文件。其他的还是跟原来差不多处理。网路上找了很多,都没有提及我想要的处理方式,也行太肤浅(在高手眼里)。
说了些废话,别拍砖,希望能给有用的人一些参考:
贴出代码与配置:
工具:myeclipse6.5+tomcat6.0+mysql5.0
首先请导入Spring2.5(最好是Spring2.5.6)与hibernate3.2的包
那位手头有Spring2.5.6的下载地址麻烦给个 官方要注册。。。
采用daobase的方法,其他dao可以公用
此处Daobase 为什么无法使用类似下面代码,如果要这么做怎么处理?
===============================
userinfoDao 接口
userinfoDaoImpl
userInfo Entity
userInfoService 接口
userInfoServiceImpl
Test 类
Web.xml
applicationContent.xml
另:此处事物配置这句为什么不能加readOnly
数据库脚本:
期望牛人指点下,我知道javaeye牛人很多 。
以上只是自己一点点摸索(只是在TestStr测试通过,未与表现层结合),欢迎拍砖!!!
附近是工程,大家直接导入可以使用!!!
谢谢指导!!!
顺便说下,头像很有意思
1:主键生成策略是我疏漏,忘记写:
@GeneratedValue(strategy=GenerationType.AUTO),一般我采用的是UUID生成方式。
2:AOP配置,我这个示例不涉及。
本示例只针对去除Hbm的xml文件,以及去除Spring管理bean的配置文件。[color=red][/color]
我觉得既然用了Spring ,那么Spring的特性还是会用。
好比hibernate3.2已经可以通过@NamedQuery 做查询,但我更愿意用hibernateTemp 去处理。
我不是什么告诉,Spring2.0 都没看过 我原来学的是1.2 而且公司都是框架封装好的
也没什么时间可以深究学习,E文有不好 o(∩_∩)o...哈哈
我的注释只是省去了配置文件。其他的还是跟原来差不多处理。网路上找了很多,都没有提及我想要的处理方式,也行太肤浅(在高手眼里)。
说了些废话,别拍砖,希望能给有用的人一些参考:
贴出代码与配置:
工具:myeclipse6.5+tomcat6.0+mysql5.0
首先请导入Spring2.5(最好是Spring2.5.6)与hibernate3.2的包
那位手头有Spring2.5.6的下载地址麻烦给个 官方要注册。。。
采用daobase的方法,其他dao可以公用
package model.dao; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.stereotype.Component; @Component public class DaoBase{ @Autowired public HibernateTemplate hibernateTemplate; public void createEntity(Object obj){ hibernateTemplate.save(obj); } public List loadAllEntity(Class clazz){ return hibernateTemplate.loadAll(clazz); } }
此处Daobase 为什么无法使用类似下面代码,如果要这么做怎么处理?
public class DaoBase extends HibernateDaoSupport{ /**保存单个对象 */ public void saveObject(Object obj){ getHibernateTemplate().save(obj); } /**查找指定对象所有记录,无where条件 返回List */ public List findObjectAll(Class entityClass){ return (List)getHibernateTemplate().loadAll(entityClass); } }
===============================
userinfoDao 接口
package model.dao; import java.util.List; import entity.Userinfo; public interface UserInfoDao { public abstract void addUserInfo(Userinfo user); public abstract List getUserInfoAll(Class user); }
userinfoDaoImpl
package model.dao.impl; import model.dao.DaoBase; import java.util.List; import org.springframework.stereotype.Component; import entity.Userinfo; import model.dao.UserInfoDao; @Component("userInfoDao") public class UserInfoDaoImpl extends DaoBase implements UserInfoDao { public void addUserInfo(Userinfo user){ createEntity(user); } public List getUserInfoAll(Class user){ return loadAllEntity(user); } }
userInfo Entity
package entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "userinfo", catalog = "test") public class Userinfo implements java.io.Serializable { // Fields private String id; private String username; private String password; private String email; // Constructors /** default constructor */ public Userinfo() { } /** full constructor */ public Userinfo(String id, String username, String password, String email) { this.id = id; this.username = username; this.password = password; this.email = email; } // Property accessors @Id @Column(name = "id", unique = true, nullable = false, length = 50) public String getId() { return this.id; } public void setId(String id) { this.id = id; } @Column(name = "username", nullable = false, length = 40) public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } @Column(name = "password", nullable = false, length = 20) public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } @Column(name = "email", nullable = false, length = 60) public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } }
userInfoService 接口
package model.service; import java.util.List; import entity.Userinfo; public interface UserInfoService { public abstract boolean createUserInfo(Userinfo user); public abstract List getUserInfoAll(Class user); }
userInfoServiceImpl
package model.service.impl; import model.dao.UserInfoDao; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import entity.Userinfo; import model.service.UserInfoService; @Component("userInfoService") public class UserInfoServiceImpl implements UserInfoService { //Spring 2.5 引入了 @Autowired 注释, //它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 @Autowired public UserInfoDao userInfoDao; /* (non-Javadoc) * @see service.impl.UserInfoService#createUserInfo(java.entity.UserInfo) */ public boolean createUserInfo(Userinfo user){ if(user==null) return false; try{ userInfoDao.addUserInfo(user); return true; }catch(Exception e){ e.printStackTrace(); return false; } } /* (non-Javadoc) * @see service.impl.UserInfoService#getUserInfoAll(java.lang.Class) */ public List getUserInfoAll(Class user){ List list = null; try{ list= userInfoDao.getUserInfoAll(Userinfo.class); }catch(Exception e){ e.printStackTrace(); return null; } return list; } }
Test 类
package test; import model.service.UserInfoService; import org.springframework.context.ApplicationContext; import entity.Userinfo; public class TestStr { private static ApplicationContext ctx = null; static{ ctx = new org.springframework.context.support.FileSystemXmlApplicationContext("F:\\workspaces\\jpaDemo\\WebRoot\\WEB-INF\\applicationContext.xml"); } /** * @param args */ public static void main(String[] args)throws Exception { // TODO Auto-generated method stub UserInfoService userInfoSrv = (UserInfoService) ctx.getBean("userInfoService"); Userinfo user = new Userinfo(); user.setId("20080002"); user.setUsername("zhong"); user.setPassword("1234"); user.setEmail("meme@163.com"); userInfoSrv.createUserInfo(user); } }
Web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name><!-- Spring --> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
applicationContent.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="jdbc:mysql://localhost:3306/test"> </property> <property name="username" value="root"></property> <property name="password" value=""></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> </bean> <!-- 配置hibernate SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <!-- spring2.5.6 可以直接 整个包搜索读取 <property name="packagesToScan" value="org.eline.entity*.*" /> --> <property name="annotatedClasses"> <list> <value>entity.Userinfo</value> </list> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <context:component-scan base-package="model" /> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <!-- 事务拦截器bean需要依赖注入一个事务管理器 --> <property name="transactionManager"> <ref local="transactionManager" /> </property> <property name="transactionAttributes"> <!-- 下面定义事务传播属性--> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="change*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- 配置hibernate 模板代码 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 定义自动代理BeanNameAutoProxyCreator --> <bean id="beanNameAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <!-- 指定对满足哪些bean name的bean自动生成业务代理 --> <property name="beanNames"> <list> <value>*Service</value> <!-- <value>*ServiceImpl</value> --> </list> </property> <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器--> <property name="interceptorNames"> <list> <!-- 此处可增加其他新的Interceptor --> <value>transactionInterceptor</value> </list> </property> </bean> </beans>
另:此处事物配置这句为什么不能加readOnly
<prop key="*">PROPAGATION_REQUIRED</prop>
数据库脚本:
USE `test`; /*Table structure for table `userinfo` */ DROP TABLE IF EXISTS `userinfo`; CREATE TABLE `userinfo` ( `id` varchar(50) NOT NULL, `username` varchar(40) NOT NULL, `password` varchar(20) NOT NULL, `email` varchar(60) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
期望牛人指点下,我知道javaeye牛人很多 。
以上只是自己一点点摸索(只是在TestStr测试通过,未与表现层结合),欢迎拍砖!!!
附近是工程,大家直接导入可以使用!!!
- jpaDemo.rar (480.5 KB)
- 下载次数: 362
评论
4 楼
xiaofengtoo
2008-12-09
xuyao 写道
这位同学,spring现在现在不用注册可以下载,目前会转到社区,出现个界面让你填写一些信息,在这个页面的左下角有个蓝色的连接,直接可以下载。目前spring改变了维护策略,只为社区提供版本更新。不过你不要用ie8,ie8(目前是beta版本)无法显示下载页面
谢谢指导!!!
顺便说下,头像很有意思
3 楼
xuyao
2008-12-09
这位同学,spring现在现在不用注册可以下载,目前会转到社区,出现个界面让你填写一些信息,在这个页面的左下角有个蓝色的连接,直接可以下载。目前spring改变了维护策略,只为社区提供版本更新。不过你不要用ie8,ie8(目前是beta版本)无法显示下载页面
2 楼
xiaofengtoo
2008-12-07
tangbo530 写道
主键生成策略呢!~
使用SPRING2.5为什么不使用aop配置!~
使用SPRING2.5为什么不使用aop配置!~
1:主键生成策略是我疏漏,忘记写:
@GeneratedValue(strategy=GenerationType.AUTO),一般我采用的是UUID生成方式。
2:AOP配置,我这个示例不涉及。
本示例只针对去除Hbm的xml文件,以及去除Spring管理bean的配置文件。[color=red][/color]
我觉得既然用了Spring ,那么Spring的特性还是会用。
好比hibernate3.2已经可以通过@NamedQuery 做查询,但我更愿意用hibernateTemp 去处理。
1 楼
tangbo530
2008-12-07
主键生成策略呢!~
使用SPRING2.5为什么不使用aop配置!~
使用SPRING2.5为什么不使用aop配置!~
发表评论
-
Spring Quartz任务调度示例
2011-05-16 17:29 1066package com.xzl.quartz; i ... -
spring 声明式事务,Propagation属性列表
2011-04-02 10:42 877@Transactional(propagation = Pr ... -
Spring hibernate 缓存配置
2010-02-05 15:36 2110最近项目中需要处理hibernate缓存: 网络上看了些文章 ... -
使用 Spring 2.5 基于注解驱动的 Spring MVC
2008-12-06 17:00 985http://www.ibm.com/developerwor ... -
Spring2.5 注解的魔力
2008-11-30 22:51 1281转此文 特别感谢remote_ro ... -
spring2.5 struts2.0.11 hibernate3.2 联合开发
2008-11-30 22:35 1440建立支持是很容易的。用myeclipse6.5 当然不用他也一 ... -
Spring 2.5的新特性:配置简化和基于注解的功能
2008-11-30 22:21 876http://developer.51cto.com/art/ ... -
spring2.5 + hibernate3.2 标注(annotation)开发的简单示例
2008-11-30 18:44 1763转载于:IamHades的专栏http://blog.csdn ... -
Spring2.5和ICEFaces开发Java EE
2008-11-30 18:37 1741ICEFaces是JSF组件的一个类库,并在此基础上添加了对A ...
相关推荐
这是一个struts 1.2 + spring 2.5 + hibernate 3.2框架demo, 运行环境为eclipse 3.2 + tomcat 5.5 + oracle 11g
struts2+spring2.5+hibernate3.2整合完整项目,带数据库脚本 ,mysql,tomcat,myeclipse完整工程
struts2.1 + spring 2.5 + hibernate 3.2 增删改查 详细例子
struts2+spring2.5+hibernate3.2 找了半天没看到。自己整了一个。
Spring2.5.chm Hibernate_3.2.0.chm Spring2.5和Hibernate3.2中文手册
struts2+spring2.5+hibernate3.2 + freemarker 全新功能实现的增删改查+freemarker 摸版 struts2 的方式自己去看简单。 spring2.5 是用注释来注入 hibernate3.2 是用ejb3注解映射关系 hibernate3 +个属性可以自动...
cdst ssh代码生成器,免费的,功能强悍的struts2+spring2.5+hibernate3.2的一套 代码生成系统。.zip cdst ssh代码生成器,免费的,功能强悍的struts2+spring2.5+hibernate3.2的一套 代码生成系统。.zip cdst ssh代码...
完整的struts1.2+hibernate3.2+spring2.5框架集成 完整的dwr2.0+spring2.5集成 处理的dwr2.0+hibernate3.2集成的问题
1、可以运行(发布后,删除\Tomcat 6.0\webapps\ssh\WebRoot\WEB-INF\lib下的asm-2.2.3.jar) 2、采用Struts1.2 + Spring2.5 + Hibernate3.2 整合开发 3、SSH入门级实例
spring2.5 + hibernate3.2x 标注(annotation)开发的简单示例 http://blog.csdn.net/IamHades/archive/2008/01/11/2038188.aspx
struts2+Spring2.5+Spring3.2)实现登录.struts2+Spring2.5+Spring3.2)实现登录.struts2+Spring2.5+Spring3.2)实现登录。
struts2+spring2.5+hibernate3.2整合所需的基本jar包,希望大家用得着。
Struts2.0+spring2.5+hibernate3.2
struts2.0+spring2.5+hibernate3.0整合框架,下载导入到eclipse下即可。。启动tomcat前先修改jdbc.properties文件。由于lib文件较大,所以请自行导入相关lib包。
非常好的学习资料 struts2+spring2.5+hibernate3.2大型项目spring mvc
对象持久层框架Hibernate和业务逻辑层框架spring的源代码,相信会对大家有帮助的,另外上传的还有struts1和struts2的源码,需要的人请搜索
jbpm4整合struts2+spring2.5+hibernate3.3.doc
struts2+spring2.5+hibernate3.2 例子
我希望能够得到大家的支持,交流,共进,所以不要大家的分数, ecside+struts2+spring2.5+hibernate3.2+mysql
NULL 博文链接:https://gaobo424.iteye.com/blog/870162