hibernate框架大作业

实现代码:

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
	
<hibernate-configuration>
	
	<!-- 记住:先配置SessionFactory标签,一个数据库对应一个SessionFactory标签 -->
	<session-factory>
		
		<!-- 必须要配置的参数有5个,4大参数,数据库的方言 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123</property>
		
		<!-- 数据库的方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
		<!-- 可选配置 -->
		<!-- 显示SQL语句,在控制台显示 -->
		<property name="hibernate.show_sql">true</property>
		<!-- 格式化SQL语句 -->
		<property name="hibernate.format_sql">true</property>
		<!-- 生成数据库的表结构 
			update:如果没有表结构,创建表结构。如果存在,不会创建,添加数据
		-->
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<property name="hibernate.current_session_context_class">thread</property>
		
		<!-- 映射配置文件,需要引入映射的配置文件 -->
		<mapping resource="com/oracle/domain/Customer.hbm.xml"/>
		<mapping resource="com/oracle/domain/Linkman.hbm.xml"/>
		
	</session-factory>
	
</hibernate-configuration>	

Customer.java

package com.oracle.domain;

import java.util.HashSet;
import java.util.Set;

public class Customer {
    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;
    
    private Set<Linkman> linkmans = new HashSet<Linkman>();
    
	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 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;
	}
	public Set<Linkman> getLinkmans() {
		return linkmans;
	}
	public void setLinkmans(Set<Linkman> linkmans) {
		this.linkmans = linkmans;
	}
    
	@Override
	public String toString() {
		return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_user_id=" + cust_user_id
				+ ", cust_create_id=" + cust_create_id + ", cust_source=" + cust_source + ", cust_industry="
				+ cust_industry + ", cust_level=" + cust_level + ", cust_linkman=" + cust_linkman + ", cust_phone="
				+ cust_phone + ", cust_mobile=" + cust_mobile + "]";
	}
    
}

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.oracle.domain.Customer" table="cst_customer">
		<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 name="cust_create_id" column="cust_create_id"/>
		<property name="cust_source" column="cust_source"/>
		<property name="cust_industry" column="cust_industry"/>
		<property name="cust_level" column="cust_level"/>
		<property name="cust_linkman" column="cust_linkman"/>
		<property name="cust_phone" column="cust_phone"/>
		<property name="cust_mobile" column="cust_mobile"/>
		
		<set name="linkmans" cascade="save-update">
			<key column="lkm_cust_id"></key>
			<one-to-many class="com.oracle.domain.Linkman" />
		
		</set>
	</class>
</hibernate-mapping>

Linkman.java

package com.oracle.domain;

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;
	}
	
	@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 + "]";
	}
}

Linkman.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.oracle.domain.Linkman" table="cst_linkman">
    		<id name="lkm_id" column="lkm_id">
    		<generator class="native"></generator>
    		</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.oracle.domain.Customer" column="lkm_cust_id" cascade="all"></many-to-one>
    	
    	</class>
    
    </hibernate-mapping>

CusDao.java

package com.oracle.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.oracle.domain.Customer;
import com.oracle.utils.HibernateUtils;

public class CusDao {

	public void addCustomer(Customer c) {
		Session session = HibernateUtils.getSession();
		
		Transaction tr = session.beginTransaction();
		
		session.save(c);
		
		tr.commit();
		
		session.close();
	}
	
	public List<Customer> findAll(){
		// QBC查询
		Session session = HibernateUtils.getSession();
		Transaction tr = session.beginTransaction();
		// 查询
		Criteria criteria = session.createCriteria(Customer.class);
		// 查询
		List<Customer> list = criteria.list();
		tr.commit();
		session.close();
		return list;
	}
	
	public List<Customer> findCus(String custname) {
		Session session = HibernateUtils.getSession();
		Transaction tr = session.beginTransaction();
		
		Criteria criteria = session.createCriteria(Customer.class);
		
		if(custname!=null&&!custname.trim().isEmpty()){
			criteria.add(Restrictions.like("cust_name", "%"+custname+"%"));
		}
		List list = criteria.list();
		
		tr.commit();
		session.close();
		return list;
	}

	public void editCus(Customer customer) {
		Session session = HibernateUtils.getSession();
		
		Transaction tr = session.beginTransaction();
		
		session.update(customer);
		
		tr.commit();
		
		session.close();
	}

	public void deleteCus(Long cust_id) {
		Session session = HibernateUtils.getSession();
		Transaction tr = session.beginTransaction();
		
		session.delete(session.get(Customer.class, cust_id));
		
		tr.commit();
		session.close();
	}

	public Customer getCusById(long cust_id) {
		Session session = HibernateUtils.getSession();
		Transaction tr = session.beginTransaction();
		
		Customer customer = session.get(Customer.class, cust_id);
		
		tr.commit();
		session.close();
		return customer;
	}

}

LinkmanDao.java

package com.oracle.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.oracle.domain.Linkman;
import com.oracle.utils.HibernateUtils;

public class LinkmanDao {

	public List<Linkman> findAll() {
		Session session = HibernateUtils.getSession();
		Transaction tr = session.beginTransaction();
		
		Criteria criteria = session.createCriteria(Linkman.class);
		List list = criteria.list();
		
		tr.commit();
		session.close();
		return list;
	}

	public List<Linkman> findByName(String lkm_name) {
		Session session = HibernateUtils.getSession();
		Transaction tr = session.beginTransaction();
		
		Criteria criteria = session.createCriteria(Linkman.class);
		
		if(lkm_name!=null&&!lkm_name.trim().isEmpty()){
			criteria.add(Restrictions.like("lkm_name", "%"+lkm_name+"%"));
		}
		List list = criteria.list();
		
		tr.commit();
		session.close();
		return list;
	}

	public void add(Linkman linkman) {
		Session session = HibernateUtils.getSession();
		Transaction tr = session.beginTransaction();
		session.save(linkman);
		
		tr.commit();
		
		session.close();
	}

	public void delete(long lkm_id) {
		Session session = HibernateUtils.getSession();
		Transaction tr = session.beginTransaction();
		
		session.delete(session.get(Linkman.class, lkm_id));
		
		tr.commit();
		session.close();
	}

	public Linkman getlinkById(long lkm_id) {
		Session session = HibernateUtils.getSession();
		Transaction tr = session.beginTransaction();
		
		Linkman linkman = session.get(Linkman.class, lkm_id);
		
		tr.commit();
		session.close();
		return linkman;
	}

	public void edit(Linkman linkman) {
		Session session = HibernateUtils.getSession();
		Transaction tr = session.beginTransaction();
		
		session.update(linkman);
		
		tr.commit();
		session.close();
	}

}

阅读更多

没有更多推荐了,返回首页