• java项目常用 BaseDao BaseService


    java项目常用 BaseDao BaseService

    IBaseDao

    复制代码
     1 package com.glht.sim.dao;
    2
    3  import java.util.List;
    4
    5
    6  public interface IBaseDao<T> {
    7 T get(long id);
    8 void create(T t);
    9 void delete(T t);
    10 void update(T t);
    11 int getTotalCount();
    12 List<T>getPage(int startIndex,int count);
    13 List<T> getAll();
    14 }
    复制代码

    BasoDao

    复制代码
     1 package com.glht.sim.dao.impl;
    2
    3 import java.lang.reflect.ParameterizedType;
    4 import java.sql.SQLException;
    5 import java.util.List;
    6
    7 import org.hibernate.HibernateException;
    8 import org.hibernate.Session;
    9 import org.springframework.orm.hibernate3.HibernateCallback;
    10 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    11
    12 import com.glht.sim.dao.IBaseDao;
    13
    14 public abstract class BaseDao<T> extends HibernateDaoSupport implements IBaseDao<T> {
    15 protected Class<T> entityClass;
    16 protected String className;
    17 public BaseDao(){
    18 entityClass=(Class<T>)((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    19 className=entityClass.getName();
    20 }
    21 public T get(long id){
    22 return (T)this.getHibernateTemplate().get(entityClass, id);
    23 }
    24 public void create(T t){
    25 this.getHibernateTemplate().save(t);
    26 }
    27 public void delete(T t){
    28 this.getHibernateTemplate().delete(t);
    29 }
    30 public void update(T t){
    31 this.getHibernateTemplate().update(t);
    32 }
    33
    34 public int getTotalCount(){
    35
    36 Object obj=this.getHibernateTemplate().execute(new HibernateCallback(){
    37 public Object doInHibernate(Session session)
    38 throws HibernateException, SQLException{
    39
    40 return session.createQuery("select count(id) from "+className).uniqueResult();
    41 }
    42 });
    43 return (int)((Long)obj).longValue();
    44 }
    45 public List<T>getPage(int startIndex,int count) {
    46
    47 return (List<T>)this.getHibernateTemplate().executeFind(
    48 new PageHibernateCallback(
    49 "from "+className+" as c order by c.id desc",startIndex,count));
    50 }
    51
    52 public List<T> getAll(){
    53 return (List<T>)this.getHibernateTemplate().find("from "+className+" as c order by c.id desc");
    54 }
    55 }
    复制代码

    IBaseService
    复制代码
     1 package com.glht.sim.service;
    2
    3 import java.util.List;
    4
    5
    6 public interface IBaseService<T> {
    7 T get(long id);
    8 void create(T obj);
    9
    10 void delete(T obj);
    11 void update(T obj);
    12 int getTotalCount();
    13 List<T> getPage(int startIndex,int count);
    14 List<T> getAll();
    15 }
    复制代码
    BaseService
    复制代码
     1 package com.glht.sim.service.impl;
    2
    3 import java.util.List;
    4
    5 import com.glht.sim.dao.IBaseDao;
    6 import com.glht.sim.service.IBaseService;
    7
    8 public abstract class BaseService<T> implements IBaseService<T> {
    9 protected IBaseDao<T> dao;
    10
    11 public T get(long id){
    12 return dao.get(id);
    13 }
    14
    15 public void create(T obj){
    16 dao.create(obj);
    17 }
    18
    19 public void delete(T obj){
    20 dao.delete(obj);
    21 }
    22 public void update(T obj){
    23 dao.update(obj);
    24 }
    25
    26 public int getTotalCount(){
    27 return dao.getTotalCount();
    28 }
    29
    30 public List<T> getPage(int startIndex,int count){
    31 return dao.getPage(startIndex,count);
    32 }
    33
    34 public List<T> getAll(){
    35 return dao.getAll();
    36 }
    37
    38 public void setDao(IBaseDao<T> dao) {
    39 this.dao = dao;
    40 }
    41
    42 }
    复制代码
  • 相关阅读:
    JZOJ 3845. 简单题(simple)
    JZOJ 3844. 统计损失(count)
    JZOJ 3843. 寻找羔羊(agnus)
    JZOJ 3833. 平坦的折线
    JZOJ 1956. 矩形
    JZOJ 3832. 在哪里建酿酒厂
    mysql 语法一 :case when详解
    阿里云推荐码
    redis配置文件详解(转)
    压力测试工具 webbench总结
  • 原文地址:https://www.cnblogs.com/iteakey/p/4140242.html
Copyright © 2020-2023  润新知