浏览 4484 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2005-07-20
我使用spring的源代码如下: ~~~~~DAO~~~~~~~~~~ public interface IPostDAO { public void savePost(Post post); public Post[] getAllPosts(); public Post getPostById(Integer id); } public class PostDAO extends HibernateDaoSupportimplements IPostDAO{ public PostDAO() { super(); } public void savePost(Post post){ getHibernateTemplate().saveOrUpdate(post); } public Post getPostById(Integer id) { Post post=null; return (Post)getHibernateTemplate().get(Post.class, id); } public Post[] getAllPosts(){ List result = new ArrayList(); result=getHibernateTemplate().find("from Post"); Post[] posts = new Post[result.size()]; result.toArray(posts); return posts; } } IUserDAO和UserDAO类似(不列出了) ~~~~~~~~~~~~~Service~~~~~~~~~~~~~~~~~~~~~~ public interface IPostService { public void savePost(PostDTO postdto); public List getAllPosts(); public void setUserdao(UserDAO userdao); } public class PostService implements IPostService { public PostService() { } private PostDAO postdao; private UserDAO userdao; public void setPostdao(PostDAO postdao) { this.postdao = postdao; } public void setUserdao(UserDAO userdao) { this.userdao = userdao; } public void savePost(PostDTO postdto){ try { Post post=new Post(); post.setPTitle(postdto.getPTitle()); post.setPContent(postdto.getPContent()); post.setPTime(postdto.getPTime()); User user=new User(); user=userdao.getUserById(new Integer(postdto.getUId())); post.setUser(user); postdao.savePost(post); } catch (RuntimeException e) { throw new RuntimeException("aa"+e.toString()); } } public List getAllPosts(){ try { List list=new ArrayList(); Post[] posts =postdao.getAllPosts(); if(posts!=null&&posts.length>0){ for(int i=0;i<posts.length;i++){ PostDTO postdto=new PostDTO(); postdto.setPTitle(posts[i].getPTitle()); postdto.setPContent(posts[i].getPContent()); postdto.setPTime(posts[i].getPTime()); list.add(postdto); } } return list; } catch (RuntimeException e) { throw new RuntimeException(e.getMessage()+"postservice you wen ti!!" ); } } } ~~~~~~~~~~~~~~~~配置文件~~~~~~~~~~~~~~~~~~~~~~~~ <?xml version="1.0" encoding="GB2312"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>org.gjt.mm.mysql.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost/cus</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value></value> </property> </bean> <bean id="mySessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource" /> </property> <property name="mappingResources"> <list> <value>User.hbm.xml</value> <value>Post.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> net.sf.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql"> true </prop> </props> </property> </bean> <bean id="myTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="mySessionFactory"/> </property> </bean> <!-- ORDER SERVICE --> <bean id="postService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref local="myTransactionManager"/> </property> <property name="target"> <ref local="postTarget"/> </property> <property name="transactionAttributes"> <props> <prop key="save*"> PROPAGATION_REQUIRED </prop> <prop key="get*"> PROPAGATION_REQUIRED </prop> </props> </property> </bean> <!-- ORDER TARGET PRIMARY BUSINESS OBJECT: Hibernate implementation --> <bean id="postTarget" class="service.PostService"> <property name="postdao"> <ref local="postdao"/> </property> <property name="userdao"> <ref local="userdao"/> </property> </bean> <!-- ORDER DAO OBJECT --> <bean id="userdao" class="dao.UserDAO"> <property name="sessionFactory"> <ref local="mySessionFactory"/> </property> </bean> <bean id="postdao" class="dao.PostDAO"> <property name="sessionFactory"> <ref local="mySessionFactory"/> </property> </bean> </beans> 配置是不是有错啊???,我一运行service里得到的总是空!不知道为什么???? 以前我用hibernate.cfg.xml去配置的数据源的。如果我想保留hibernate.cfg.xml这个配置应该怎么写啊?? 请多多指教! 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2005-07-26
可以自己写个工厂类,生成servcie实例
|
|
返回顶楼 | |
发表时间:2005-07-26
在你的action里边调用,拿到实例
|
|
返回顶楼 | |
发表时间:2005-07-28
好难看阿,这些代码。。。晕
|
|
返回顶楼 | |
发表时间:2005-07-28
你把代码打包发上来看看。
|
|
返回顶楼 | |