社区
Java EE
帖子详情
SessionBean中同时调用EntityBean和DAO的问题
kexsong
2003-05-15 01:08:37
在我的系统中,有这样的SessionBean:在它的事务方法中调用EntityBean的Finder方法获得一些数据,之后用DAO直接操作数据库,但是一直执行不下去。
请问,在SessionBean的事务方法中,能同时调用EntityBean和DAO操作数据库吗?如果能要注意些什么问题?
...全文
26
2
打赏
收藏
SessionBean中同时调用EntityBean和DAO的问题
在我的系统中,有这样的SessionBean:在它的事务方法中调用EntityBean的Finder方法获得一些数据,之后用DAO直接操作数据库,但是一直执行不下去。 请问,在SessionBean的事务方法中,能同时调用EntityBean和DAO操作数据库吗?如果能要注意些什么问题?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
2 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
steeven
2003-05-15
打赏
举报
回复
会不会是dao写和entity同一行数据了? 会不会产生锁定冲突? 可以查查看数据库的锁定冲突
pingcsdn
2003-05-15
打赏
举报
回复
根据现有的开发经验, 一般我们在SessionBean(或不用EJB的时候的Module Bean)
中取到Connection然后调用专门操作数据库(DAO)中的方法, 也就是在Bean
中进行Connection的打开与关闭操作, 而在DAO中进行Statement和ResultSet操作
SessionBean 调用 EntityBean,然后 EntityBean 调用 DAO 是完全可以的。
spring_MVC源码
弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,现在这一篇补上。下面开始贴代码。 文
中
用的框架版本:spring 3,hibernate 3,没有的,自己上网下。 先说web.xml配置: [java] view plaincopy 01.<?xml version="1.0" encoding="UTF-8"?> 02.
03.
s3h3
04.
05.
contextConfigLocation
06.
classpath:applicationContext*.xml
07.
08.
09.
org.springframework.web.context.ContextLoaderListener
10.
11. 12.
13.
spring
14.
org.springframework.web.servlet.DispatcherServlet
15.
1
16.
17.
18.
spring
<!-- 这里在配成spring,下边也要写一个名为spring-servlet.xml的文件,主要用来配置它的controller --> 19.
*.do
20.
21.
22.
index.jsp
23.
24.
spring-servlet,主要配置controller的信息 [java] view plaincopy 01.<?xml version="1.0" encoding="UTF-8"?> 02. <
bea
ns 03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 04. xmlns:context="http://www.springframework.org/schema/context" 05. xsi:schemaLocation="http://www.springframework.org/schema/
bea
ns http://www.springframework.org/schema/
bea
ns/spring-
bea
ns-3.0.xsd 06. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 07. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 08. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 09. 10.
11. <!-- 把标记了@Controller注解的类转换为
bea
n --> 12.
13. <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> 14. <
bea
n class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 15. 16. <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> 17. <
bea
n class="org.springframework.web.servlet.view.InternalResourceViewResolver" 18. p:prefix="/WEB-INF/view/" p:suffix=".jsp" /> 19. 20. <
bea
n id="multipartResolver" 21. class="org.springframework.web.multipart.commons.CommonsMultipartResolver" 22. p:defaultEncoding="utf-8" /> 23.
bea
ns> applicationContext.xml代码 [java] view plaincopy 01.<?xml version="1.0" encoding="UTF-8"?> 02.<
bea
ns 03. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 04. xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" 05. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 06. xsi:schemaLocation=" 07. http://www.springframework.org/schema/
bea
ns http://www.springframework.org/schema/
bea
ns/spring-
bea
ns-3.0.xsd 08. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 09. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 11. 12.
13.
<!-- 自动扫描所有注解该路径 --> 14. 15.
16. 17. <
bea
n id="
session
Factory" 18. class="org.springframework.orm.hibernate3.annotation.Annotation
Session
Factory
Bea
n"> 19.
20.
21.
22.
${dataSource.dialect}
23.
${dataSource.hbm2ddl.auto}
24.
update
25.
26.
27.
28.
29.
com.mvc.
entity
<!-- 扫描实体类,也就是平时所说的model --> 30.
31.
32.
bea
n> 33. 34. <
bea
n id="transactionManager" 35. class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 36.
37.
38.
bea
n> 39. 40. <
bea
n id="dataSource" 41. class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 42.
43.
44.
45.
46.
bea
n> 47. <!--
Dao
的实现 --> 48. <
bea
n id="
entity
Dao
" class="com.mvc.
dao
.
Entity
Dao
Impl"> 49.
50.
bea
n> 51.
52.
53. 54.
55.
bea
ns> hibernate.properties数据库连接配置 [java] view plaincopy 01.dataSource.password=123 02.dataSource.username=root 03.dataSource.databaseName=test 04.dataSource.driverClassName=com.mysql.jdbc.Driver 05.dataSource.dialect=org.hibernate.dialect.MySQL5Dialect 06.dataSource.serverName=localhost:3306 07.dataSource.url=jdbc:mysql://localhost:3306/test 08.dataSource.properties=user=${dataSource.username};databaseName=${dataSource.databaseName};serverName=${dataSource.serverName};password=${dataSource.password} 09.dataSource.hbm2ddl.auto=update 配置已经完成,下面开始例子 先在数据库建表,例子用的是mysql数据库 [java] view plaincopy 01.CREATE TABLE `test`.`student` ( 02. `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 03. `name` varchar(45) NOT NULL, 04. `psw` varchar(45) NOT NULL, 05. PRIMARY KEY (`id`) 06.) 建好表后,生成实体类 [java] view plaincopy 01.package com.mvc.
entity
; 02. 03.import java.io.Serializable; 04. 05.import javax.persistence.Basic; 06.import javax.persistence.Column; 07.import javax.persistence.
Entity
; 08.import javax.persistence.GeneratedValue; 09.import javax.persistence.GenerationType; 10.import javax.persistence.Id; 11.import javax.persistence.Table; 12. 13.@
Entity
14.@Table(name = "student") 15.public class Student implements Serializable { 16. private static final long serialVersionUID = 1L; 17. @Id 18. @Basic(optional = false) 19. @GeneratedValue(strategy = GenerationType.ID
ENTITY
) 20. @Column(name = "id", nullable = false) 21. private Integer id; 22. @Column(name = "name") 23. private String user; 24. @Column(name = "psw") 25. private String psw; 26. public Integer getId() { 27. return id; 28. } 29. public void setId(Integer id) { 30. this.id = id; 31. } 32. 33. public String getUser() { 34. return user; 35. } 36. public void setUser(String user) { 37. this.user = user; 38. } 39. public String getPsw() { 40. return psw; 41. } 42. public void setPsw(String psw) { 43. this.psw = psw; 44. } 45.}
Dao
层实现 [java] view plaincopy 01.package com.mvc.
dao
; 02. 03.import java.util.List; 04. 05.public interface
Entity
Dao
{ 06. public List<Object> createQuery(final String queryString); 07. public Object save(final Object model); 08. public void update(final Object model); 09. public void delete(final Object model); 10.} [java] view plaincopy 01.package com.mvc.
dao
; 02. 03.import java.util.List; 04. 05.import org.hibernate.Query; 06.import org.springframework.orm.hibernate3.HibernateCallback; 07.import org.springframework.orm.hibernate3.support.Hibernate
Dao
Support; 08. 09.public class
Entity
Dao
Impl extends Hibernate
Dao
Support implements
Entity
Dao
{ 10. public List<Object> createQuery(final String queryString) { 11. return (List<Object>) getHibernateTemplate().execute( 12. new HibernateCallback<Object>() { 13. public Object doInHibernate(org.hibernate.
Session
session
) 14. throws org.hibernate.HibernateException { 15. Query query =
session
.createQuery(queryString); 16. List<Object> rows = query.list(); 17. return rows; 18. } 19. }); 20. } 21. public Object save(final Object model) { 22. return getHibernateTemplate().execute( 23. new HibernateCallback<Object>() { 24. public Object doInHibernate(org.hibernate.
Session
session
) 25. throws org.hibernate.HibernateException { 26.
session
.save(model); 27. return null; 28. } 29. }); 30. } 31. public void update(final Object model) { 32. getHibernateTemplate().execute(new HibernateCallback<Object>() { 33. public Object doInHibernate(org.hibernate.
Session
session
) 34. throws org.hibernate.HibernateException { 35.
session
.update(model); 36. return null; 37. } 38. }); 39. } 40. public void delete(final Object model) { 41. getHibernateTemplate().execute(new HibernateCallback<Object>() { 42. public Object doInHibernate(org.hibernate.
Session
session
) 43. throws org.hibernate.HibernateException { 44.
session
.delete(model); 45. return null; 46. } 47. }); 48. } 49.}
Dao
在applicationContext.xml注入 <
bea
n id="
entity
Dao
" class="com.mvc.
dao
.
Entity
Dao
Impl">
bea
n>
Dao
只有一个类的实现,直接供其它service层
调用
,如果你想更换为其它的
Dao
实现,也只需修改这里的配置就行了。 开始写view页面,WEB-INF/view下新建页面student.jsp,WEB-INF/view这路径是在spring-servlet.xml文件配置的,你可以配置成其它,也可以多个路径。student.jsp代码 [xhtml] view plaincopy 01.<%@ page language="java" contentType="text/html; charset=UTF-8" 02. pageEncoding="UTF-8"%> 03.<%@ include file="/include/head.jsp"%> 04. 05.<html> 06.<head> 07.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 08.<title>添加</title> 09.
11.// -->
<!-- 13.table{ border-collapse:collapse; } 14.td{ border:1px solid #f00; } 15.--><style mce_bogus="1">table{ border-collapse:collapse; } 16.td{ border:1px solid #f00; }</style> 17.
<!-- 18.function add(){ 19. [removed].href="<%=request.getContextPath() %>/student.do?method=add"; 20.} 21. 22.function del(id){ 23.$.ajax( { 24. type : "POST", 25. url : "<%=request.getContextPath()%>/student.do?method=del&id;=" + id, 26. dataType: "json", 27. success : function(data) { 28. if(data.del == "true"){ 29. alert("删除成功!"); 30. $("#" + id).remove(); 31. } 32. else{ 33. alert("删除失败!"); 34. } 35. }, 36. error :function(){ 37. alert("网络连接出错!"); 38. } 39.}); 40.} 41.// -->
47.
48.
序号
49.
姓名
50.
密码
51.
操作
52.
53.
54.
"> 55.
56.
57.
58.
59. <input type="button" value="编辑"/> 60. <input type="button" value="${student.id}"/>')" value="删除"/> 61.
62. 63.
64. 65. 66.</body> 67.</html> student_add.jsp [xhtml] view plaincopy 01.<%@ page language="java" contentType="text/html; charset=UTF-8" 02. pageEncoding="UTF-8"%> 03.<%@ include file="/include/head.jsp"%> 04. 05.<html> 06.<head> 07.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 08.<title>学生添加</title> 09.
<!-- 10.function turnback(){ 11. [removed].href="<%=request.getContextPath() %>/student.do"; 12.} 13.// -->
/student.do?method=save"> 17.
18.
19.
姓名
<input id="user" name="user" type="text" /></td>
20.
密码
<input id="psw" name="psw" type="text" /></td>
21.
<input type="submit" value="提交"/><input type="button" value="返回" />
22.
23. 24.</form> 25.</body> 26.</html> controller类实现,只需把注解写上,spring就会自动帮你找到相应的
bea
n,相应的注解标记意义,不明白的,可以自己查下@Service,@Controller,@
Entity
等等的内容。 [java] view plaincopy 01.package com.mvc.controller; 02. 03.import java.util.List; 04. 05.import javax.servlet.http.HttpServletRequest; 06.import javax.servlet.http.HttpServletResponse; 07. 08.import org.apache.commons.logging.Log; 09.import org.apache.commons.logging.LogFactory; 10.import org.springframework.
bea
ns.factory.annotation.Autowired; 11.import org.springframework.stereotype.Controller; 12.import org.springframework.ui.ModelMap; 13.import org.springframework.web.bind.annotation.RequestMapping; 14.import org.springframework.web.bind.annotation.RequestMethod; 15.import org.springframework.web.bind.annotation.RequestParam; 16.import org.springframework.web.servlet.ModelAndView; 17. 18.import com.mvc.
entity
.Student; 19.import com.mvc.service.StudentService; 20. 21.@Controller 22.@RequestMapping("/student.do") 23.public class StudentController { 24. protected final transient Log log = LogFactory 25. .getLog(StudentController.class); 26. @Autowired 27. private StudentService studentService; 28. public StudentController(){ 29. 30. } 31. 32. @RequestMapping 33. public String load(ModelMap modelMap){ 34. List<Object> list = studentService.getStudentList(); 35. modelMap.put("list", list); 36. return "student"; 37. } 38. 39. @RequestMapping(params = "method=add") 40. public String add(HttpServletRequest request, ModelMap modelMap) throws Exception{ 41. return "student_add"; 42. } 43. 44. @RequestMapping(params = "method=save") 45. public String save(HttpServletRequest request, ModelMap modelMap){ 46. String user = request.getParameter("user"); 47. String psw = request.getParameter("psw"); 48. Student st = new Student(); 49. st.setUser(user); 50. st.setPsw(psw); 51. try{ 52. studentService.save(st); 53. modelMap.put("addstate", "添加成功"); 54. } 55. catch(Exception e){ 56. log.error(e.getMessage()); 57. modelMap.put("addstate", "添加失败"); 58. } 59. 60. return "student_add"; 61. } 62. 63. @RequestMapping(params = "method=del") 64. public void del(@RequestParam("id") String id, HttpServletResponse response){ 65. try{ 66. Student st = new Student(); 67. st.setId(Integer.valueOf(id)); 68. studentService.delete(st); 69. response.getWriter().print("{/"del/":/"true/"}"); 70. } 71. catch(Exception e){ 72. log.error(e.getMessage()); 73. e.printStackTrace(); 74. } 75. } 76.} service类实现 [java] view plaincopy 01.package com.mvc.service; 02. 03.import java.util.List; 04. 05.import org.springframework.
bea
ns.factory.annotation.Autowired; 06.import org.springframework.stereotype.Service; 07.import org.springframework.transaction.annotation.Transactional; 08. 09.import com.mvc.
dao
.
Entity
Dao
; 10.import com.mvc.
entity
.Student; 11. 12.@Service 13.public class StudentService { 14. @Autowired 15. private
Entity
Dao
entity
Dao
; 16. 17. @Transactional 18. public List<Object> getStudentList(){ 19. StringBuffer sff = new StringBuffer(); 20. sff.append("select a from ").append(Student.class.getSimpleName()).append(" a "); 21. List<Object> list =
entity
Dao
.createQuery(sff.toString()); 22. return list; 23. } 24. 25. public void save(Student st){ 26.
entity
Dao
.save(st); 27. } 28. public void delete(Object obj){ 29.
entity
Dao
.delete(obj); 30. } 31.} OK,例子写完。有其它业务内容,只需直接新建view,并实现相应comtroller和service就行了,配置和
dao
层的内容基本不变,也就是每次只需写jsp(view),controller和service
调用
dao
就行了。 怎样,看了这个,spring mvc是不是比ssh实现更方便灵活。
Xdoclet生成
Session
Bea
n 和
Entity
Bea
n代码
xDoclet 简介使用XDoclet,你能够在J2EE环境下更加高效地工作,你所看到的
Bea
n以及
Bea
n之间的关系将更加简单,许多繁杂的事情将远离你的EJB开发过程。XDoclet从Rickard Oberg创建的EJBDoclet工具发展而来,它的设想很简单:避免为每个EJB提供多个文件,而是从单一
Bea
n类文件
中
提供组件需要的所有信息。那么,这是如何实现的呢?Java没有.NET吹
什么是Java
Bea
n、
bea
n? 什么是POJO、PO、DTO、VO、BO ? 什么是EJB、
Entity
Bea
n?
前言: 在Java开发
中
经常遇到这些概念
问题
,有的可能理解混淆,有的可能理解不到位,特此花了很多时间理顺了这些概念。不过有些概念实际开发
中
并没有使用到,可能理解还不够准确,只能靠后续不断纠正了。 1、什么是POJO ? POJO(Plain Old Java Object)这种叫法是Martin Fowler、Rebecca Parsons和Josh MacKenzie在2000年的一...
Android Green
DAO
ORM的使用(一) 生成
DAO
和
Bea
n
简介使用Green
DAO
生成
DAO
和
Bea
n
Java各种对象(PO,BO,VO,DTO,POJO,
DAO
,
Entity
,Java
Bea
n,Java
Bea
ns)的区分
PO:持久对象 (persistent object),po(persistent object)就是在Object/Relation Mapping框架
中
的
Entity
,po的每个属性基本上都对应数据库表里面的某个字段。完全是一个符合Java
Bea
n规范的纯Java对象,没有增加别的属性和方法。持久对象是由insert数据库创建,由数据库delete删除的。基本上持久对象生命周期和数据库密切相关。 VO:值对象(Value Object),通常用于业务层之间的数据传递,和PO一样也是仅仅包含数据而已。但
Java EE
67,515
社区成员
225,878
社区内容
发帖
与我相关
我的任务
Java EE
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
复制链接
扫一扫
分享
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章