- dong198645
- 等级: 初级会员
- 性别:
- 文章: 27
- 积分: 30
- 来自: 北京
|
我想做一个公共的DAO类,然后用每个对象都可以访问这同一个DAO.不知道怎么做,我自己的想法如下..请指点指点...
DAO
- package com.text.comm;
- import com.text.base.entity.Role;
- import com.text.base.entity.User;
- import com.text.base.exception.DaoException;
- import com.text.base.util.TranscationManager;
-
-
-
- public class DAO{
-
-
-
- public Object loadObject(TranscationManager tm,Object t,int id) throws DaoException {
-
- try{
-
- if(t instanceof User){
- t = tm.getSession().get(User.class,id);
- }
- if(t instanceof Role){
- t = tm.getSession().get(Role.class,id);
- }
-
-
- }catch(Exception e){
- throw new DaoException("loadObject:"+e);
- }
- return t;
- }
-
- public boolean insert(TranscationManager tm,Object t) throws DaoException {
- try{
- tm.getSession().save(t);
- }catch(Exception e){
- throw new DaoException("insert:"+e);
- }
- return true;
-
- }
-
- public boolean delete(TranscationManager tm,Object t) throws DaoException {
- try{
- tm.getSession().delete(t);
- }catch(Exception e){
- throw new DaoException("delete:"+e);
- }
- return true;
- }
-
- public boolean update(TranscationManager tm,Object t) throws DaoException {
- try{
- tm.getSession().update(t);
- }catch(Exception e){
- throw new DaoException("update:"+e);
- }
- return true;
- }
-
-
-
- }
BIZ
- package com.text.comm;
-
-
- import com.text.base.entity.User;
- import com.text.base.exception.BIZException;
- import com.text.base.exception.DaoException;
- import com.text.base.util.TranscationManager;
-
-
- public class UserBIZ{
-
-
- public boolean create(String username, String password) throws BIZException{
- DAO dao = new DAO();
- TranscationManager tm = new TranscationManager();
- try{
-
- tm.startOperate(true);
-
-
- User po = new User();
- po.setUsername(username);
- po.setPwdOriginal(password);
- dao.insert(tm, po);
- }catch(DaoException e){
-
- tm.rollbackOperate();
- throw new BIZException("create:"+e);
- }finally{
-
- tm.endOperate();
- }
- return true;
- }
-
- }
Service
- package com.text.comm;
-
- public class Service{
-
- public void execPre(){
-
- String name="haha";
- String pwd="1234";
- UserBIZ biz = new UserBIZ();
- try{
- biz.create(name,pwd);
-
- }catch(Exception e){
- }
- }
- }
本人是新手...没有什么经验...这只是自己的一个想法...不知道行不行....请大家指点...见笑了....
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|
- 温柔一刀
- 等级:
- 性别:
- 文章: 801
- 积分: 1192
- 来自: 上海
|
泛型
|
返回顶楼 |
|
|
- lzmhehe
- 等级: 初级会员
- 性别:
- 文章: 337
- 积分: 22
- 来自: 北京
|
public void save(Object entity)
{
getHibernateTemplate().save(entity);
}
public void update(Object entity)
{
getHibernateTemplate().update(entity);
}
public void delete(Object entity)
{
getHibernateTemplate().delete(entity);
}
public <T> T load(Class<T> entity, Serializable id)
{
return (T)getHibernateTemplate().load(entity, id);
}
|
返回顶楼 |
|
|