CREATE TABLE `cst_linkman` (
`lkm_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '联系人编号(主键)',
`lkm_name` varchar(16) DEFAULT NULL COMMENT '联系人姓名',
`lkm_cust_id` bigint(32) NOT NULL COMMENT '客户id',
`lkm_gender` char(1) DEFAULT NULL COMMENT '联系人性别',
`lkm_phone` varchar(16) DEFAULT NULL COMMENT '联系人办公电话',
`lkm_mobile` varchar(16) DEFAULT NULL COMMENT '联系人手机',
`lkm_email` varchar(64) DEFAULT NULL COMMENT '联系人邮箱',
`lkm_qq` varchar(16) DEFAULT NULL COMMENT '联系人qq',
`lkm_position` varchar(16) DEFAULT NULL COMMENT '联系人职位',
`lkm_memo` varchar(512) DEFAULT NULL COMMENT '联系人备注',
PRIMARY KEY (`lkm_id`),
KEY `FK_cst_linkman_lkm_cust_id` (`lkm_cust_id`),
CONSTRAINT `FK_cst_linkman_lkm_cust_id` FOREIGN KEY (`lkm_cust_id`) REFERENCES `cst_customer` (`cust_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
package com.itzheng.crm.domain;
/*
* 联系人的实体
* CREATE TABLE `cst_linkman` (
`lkm_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '联系人编号(主键)',
`lkm_name` VARCHAR(16) DEFAULT NULL COMMENT '联系人姓名',
`lkm_cust_id` BIGINT(32) NOT NULL COMMENT '客户id',
`lkm_gender` CHAR(1) DEFAULT NULL COMMENT '联系人性别',
`lkm_phone` VARCHAR(16) DEFAULT NULL COMMENT '联系人办公电话',
`lkm_mobile` VARCHAR(16) DEFAULT NULL COMMENT '联系人手机',
`lkm_email` VARCHAR(64) DEFAULT NULL COMMENT '联系人邮箱',
`lkm_qq` VARCHAR(16) DEFAULT NULL COMMENT '联系人qq',
`lkm_position` VARCHAR(16) DEFAULT NULL COMMENT '联系人职位',
`lkm_memo` VARCHAR(512) DEFAULT NULL COMMENT '联系人备注',
PRIMARY KEY (`lkm_id`),
KEY `FK_cst_linkman_lkm_cust_id` (`lkm_cust_id`),
CONSTRAINT `FK_cst_linkman_lkm_cust_id` FOREIGN KEY (`lkm_cust_id`) REFERENCES `cst_customer` (`cust_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=INNODB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
*/
public class LinkMan {
private Long lkm_id;
private String lkm_name;
// 外键要编程映射
private String lkm_gender;
private String lkm_phone;
private String lkm_mobile;
private String lkm_email;
private String lkm_qq;
private String lkm_position;
private String lkm_memo;
// 客户是一联系人是多
// 联系人这一方放置客户的对象
private Customer customer;
public Long getLkm_id() {
return lkm_id;
}
public void setLkm_id(Long lkm_id) {
this.lkm_id = lkm_id;
}
public String getLkm_name() {
return lkm_name;
}
public void setLkm_name(String lkm_name) {
this.lkm_name = lkm_name;
}
public String getLkm_gender() {
return lkm_gender;
}
public void setLkm_gender(String lkm_gender) {
this.lkm_gender = lkm_gender;
}
public String getLkm_phone() {
return lkm_phone;
}
public void setLkm_phone(String lkm_phone) {
this.lkm_phone = lkm_phone;
}
public String getLkm_mobile() {
return lkm_mobile;
}
public void setLkm_mobile(String lkm_mobile) {
this.lkm_mobile = lkm_mobile;
}
public String getLkm_email() {
return lkm_email;
}
public void setLkm_email(String lkm_email) {
this.lkm_email = lkm_email;
}
public String getLkm_qq() {
return lkm_qq;
}
public void setLkm_qq(String lkm_qq) {
this.lkm_qq = lkm_qq;
}
public String getLkm_position() {
return lkm_position;
}
public void setLkm_position(String lkm_position) {
this.lkm_position = lkm_position;
}
public String getLkm_memo() {
return lkm_memo;
}
public void setLkm_memo(String lkm_memo) {
this.lkm_memo = lkm_memo;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public LinkMan() {
// TODO Auto-generated constructor stub
}
public LinkMan(Long lkm_id, String lkm_name, String lkm_gender, String lkm_phone, String lkm_mobile,
String lkm_email, String lkm_qq, String lkm_position, String lkm_memo, Customer customer) {
super();
this.lkm_id = lkm_id;
this.lkm_name = lkm_name;
this.lkm_gender = lkm_gender;
this.lkm_phone = lkm_phone;
this.lkm_mobile = lkm_mobile;
this.lkm_email = lkm_email;
this.lkm_qq = lkm_qq;
this.lkm_position = lkm_position;
this.lkm_memo = lkm_memo;
this.customer = customer;
}
@Override
public String toString() {
return "LinkMan [lkm_id=" + lkm_id + ", lkm_name=" + lkm_name + ", lkm_gender=" + lkm_gender + ", lkm_phone="
+ lkm_phone + ", lkm_mobile=" + lkm_mobile + ", lkm_email=" + lkm_email + ", lkm_qq=" + lkm_qq
+ ", lkm_position=" + lkm_position + ", lkm_memo=" + lkm_memo + ", customer=" + customer + "]";
}
}
<?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.LinkMan" table="cst_linkman">
<!-- 建立类中的属性与表中的主键对应 -->
<id name="lkm_id" column="lkm_id">
<!-- 主键生成策略 -->
<generator class="native" />
</id>
<!-- 建立类中的普通的属性和表的字段的对应映射 -->
<property name="lkm_name" column="lkm_name" />
<property name="lkm_gender" column="lkm_gender" />
<property name="lkm_phone" column="lkm_phone" />
<property name="lkm_mobile" column="lkm_mobile" />
<property name="lkm_email" column="lkm_email" />
<property name="lkm_qq" column="lkm_qq" />
<property name="lkm_position" column="lkm_position" />
<property name="lkm_memo" column="lkm_memo" />
<!-- 配置客户与字典的多对一的映射 -->
<many-to-one name="customer" class="com.itzheng.crm.domain.Customer" column="lkm_cust_id"></many-to-one>
</class>
</hibernate-mapping>
package com.itzheng.crm.dao;
/*
* 联系人的DAO接口
*/
public interface LinkManDao {
}
继承HibernateDaoSupport
package com.itzheng.crm.dao.impl;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.itzheng.crm.dao.LinkManDao;
/*
* 联系人的DAO的实现类
*/
public class LinkManDaoImpl extends HibernateDaoSupport implements LinkManDao {
}
package com.itzheng.crm.service;
public interface LinkManService {
}
注入Dao
package com.itzheng.crm.service.impl;
import com.itzheng.crm.dao.LinkManDao;
import com.itzheng.crm.service.LinkManService;
public class LinkManServiceImpl implements LinkManService {
private LinkManDao linkManDao;
public LinkManDao setLinkManDao() {
return linkManDao;
}
}
package com.itzheng.crm.web.action;
import com.itzheng.crm.domain.LinkMan;
import com.itzheng.crm.service.LinkManService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/*
* 联系人的Action的类
*/
public class LinkManAction extends ActionSupport implements ModelDriven<LinkMan> {
// 模型驱动使用的
private LinkMan linkMan = new LinkMan();
@Override
public LinkMan getModel() {
// TODO Auto-generated method stub
return linkMan;
}
// 输入Service
private LinkManService linkManService;
public void setLinkManService(LinkManService linkManService) {
this.linkManService = linkManService;
}
}
设置分页参数
编写findAll方法
package com.itzheng.crm.web.action;
import org.hibernate.criterion.DetachedCriteria;
import com.itzheng.crm.domain.LinkMan;
import com.itzheng.crm.domain.PageBean;
import com.itzheng.crm.service.LinkManService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/*
* 联系人的Action的类
*/
public class LinkManAction extends ActionSupport implements ModelDriven<LinkMan> {
// 模型驱动使用的
private LinkMan linkMan = new LinkMan();
@Override
public LinkMan getModel() {
// TODO Auto-generated method stub
return linkMan;
}
// 输入Service
private LinkManService linkManService;
public void setLinkManService(LinkManService linkManService) {
this.linkManService = linkManService;
}
// 分页参数
// 使用set方法的属性驱动的方式来接收数据
private Integer currPage = 1;
public void setCurrPage(Integer currPage) {
if (currPage == null) {
currPage = 1;
}
this.currPage = currPage;
}
// 使用set方法接受每页显示的记录数
private Integer pageSize = 3;
public void setPageSize(Integer pageSize) {
if (pageSize == null) {
pageSize = 3;
}
this.pageSize = pageSize;
}
/*
* 查询联系人列表的Action
*/
public String findAll() {
// 创建离线条件查询
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(LinkMan.class);
// 设置条件
// 调用业务层
PageBean<LinkMan> pageBean = linkManService.findAll(detachedCriteria, currPage, pageSize);
ActionContext.getContext().getValueStack().push(pageBean);
return "findAll";
}
}
在LinkManServiceImpl 编写findAll的方法
package com.itzheng.crm.service.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import com.itzheng.crm.dao.LinkManDao;
import com.itzheng.crm.domain.LinkMan;
import com.itzheng.crm.domain.PageBean;
import com.itzheng.crm.service.LinkManService;
public class LinkManServiceImpl implements LinkManService {
private LinkManDao linkManDao;
public void setLinkManDao(LinkManDao linkManDao) {
this.linkManDao = linkManDao;
}
@Override
//业务层来分页查询联系人的方法
public PageBean<LinkMan> findAll(DetachedCriteria detachedCriteria, Integer currPage, Integer pageSize) {
PageBean<LinkMan> pageBean = new PageBean<LinkMan>();
//设置当前页数
pageBean.setCurrPage(currPage);
//设置每页显示的记录数
pageBean.setPageSize(pageSize);
//设置总记录数
Integer totalCount = linkManDao.findCount(detachedCriteria);
//设置总页数
pageBean.setTotalCount(totalCount);
double tc = totalCount;
Double num = Math.ceil(tc / pageSize);
pageBean.setTotalPage(num.intValue());
//以及每页显示的数据的集合
Integer begin = (currPage - 1) * pageSize;
List<LinkMan> list = linkManDao.findByPage(detachedCriteria,begin,pageSize);
pageBean.setList(list);
return pageBean;
}
}
LinkManDaoImpl
package com.itzheng.crm.dao.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.itzheng.crm.dao.LinkManDao;
import com.itzheng.crm.domain.LinkMan;
/*
* 联系人的DAO的实现类
*/
public class LinkManDaoImpl extends HibernateDaoSupport implements LinkManDao {
@Override
// DAO 当中统计个数的方法
public Integer findCount(DetachedCriteria detachedCriteria) {
// TODO Auto-generated method stub
detachedCriteria.setProjection(Projections.rowCount());// 带条件的查询
List<Long> list = (List<Long>) this.getHibernateTemplate().findByCriteria(detachedCriteria);
if (list.size() > 0) {
return list.get(0).intValue();
}
return null;
}
@Override
// DAO 的分页查询
public List<LinkMan> findByPage(DetachedCriteria detachedCriteria, Integer begin, Integer pageSize) {
// TODO Auto-generated method stub
// 情况count语句
detachedCriteria.setProjection(null);
// 等到list集合:带条件的查询
List<LinkMan> list = (List<LinkMan>) this.getHibernateTemplate().findByCriteria(detachedCriteria, begin,
pageSize);
return list;
}
}