一:客户Customer
二:联系人Linkman
package com.feizhu.domain;
import java.util.HashSet;
import java.util.Set;
/**
* 客户
* 一方
* @author Administrator
*
*/
public class Customer {
// `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
// `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
// `cust_user_id` bigint(32) DEFAULT NULL COMMENT '负责人id',
// `cust_create_id` bigint(32) DEFAULT NULL COMMENT '创建人id',
// `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
// `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
// `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
// `cust_linkman` varchar(64) DEFAULT NULL COMMENT '联系人',
// `cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
// `cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
//
private Long cust_id;
private String cust_name;
private Long cust_user_id;
private Long cust_create_id;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
//Hibernate框架默认是set集合 这个集合必须手动初始化
private Set<Linkman> linkmans=new HashSet<Linkman>();
public Long getCust_id() {
return cust_id;
}
public Set<Linkman> getLinkmans() {
return linkmans;
}
public void setLinkmans(Set<Linkman> linkmans) {
this.linkmans = linkmans;
}
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 Long getCust_user_id() {
return cust_user_id;
}
public void setCust_user_id(Long cust_user_id) {
this.cust_user_id = cust_user_id;
}
public Long getCust_create_id() {
return cust_create_id;
}
public void setCust_create_id(Long cust_create_id) {
this.cust_create_id = cust_create_id;
}
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_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
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;
}
}
------------------
<?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.feizhu.domain.Customer" table="cst_customer">
<!-- 配置id
见到name属性,javaBean的属性
见到column属性,是表结构的字段
-->
<id name="cust_id" column="cust_id">
<!-- 主键的生成策略 -->
<generator class="native"></generator>
</id>
<!-- 配置其他属性 -->
<property name="cust_name" column="cust_name"></property>
<property name="cust_user_id" column="cust_user_id"></property>
<property name="cust_create_id" column="cust_create_id"></property>
<property name="cust_source" column="cust_source"></property>
<property name="cust_industry" column="cust_industry"></property>
<property name="cust_level" column="cust_level"></property>
<property name="cust_linkman" column="cust_linkman"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
<!-- 配置一方 -->
<!--
set标签name属性:表示集合的名称
-->
<set name="linkmans">
<!-- 需要出现子标签 -->
<!-- 外键的字段 -->
<key column="lkm_cust_id"></key>
<one-to-many class="com.feizhu.domain.Linkman"/>
</set>
</class>
</hibernate-mapping>
---------------
}
-----------------
核心配置文件:
---------------
create database hbn2;
use hbn2;
客户表:
CREATE TABLE `cst_customer` () ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
联系人表:
EATE 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.feizhu.servlet;
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import com.feizhu.domain.Linkman;
import com.feizhu.service.AddLinkManService;
/**
* 新增联系人
*/
public class AddLinkManServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置编码
request.setCharacterEncoding("utf-8");
//接受参数
Map<String, String[]> map = request.getParameterMap();
//获取id
String scust_id =map.get("cust_id")[0];
//类型转换
Long cust_id=Long.parseLong(scust_id);
//创建对象
Linkman man = new Linkman();
//封装数据
try {
BeanUtils.populate(man, map);
//调用业务层
new AddLinkManService().addLinkMan(man,cust_id);
System.out.println("保存联系人成功");
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package com.feizhu.service;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.feizhu.dao.AddLinkManDao;
import com.feizhu.domain.Customer;
import com.feizhu.domain.Linkman;
import com.feizhu.utils.HibernateUtil;
public class AddLinkManService {
/**
* 编写业务,保存联系人 先把客户查出来,给联系人设置客户,保存联系人
*
* @param man
* @param cust_id
*/
public void addLinkMan(Linkman man, Long cust_id) {
// 获取session
Session session = HibernateUtil.getCurrentSession();
// 开启事物
Transaction tr = session.beginTransaction();
try {
// 调用dao层 根据id查询客户
Customer customer = new AddLinkManDao().findById(cust_id);
//设置
man.setCustomer(customer);
//保存联系人
new AddLinkManDao().save(man);
// 提交事物
tr.commit();
} catch (Exception e) {
e.printStackTrace();
//出现异常 回滚事物
tr.rollback();
}
}
}
----------------------------