引入数据字典,配置一对多的关系
异步加载
(JSON的使用,将list集合封装为json的数据,然后删除部分数据,JSON的数据发送到前端,前端页面获取对应的数据,遍历json的数据)
数据字典(下拉列表)
添加事务、客户查询出来并分页处理
创建分页订单PageBean
CREATE TABLE `cst_customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
`cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
`cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
创建Customer实体
package com.itzheng.crm.domain;
/*
* 客户管理的实体
*/
public class Customer {
private Long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_phone;
private String cust_mobile;
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
public Customer() {
// TODO Auto-generated constructor stub
}
public Customer(Long cust_id, String cust_name, String cust_source, String cust_industry, String cust_level,
String cust_phone, String cust_mobile) {
super();
this.cust_id = cust_id;
this.cust_name = cust_name;
this.cust_source = cust_source;
this.cust_industry = cust_industry;
this.cust_level = cust_level;
this.cust_phone = cust_phone;
this.cust_mobile = cust_mobile;
}
@Override
public String toString() {
return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source
+ ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_phone=" + cust_phone
+ ", cust_mobile=" + cust_mobile + "]";
}
}
创建实体映射在domian下创建Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 建立类与表的映射 -->
<class name="com.itzheng.crm.domain.Customer" table="cst_customer">
<!-- 建立类中的属性与表中的主键对应 -->
<id name="cust_id" column="cust_id">
<!-- 主键生成策略 -->
<generator class="native" />
</id>
<!-- 建立类中的普通的属性和表的字段的对应映射 -->
<property name="cust_name" column="cust_name" />
<property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry" />
<property name="cust_level" column="cust_level" />
<property name="cust_phone" column="cust_phone" />
<property name="cust_mobile" column="cust_mobile" />
</class>
</hibernate-mapping>
package com.itzheng.crm.web.action;
import com.itzheng.crm.domain.Customer;
import com.itzheng.crm.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/*
* 客户管理的Action的类
*/
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
// 模型驱动使用的对象
private Customer customer = new Customer();
@Override
public Customer getModel() {
// TODO Auto-generated method stub
return customer;
}
//注入Service
private CustomerService customerService;
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
}
CustomerService
package com.itzheng.crm.service;
/*
* 客户管理Service的接口
*/
public interface CustomerService {
}
CustomerServiceImpl
package com.itzheng.crm.service.impl;
import com.itzheng.crm.dao.CustomerDao;
import com.itzheng.crm.service.CustomerService;
/*
* 客户管理Service的接口的实现类
*/
public class CustomerServiceImpl implements CustomerService {
//注入客户的DAO
private CustomerDao customerDao;
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
}
CustomerDao
package com.itzheng.crm.dao;
/*
* 客户管理的DAO的接口
*/
public interface CustomerDao {
}
创建CustomerDaoImpl 继承HibernateDaoSupport
package com.itzheng.crm.dao.impl;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.itzheng.crm.dao.CustomerDao;
/*
* 客户管理的DAO的实现类
*/
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
}
在applicationContext.xml当中
<!-- 配置Action -->
<bean id="customerAction" class="com.itzheng.crm.web.action.CustomerAction" scope="prototype">
<property name="customerService" ref="customerService"/>
</bean>
<!-- 配置Service -->
<bean id="customerService" class="com.itzheng.crm.service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"/>
</bean>
<!-- 配置DAO -->
<bean id="customerDao" class="com.itzheng.crm.dao.impl.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
数据字典用来规范某些地方具体值和数据
SQL语句
CREATE TABLE `base_dict` (
`dict_id` varchar(32) NOT NULL COMMENT '数据字典id(主键)',
`dict_type_code` varchar(10) NOT NULL COMMENT '数据字典类别代码',
`dict_type_name` varchar(64) NOT NULL COMMENT '数据字典类别名称',
`dict_item_name` varchar(64) NOT NULL COMMENT '数据字典项目名称',
`dict_item_code` varchar(10) DEFAULT NULL COMMENT '数据字典项目(可为空)',
`dict_sort` int(10) DEFAULT NULL COMMENT '排序字段',
`dict_enable` char(1) NOT NULL COMMENT '1:使用 0:停用',
`dict_memo` varchar(64) DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`dict_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `base_dict` */
LOCK TABLES `base_dict` WRITE;
insert into `base_dict`(`dict_id`,`dict_type_code`,`dict_type_name`,`dict_item_name`,`dict_item_code`,`dict_sort`,`dict_enable`,`dict_memo`) values ('1','001','客户行业','教育培训 ',NULL,1,'1',NULL),('10','003','公司性质','民企',NULL,3,'1',NULL),('12','004','年营业额','1-10万',NULL,1,'1',NULL),('13','004','年营业额','10-20万',NULL,2,'1',NULL),('14','004','年营业额','20-50万',NULL,3,'1',NULL),('15','004','年营业额','50-100万',NULL,4,'1',NULL),('16','004','年营业额','100-500万',NULL,5,'1',NULL),('17','004','年营业额','500-1000万',NULL,6,'1',NULL),('18','005','客户状态','基础客户',NULL,1,'1',NULL),('19','005','客户状态','潜在客户',NULL,2,'1',NULL),('2','001','客户行业','电子商务',NULL,2,'1',NULL),('20','005','客户状态','成功客户',NULL,3,'1',NULL),('21','005','客户状态','无效客户',NULL,4,'1',NULL),('22','006','客户级别','普通客户',NULL,1,'1',NULL),('23','006','客户级别','VIP客户',NULL,2,'1',NULL),('24','007','商机状态','意向客户',NULL,1,'1',NULL),('25','007','商机状态','初步沟通',NULL,2,'1',NULL),('26','007','商机状态','深度沟通',NULL,3,'1',NULL),('27','007','商机状态','签订合同',NULL,4,'1',NULL),('3','001','客户行业','对外贸易',NULL,3,'1',NULL),('30','008','商机类型','新业务',NULL,1,'1',NULL),('31','008','商机类型','现有业务',NULL,2,'1',NULL),('32','009','商机来源','电话营销',NULL,1,'1',NULL),('33','009','商机来源','网络营销',NULL,2,'1',NULL),('34','009','商机来源','推广活动',NULL,3,'1',NULL),('4','001','客户行业','酒店旅游',NULL,4,'1',NULL),('5','001','客户行业','房地产',NULL,5,'1',NULL),('6','002','客户信息来源','电话营销',NULL,1,'1',NULL),('7','002','客户信息来源','网络营销',NULL,2,'1',NULL),('8','003','公司性质','合资',NULL,1,'1',NULL),('9','003','公司性质','国企',NULL,2,'1',NULL);
UNLOCK TABLES;
创建实体
package com.itzheng.crm.domain;
/*
* 数据字典的实体
* CREATE TABLE `base_dict` (
`dict_id` VARCHAR(32) NOT NULL COMMENT '数据字典id(主键)',
`dict_type_code` VARCHAR(10) NOT NULL COMMENT '数据字典类别代码',
`dict_type_name` VARCHAR(64) NOT NULL COMMENT '数据字典类别名称',
`dict_item_name` VARCHAR(64) NOT NULL COMMENT '数据字典项目名称',
`dict_item_code` VARCHAR(10) DEFAULT NULL COMMENT '数据字典项目(可为空)',
`dict_sort` INT(10) DEFAULT NULL COMMENT '排序字段',
`dict_enable` CHAR(1) NOT NULL COMMENT '1:使用 0:停用',
`dict_memo` VARCHAR(64) DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`dict_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
*/
public class BaseDict {
private String dict_id;
private String dict_type_code;
private String dict_type_name;
private String dict_item_name;
private String dict_item_code;
private Integer dict_sort;
private String dict_enable;
private String dict_memo;
public String getDict_id() {
return dict_id;
}
public void setDict_id(String dict_id) {
this.dict_id = dict_id;
}
public String getDict_type_code() {
return dict_type_code;
}
public void setDict_type_code(String dict_type_code) {
this.dict_type_code = dict_type_code;
}
public String getDict_type_name() {
return dict_type_name;
}
public void setDict_type_name(String dict_type_name) {
this.dict_type_name = dict_type_name;
}
public String getDict_item_name() {
return dict_item_name;
}
public void setDict_item_name(String dict_item_name) {
this.dict_item_name = dict_item_name;
}
public String getDict_item_code() {
return dict_item_code;
}
public void setDict_item_code(String dict_item_code) {
this.dict_item_code = dict_item_code;
}
public Integer getDict_sort() {
return dict_sort;
}
public void setDict_sort(Integer dict_sort) {
this.dict_sort = dict_sort;
}
public String getDict_enable() {
return dict_enable;
}
public void setDict_enable(String dict_enable) {
this.dict_enable = dict_enable;
}
public String getDict_memo() {
return dict_memo;
}
public void setDict_memo(String dict_memo) {
this.dict_memo = dict_memo;
}
public BaseDict() {
// TODO Auto-generated constructor stub
}
public BaseDict(String dict_id, String dict_type_code, String dict_type_name, String dict_item_name,
String dict_item_code, Integer dict_sort, String dict_enable, String dict_memo) {
super();
this.dict_id = dict_id;
this.dict_type_code = dict_type_code;
this.dict_type_name = dict_type_name;
this.dict_item_name = dict_item_name;
this.dict_item_code = dict_item_code;
this.dict_sort = dict_sort;
this.dict_enable = dict_enable;
this.dict_memo = dict_memo;
}
@Override
public String toString() {
return "BaseDict [dict_id=" + dict_id + ", dict_type_code=" + dict_type_code + ", dict_type_name="
+ dict_type_name + ", dict_item_name=" + dict_item_name + ", dict_item_code=" + dict_item_code
+ ", dict_sort=" + dict_sort + ", dict_enable=" + dict_enable + ", dict_memo=" + dict_memo + "]";
}
}
创建映射
BaseDict.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 建立类与表的映射 -->
<class name="com.itzheng.crm.domain.BaseDict" table="base_dict">
<!-- 建立类中的属性与表中的主键对应 -->
<id name="dict_id" column="dict_id">
<!-- 主键生成策略 -->
<generator class="uuid" />
</id>
<!-- 建立类中的普通的属性和表的字段的对应映射 -->
<property name="dict_type_code" column="dict_type_code" />
<property name="dict_type_name" column="dict_type_name" />
<property name="dict_item_name" column="dict_item_name" />
<property name="dict_item_code" column="dict_item_code" />
<property name="dict_sort" column="dict_sort" />
<property name="dict_enable" column="dict_enable" />
<property name="dict_memo" column="dict_memo" />
</class>
</hibernate-mapping>
package com.itzheng.crm.domain;
/*
* 客户管理的实体
*/
public class Customer {
private Long cust_id;
private String cust_name;
/*
* private String cust_source; private String cust_industry; private String
* cust_level;
*/
private String cust_phone;
private String cust_mobile;
/*
* 客户和字典表是属于多对一:需要在多的一方放的是一的一方的对象
*/
private BaseDict baseDictSource;
private BaseDict baseDictIndustry;
private BaseDict baseDictLevel;
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
public BaseDict getBaseDictSource() {
return baseDictSource;
}
public void setBaseDictSource(BaseDict baseDictSource) {
this.baseDictSource = baseDictSource;
}
public BaseDict getBaseDictIndustry() {
return baseDictIndustry;
}
public void setBaseDictIndustry(BaseDict baseDictIndustry) {
this.baseDictIndustry = baseDictIndustry;
}
public BaseDict getBaseDictLevel() {
return baseDictLevel;
}
public void setBaseDictLevel(BaseDict baseDictLevel) {
this.baseDictLevel = baseDictLevel;
}
public Customer() {
// TODO Auto-generated constructor stub
}
public Customer(Long cust_id, String cust_name, String cust_phone, String cust_mobile, BaseDict baseDictSource,
BaseDict baseDictIndustry, BaseDict baseDictLevel) {
super();
this.cust_id = cust_id;
this.cust_name = cust_name;
this.cust_phone = cust_phone;
this.cust_mobile = cust_mobile;
this.baseDictSource = baseDictSource;
this.baseDictIndustry = baseDictIndustry;
this.baseDictLevel = baseDictLevel;
}
@Override
public String toString() {
return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_phone=" + cust_phone
+ ", cust_mobile=" + cust_mobile + ", baseDictSource=" + baseDictSource + ", baseDictIndustry="
+ baseDictIndustry + ", baseDictLevel=" + baseDictLevel + "]";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 建立类与表的映射 -->
<class name="com.itzheng.crm.domain.Customer" table="cst_customer">
<!-- 建立类中的属性与表中的主键对应 -->
<id name="cust_id" column="cust_id">
<!-- 主键生成策略 -->
<generator class="native" />
</id>
<!-- 建立类中的普通的属性和表的字段的对应映射 -->
<property name="cust_name" column="cust_name" />
<property name="cust_phone" column="cust_phone" />
<property name="cust_mobile" column="cust_mobile" />
<!-- 配置客户与字典的多对一的映射 -->
<many-to-one name="baseDictSource" class="com.itzheng.crm.domain.BaseDict" column="cust_source"></many-to-one>
<many-to-one name="baseDictIndustry" class="com.itzheng.crm.domain.BaseDict" column="cust_industry"></many-to-one>
<many-to-one name="baseDictLevel" class="com.itzheng.crm.domain.BaseDict" column="cust_level"></many-to-one>
</class>
</hibernate-mapping>
package com.itzheng.crm.dao;
/*
* 字典的Dao的接口
*/
public interface BaseDictDao {
}
package com.itzheng.crm.dao.impl;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.itzheng.crm.dao.BaseDictDao;
/*
* 字典的Dao的接口的实现类
*/
public class BaseDictDaoImpl extends HibernateDaoSupport implements BaseDictDao {
}
package com.itzheng.crm.service;
/*
* 字典的业务层的接口
*/
public interface BaseDictService {
}
package com.itzheng.crm.service.impl;
import com.itzheng.crm.dao.BaseDictDao;
import com.itzheng.crm.service.BaseDictService;
/*
* 字典的业务层的实现类
*/
public class BaseDictServiceImpl implements BaseDictService {
//注入Dao
private BaseDictDao baseDictDao;
public void setBaseDictDao(BaseDictDao baseDictDao) {
this.baseDictDao = baseDictDao;
}
}
package com.itzheng.crm.web.action;
import com.itzheng.crm.domain.BaseDict;
import com.itzheng.crm.service.BaseDictService;
/*
* 字典的Action类
*/
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class BaseDictAction extends ActionSupport implements ModelDriven<BaseDict> {
//模型驱动使用的对象
private BaseDict baseDict = new BaseDict();
@Override
public BaseDict getModel() {
// TODO Auto-generated method stub
return baseDict;
}
//注入Service
private BaseDictService baseDictService;
public void setBaseDictService(BaseDictService baseDictService) {
this.baseDictService = baseDictService;
}
}
/*
* 根据类型名称查询字典的方法:findByTypeCode
*/
public String findByTypeCode() throws IOException {
System.out.println("BaseDictAction中的findByTypeCode方法执行了。。。。");
//调用业务层查询
List<BaseDict> list = baseDictService.findByTypeCode(baseDict.getDict_type_code());
//将list转换成json,----jsonlib/fastjson4
//引入jsonlib的包
/*
* JSONConfig:转JSON的一个配置对象
* JSONArray;将数组和list集合转换为json
* JSONObject:将对象和Map集合转成JSON
*/
JsonConfig jsonconfig = new JsonConfig();
jsonconfig.setExcludes(new String[] {"dict_sort","dict_enable","dict_memo"});//(将list当中的这些内容删除)
JSONArray jsonArray = JSONArray.fromObject(list,jsonconfig);//将list集合转换为JSON
System.out.println(jsonArray.toString());
//将JSON打印到页面上
ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
ServletActionContext.getResponse().getWriter().println(jsonArray.toString());
return NONE;
}
@Override
public List<BaseDict> findByTypeCode(String dict_type_code) {
// TODO Auto-generated method stub
return baseDictDao.findByTypeCode(dict_type_code);
}
package com.itzheng.crm.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.itzheng.crm.dao.BaseDictDao;
import com.itzheng.crm.domain.BaseDict;
public class BaseDictDaoImpl extends HibernateDaoSupport implements BaseDictDao {
@Override
//根据类型编码去查询字典数据
public List<BaseDict> findByTypeCode(String dict_type_code) {
// TODO Auto-generated method stub
List<BaseDict> find = (List<BaseDict>) this.getHibernateTemplate().find("from BaseDict where dict_type_code = ?", dict_type_code);
return find;
}
}
package com.itzheng.crm.service.impl;
import com.itzheng.crm.dao.CustomerDao;
import com.itzheng.crm.domain.Customer;
import com.itzheng.crm.service.CustomerService;
/*
* 客户管理Service的接口的实现类
*/
public class CustomerServiceImpl implements CustomerService {
//注入客户的DAO
private CustomerDao customerDao;
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
//业务层保存客户的方法
@Override
public void save(Customer customer) {
// TODO Auto-generated method stub
customerDao.save(customer);
}
}
package com.itzheng.crm.dao.impl;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.itzheng.crm.dao.CustomerDao;
import com.itzheng.crm.domain.Customer;
/*
* 客户管理的DAO的实现类
*/
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
@Override
public void save(Customer customer) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(customer);
}
}
package com.itzheng.crm.domain;
import java.util.List;
public class PageBean<T> {
private Integer currPage;// 当前页数
private Integer pageSize;// 每页显示的记录数
private Integer totalCount;// 总记录数
private Integer totalPage;// 总页数
private List<T> list;// 每页查询到数据的集合
public Integer getCurrPage() {
return currPage;
}
public void setCurrPage(Integer currPage) {
this.currPage = currPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
/*
* 分页查询客户的方法
*/
public String findAll() {
// 接收参数:分页的参数:
// 最好使用DetachedCriteria对象(条件查询--带分页)
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
// 调用业务层查询:
PageBean<Customer> pageBean = customerService.findByPage(detachedCriteria, currPage, pageSize);
ActionContext.getContext().getValueStack().push(pageBean);//放入到值栈当中
return "findAll";
}
@Override
// 业务层分页查询客户的方法(页码数以及对应的集合)
public PageBean<Customer> findByPage(DetachedCriteria detachedCriteria, Integer currPage, Integer pageSize) {
// TODO Auto-generated method stub
PageBean<Customer> pageBean = new PageBean<Customer>();
// 封装当前的页数
pageBean.setCurrPage(currPage);
// 封装每页显示的记录
pageBean.setPageSize(pageSize);
// 封装总记录数
// 调用Dao
Integer totalCount = customerDao.findCount(detachedCriteria);
pageBean.setTotalCount(totalCount);//这个是总数据条数
// 封装总页数;
Double tc = totalCount.doubleValue();
Double num = Math.ceil(tc/pageSize);// 向上取整(总记录数除以每一页数据的数量等于总页数)
pageBean.setTotalPage(num.intValue());
//封装每一页显示的数据的集合
Integer begin = (currPage - 1) * pageSize;//计算要从几开始查询数据库当中数据
List<Customer> list = customerDao.findByPage(detachedCriteria,begin,pageSize);
pageBean.setList(list);
return pageBean;
}
// DAO 当中带条件的统计个数的方法
@Override
public Integer findCount(DetachedCriteria detachedCriteria) {
// TODO Auto-generated method stub
// select count(*) from xxx where 条件;
DetachedCriteria setProjection = detachedCriteria.setProjection(Projections.rowCount());// 设置条件获取个数
List<Long> list = (List<Long>) this.getHibernateTemplate().findByCriteria(setProjection);
if (list.size() > 0) {
return list.get(0).intValue();
}
return null;
}
@Override
// DAO 当中分页查询客户的方法
public List<Customer> findByPage(DetachedCriteria detachedCriteria, Integer begin, Integer pageSize) {
// TODO Auto-generated method stub
detachedCriteria.setProjection(null);// 情况detachedCriteria当中的查询方法
return (List<Customer>) this.getHibernateTemplate().findByCriteria(detachedCriteria, begin, pageSize);
}