hibernate框架作业

实现代码:

hibernate.cfg.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  5.       
  6. <hibernate-configuration>  
  7.       
  8.     <!-- 记住:先配置SessionFactory标签,一个数据库对应一个SessionFactory标签 -->  
  9.     <session-factory>  
  10.           
  11.         <!-- 必须要配置的参数有5个,4大参数,数据库的方言 -->  
  12.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
  13.         <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>  
  14.         <property name="hibernate.connection.username">root</property>  
  15.         <property name="hibernate.connection.password">123</property>  
  16.           
  17.         <!-- 数据库的方言 -->  
  18.         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
  19.           
  20.         <!-- 可选配置 -->  
  21.         <!-- 显示SQL语句,在控制台显示 -->  
  22.         <property name="hibernate.show_sql">true</property>  
  23.         <!-- 格式化SQL语句 -->  
  24.         <property name="hibernate.format_sql">true</property>  
  25.         <!-- 生成数据库的表结构   
  26.             update:如果没有表结构,创建表结构。如果存在,不会创建,添加数据  
  27.         -->  
  28.         <property name="hibernate.hbm2ddl.auto">update</property>  
  29.           
  30.         <property name="hibernate.current_session_context_class">thread</property>  
  31.           
  32.         <!-- 映射配置文件,需要引入映射的配置文件 -->  
  33.         <mapping resource="com/oracle/domain/Customer.hbm.xml"/>  
  34.         <mapping resource="com/oracle/domain/Linkman.hbm.xml"/>  
  35.           
  36.     </session-factory>  
  37.       
  38. </hibernate-configuration>      

Customer.java

[java]  view plain  copy
  1. package com.oracle.domain;  
  2.   
  3. import java.util.HashSet;  
  4. import java.util.Set;  
  5.   
  6. public class Customer {  
  7.     private Long cust_id;  
  8.     private String cust_name;  
  9.     private Long cust_user_id;  
  10.     private Long cust_create_id;  
  11.     private String cust_source;  
  12.     private String cust_industry;  
  13.     private String cust_level;  
  14.     private String cust_linkman;  
  15.     private String cust_phone;  
  16.     private String cust_mobile;  
  17.       
  18.     private Set<Linkman> linkmans = new HashSet<Linkman>();  
  19.       
  20.     public Long getCust_id() {  
  21.         return cust_id;  
  22.     }  
  23.     public void setCust_id(Long cust_id) {  
  24.         this.cust_id = cust_id;  
  25.     }  
  26.     public String getCust_name() {  
  27.         return cust_name;  
  28.     }  
  29.     public void setCust_name(String cust_name) {  
  30.         this.cust_name = cust_name;  
  31.     }  
  32.     public Long getCust_user_id() {  
  33.         return cust_user_id;  
  34.     }  
  35.     public void setCust_user_id(Long cust_user_id) {  
  36.         this.cust_user_id = cust_user_id;  
  37.     }  
  38.     public Long getCust_create_id() {  
  39.         return cust_create_id;  
  40.     }  
  41.     public void setCust_create_id(Long cust_create_id) {  
  42.         this.cust_create_id = cust_create_id;  
  43.     }  
  44.     public String getCust_source() {  
  45.         return cust_source;  
  46.     }  
  47.     public void setCust_source(String cust_source) {  
  48.         this.cust_source = cust_source;  
  49.     }  
  50.     public String getCust_industry() {  
  51.         return cust_industry;  
  52.     }  
  53.     public void setCust_industry(String cust_industry) {  
  54.         this.cust_industry = cust_industry;  
  55.     }  
  56.     public String getCust_level() {  
  57.         return cust_level;  
  58.     }  
  59.     public void setCust_level(String cust_level) {  
  60.         this.cust_level = cust_level;  
  61.     }  
  62.     public String getCust_linkman() {  
  63.         return cust_linkman;  
  64.     }  
  65.     public void setCust_linkman(String cust_linkman) {  
  66.         this.cust_linkman = cust_linkman;  
  67.     }  
  68.     public String getCust_phone() {  
  69.         return cust_phone;  
  70.     }  
  71.     public void setCust_phone(String cust_phone) {  
  72.         this.cust_phone = cust_phone;  
  73.     }  
  74.     public String getCust_mobile() {  
  75.         return cust_mobile;  
  76.     }  
  77.     public void setCust_mobile(String cust_mobile) {  
  78.         this.cust_mobile = cust_mobile;  
  79.     }  
  80.     public Set<Linkman> getLinkmans() {  
  81.         return linkmans;  
  82.     }  
  83.     public void setLinkmans(Set<Linkman> linkmans) {  
  84.         this.linkmans = linkmans;  
  85.     }  
  86.       
  87.     @Override  
  88.     public String toString() {  
  89.         return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_user_id=" + cust_user_id  
  90.                 + ", cust_create_id=" + cust_create_id + ", cust_source=" + cust_source + ", cust_industry="  
  91.                 + cust_industry + ", cust_level=" + cust_level + ", cust_linkman=" + cust_linkman + ", cust_phone="  
  92.                 + cust_phone + ", cust_mobile=" + cust_mobile + "]";  
  93.     }  
  94.       
  95. }  

Customer.hbm.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC   
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping>  
  6.     <class name="com.oracle.domain.Customer" table="cst_customer">  
  7.         <id name="cust_id" column="cust_id">  
  8.             <generator class="native"></generator>  
  9.         </id>  
  10.           
  11.         <property name="cust_name" column="cust_name"></property>  
  12.         <property name="cust_user_id" column="cust_user_id"/>  
  13.         <property name="cust_create_id" column="cust_create_id"/>  
  14.         <property name="cust_source" column="cust_source"/>  
  15.         <property name="cust_industry" column="cust_industry"/>  
  16.         <property name="cust_level" column="cust_level"/>  
  17.         <property name="cust_linkman" column="cust_linkman"/>  
  18.         <property name="cust_phone" column="cust_phone"/>  
  19.         <property name="cust_mobile" column="cust_mobile"/>  
  20.           
  21.         <set name="linkmans" cascade="save-update">  
  22.             <key column="lkm_cust_id"></key>  
  23.             <one-to-many class="com.oracle.domain.Linkman" />  
  24.           
  25.         </set>  
  26.     </class>  
  27. </hibernate-mapping>  

Linkman.java

[java]  view plain  copy
  1. package com.oracle.domain;  
  2.   
  3. public class Linkman {  
  4.       
  5.     private Long lkm_id;  
  6.     private String lkm_name;  
  7.     private String lkm_gender;  
  8.     private String lkm_phone;  
  9.     private String lkm_mobile;  
  10.     private String lkm_email;  
  11.     private String lkm_qq;  
  12.     private String lkm_position;  
  13.     private String lkm_memo;  
  14.       
  15.     private Customer customer;  
  16.       
  17.     public Long getLkm_id() {  
  18.         return lkm_id;  
  19.     }  
  20.     public void setLkm_id(Long lkm_id) {  
  21.         this.lkm_id = lkm_id;  
  22.     }  
  23.     public String getLkm_name() {  
  24.         return lkm_name;  
  25.     }  
  26.     public void setLkm_name(String lkm_name) {  
  27.         this.lkm_name = lkm_name;  
  28.     }  
  29.     public String getLkm_gender() {  
  30.         return lkm_gender;  
  31.     }  
  32.     public void setLkm_gender(String lkm_gender) {  
  33.         this.lkm_gender = lkm_gender;  
  34.     }  
  35.     public String getLkm_phone() {  
  36.         return lkm_phone;  
  37.     }  
  38.     public void setLkm_phone(String lkm_phone) {  
  39.         this.lkm_phone = lkm_phone;  
  40.     }  
  41.     public String getLkm_mobile() {  
  42.         return lkm_mobile;  
  43.     }  
  44.     public void setLkm_mobile(String lkm_mobile) {  
  45.         this.lkm_mobile = lkm_mobile;  
  46.     }  
  47.     public String getLkm_email() {  
  48.         return lkm_email;  
  49.     }  
  50.     public void setLkm_email(String lkm_email) {  
  51.         this.lkm_email = lkm_email;  
  52.     }  
  53.     public String getLkm_qq() {  
  54.         return lkm_qq;  
  55.     }  
  56.     public void setLkm_qq(String lkm_qq) {  
  57.         this.lkm_qq = lkm_qq;  
  58.     }  
  59.     public String getLkm_position() {  
  60.         return lkm_position;  
  61.     }  
  62.     public void setLkm_position(String lkm_position) {  
  63.         this.lkm_position = lkm_position;  
  64.     }  
  65.     public String getLkm_memo() {  
  66.         return lkm_memo;  
  67.     }  
  68.     public void setLkm_memo(String lkm_memo) {  
  69.         this.lkm_memo = lkm_memo;  
  70.     }  
  71.       
  72.     public Customer getCustomer() {  
  73.         return customer;  
  74.     }  
  75.     public void setCustomer(Customer customer) {  
  76.         this.customer = customer;  
  77.     }  
  78.       
  79.     @Override  
  80.     public String toString() {  
  81.           
  82.         return "Linkman [lkm_id=" + lkm_id + ", lkm_name=" + lkm_name + ", lkm_gender=" + lkm_gender + ", lkm_phone="  
  83.                 + lkm_phone + ", lkm_mobile=" + lkm_mobile + ", lkm_email=" + lkm_email + ", lkm_qq=" + lkm_qq  
  84.                 + ", lkm_position=" + lkm_position + ", lkm_memo=" + lkm_memo + ", customer=" + customer + "]";  
  85.     }  
  86. }  

Linkman.hbm.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC   
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  5.     <hibernate-mapping>  
  6.         <class name="com.oracle.domain.Linkman" table="cst_linkman">  
  7.             <id name="lkm_id" column="lkm_id">  
  8.             <generator class="native"></generator>  
  9.             </id>  
  10.               
  11.             <property name="lkm_name" column="lkm_name"/>  
  12.             <property name="lkm_gender" column="lkm_gender"/>  
  13.             <property name="lkm_phone" column="lkm_phone"/>  
  14.             <property name="lkm_mobile" column="lkm_mobile"/>  
  15.             <property name="lkm_email" column="lkm_email"/>  
  16.             <property name="lkm_qq" column="lkm_qq"/>  
  17.             <property name="lkm_position" column="lkm_position"/>  
  18.             <property name="lkm_memo" column="lkm_memo"/>  
  19.           
  20.             <many-to-one name="customer" class="com.oracle.domain.Customer" column="lkm_cust_id" cascade="all"></many-to-one>  
  21.           
  22.         </class>  
  23.       
  24.     </hibernate-mapping>  

CusDao.java

[java]  view plain  copy
  1. package com.oracle.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.hibernate.Criteria;  
  6. import org.hibernate.Session;  
  7. import org.hibernate.Transaction;  
  8. import org.hibernate.criterion.Restrictions;  
  9.   
  10. import com.oracle.domain.Customer;  
  11. import com.oracle.utils.HibernateUtils;  
  12.   
  13. public class CusDao {  
  14.   
  15.     public void addCustomer(Customer c) {  
  16.         Session session = HibernateUtils.getSession();  
  17.           
  18.         Transaction tr = session.beginTransaction();  
  19.           
  20.         session.save(c);  
  21.           
  22.         tr.commit();  
  23.           
  24.         session.close();  
  25.     }  
  26.       
  27.     public List<Customer> findAll(){  
  28.         // QBC查询  
  29.         Session session = HibernateUtils.getSession();  
  30.         Transaction tr = session.beginTransaction();  
  31.         // 查询  
  32.         Criteria criteria = session.createCriteria(Customer.class);  
  33.         // 查询  
  34.         List<Customer> list = criteria.list();  
  35.         tr.commit();  
  36.         session.close();  
  37.         return list;  
  38.     }  
  39.       
  40.     public List<Customer> findCus(String custname) {  
  41.         Session session = HibernateUtils.getSession();  
  42.         Transaction tr = session.beginTransaction();  
  43.           
  44.         Criteria criteria = session.createCriteria(Customer.class);  
  45.           
  46.         if(custname!=null&&!custname.trim().isEmpty()){  
  47.             criteria.add(Restrictions.like("cust_name""%"+custname+"%"));  
  48.         }  
  49.         List list = criteria.list();  
  50.           
  51.         tr.commit();  
  52.         session.close();  
  53.         return list;  
  54.     }  
  55.   
  56.     public void editCus(Customer customer) {  
  57.         Session session = HibernateUtils.getSession();  
  58.           
  59.         Transaction tr = session.beginTransaction();  
  60.           
  61.         session.update(customer);  
  62.           
  63.         tr.commit();  
  64.           
  65.         session.close();  
  66.     }  
  67.   
  68.     public void deleteCus(Long cust_id) {  
  69.         Session session = HibernateUtils.getSession();  
  70.         Transaction tr = session.beginTransaction();  
  71.           
  72.         session.delete(session.get(Customer.class, cust_id));  
  73.           
  74.         tr.commit();  
  75.         session.close();  
  76.     }  
  77.   
  78.     public Customer getCusById(long cust_id) {  
  79.         Session session = HibernateUtils.getSession();  
  80.         Transaction tr = session.beginTransaction();  
  81.           
  82.         Customer customer = session.get(Customer.class, cust_id);  
  83.           
  84.         tr.commit();  
  85.         session.close();  
  86.         return customer;  
  87.     }  
  88.   
  89. }  

LinkmanDao.java

[java]  view plain  copy
  1. package com.oracle.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.hibernate.Criteria;  
  6. import org.hibernate.Session;  
  7. import org.hibernate.Transaction;  
  8. import org.hibernate.criterion.Restrictions;  
  9.   
  10. import com.oracle.domain.Linkman;  
  11. import com.oracle.utils.HibernateUtils;  
  12.   
  13. public class LinkmanDao {  
  14.   
  15.     public List<Linkman> findAll() {  
  16.         Session session = HibernateUtils.getSession();  
  17.         Transaction tr = session.beginTransaction();  
  18.           
  19.         Criteria criteria = session.createCriteria(Linkman.class);  
  20.         List list = criteria.list();  
  21.           
  22.         tr.commit();  
  23.         session.close();  
  24.         return list;  
  25.     }  
  26.   
  27.     public List<Linkman> findByName(String lkm_name) {  
  28.         Session session = HibernateUtils.getSession();  
  29.         Transaction tr = session.beginTransaction();  
  30.           
  31.         Criteria criteria = session.createCriteria(Linkman.class);  
  32.           
  33.         if(lkm_name!=null&&!lkm_name.trim().isEmpty()){  
  34.             criteria.add(Restrictions.like("lkm_name""%"+lkm_name+"%"));  
  35.         }  
  36.         List list = criteria.list();  
  37.           
  38.         tr.commit();  
  39.         session.close();  
  40.         return list;  
  41.     }  
  42.   
  43.     public void add(Linkman linkman) {  
  44.         Session session = HibernateUtils.getSession();  
  45.         Transaction tr = session.beginTransaction();  
  46.         session.save(linkman);  
  47.           
  48.         tr.commit();  
  49.           
  50.         session.close();  
  51.     }  
  52.   
  53.     public void delete(long lkm_id) {  
  54.         Session session = HibernateUtils.getSession();  
  55.         Transaction tr = session.beginTransaction();  
  56.           
  57.         session.delete(session.get(Linkman.class, lkm_id));  
  58.           
  59.         tr.commit();  
  60.         session.close();  
  61.     }  
  62.   
  63.     public Linkman getlinkById(long lkm_id) {  
  64.         Session session = HibernateUtils.getSession();  
  65.         Transaction tr = session.beginTransaction();  
  66.           
  67.         Linkman linkman = session.get(Linkman.class, lkm_id);  
  68.           
  69.         tr.commit();  
  70.         session.close();  
  71.         return linkman;  
  72.     }  
  73.   
  74.     public void edit(Linkman linkman) {  
  75.         Session session = HibernateUtils.getSession();  
  76.         Transaction tr = session.beginTransaction();  
  77.           
  78.         session.update(linkman);  
  79.           
  80.         tr.commit();  
  81.         session.close();  
  82.     }  
  83.   
  84. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值