1. 程式人生 > >JavaWeb學習之Hibernate框架(二)

JavaWeb學習之Hibernate框架(二)

utils xtend auto etl SQ dial begin 可選 oct

hibernateAPI詳解

Configuration

創建

技術分享圖片

加載主配置

技術分享圖片

創建sessionFactory

技術分享圖片

SessionFactory

技術分享圖片

技術分享圖片

session

技術分享圖片

獲得事務

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

Transaction封裝事務的操作

打開事務

技術分享圖片

技術分享圖片

提交事務

技術分享圖片

回滾事務

技術分享圖片

CRM練習:保存客戶

CRM:customer relation manager  客戶關系管理系統

1、創建web項目

2、導包

  hibernate包、數據庫驅動包、標簽庫包、BeanUtils

3、引入靜態頁面

4、搭建hibernate框架

5、思路分析

 技術分享圖片

6、開發

7、測試

package com.web;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
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.domain.Customer;
import com.service.CustomerService;

public class AddCustomerServlet extends HttpServlet {
	private CustomerService customerService = new CustomerService();

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1、獲取數據
		request.setCharacterEncoding("UTF-8");
		Map<String, String[]> map = request.getParameterMap();
		// 2、創建對象
		Customer customer = new Customer();
		// 3、封裝數據
		try {
			BeanUtils.populate(customer, map);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 4、調用service中的保存方法
		customerService.save(customer);
		// 5、重定向
		response.sendRedirect(request.getContextPath()+"/ListCustomerServlet");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

  

package com.service;

import com.dao.CustomerDao;
import com.domain.Customer;

public class CustomerService {
	private CustomerDao customerDao = new CustomerDao();

	public void save(Customer customer) {
		customerDao.save(customer); 
	}
}

  

package com.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.domain.Customer;
import com.util.HibernateUtil;

public class CustomerDao {
	public void save(Customer customer) {
		Session session = HibernateUtil.openSession();
		Transaction tx = session.beginTransaction();
		session.save(customer);
		tx.commit();
		session.close();
	}
}

  

package com.domain;

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

public class Customer {
	private Long cust_id;
	// private String 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> linkmens =new HashSet<LinkMan>();
	
	public Set<LinkMan> getLinkmens() {
		return linkmens;
	}

	public void setLinkmens(Set<LinkMan> linkmens) {
		this.linkmens = linkmens;
	}

	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 String getCust_id() {
	// return cust_id;
	// }
	// public void setCust_id(String cust_id) {
	// this.cust_id = cust_id;
	// }
	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;
	}

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

}

  

<?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  package="com.domain">
	<!-- class中的name寫實體類名,table寫數據庫中對應的表名 -->
	<class name="Customer" table="cst_customer">
		<!--id標簽代表主鍵 -->
		<id name="cust_id" column="cust_id">
			<!-- 代表主鍵的生成模式 -->
			<generator class="native"></generator>
			<!-- identity適用於Mysql主鍵生成策略
			<generator class="identity"></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="linkmens">
		     <key  column="lkm_cust_id"></key>
		     <one-to-many  class="LinkMan"/>
		</set>
	</class>
</hibernate-mapping>

  

package com.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static SessionFactory sf;
	static{
		//加載hibernate主配置文件
		Configuration conf=new Configuration().configure();
		sf=conf.buildSessionFactory();
	}
	//獲得全新的session
	public static Session openSession(){
		return sf.openSession();
	}
	//獲取一個與當前線程綁定的session
	public static Session getCurrentSession(){
		return sf.getCurrentSession();
	}
}

  

<?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>
	<session-factory>
	<!--  #hibernate.dialect org.hibernate.dialect.MySQLDialect
             #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
             #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
             #hibernate.connection.driver_class com.mysql.jdbc.Driver
             #hibernate.connection.url jdbc:mysql:///test
             #hibernate.connection.username gavin
             #hibernate.connection.password-->
	 <!--必須配置的5條 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///crm?useUnicode=true&characterEncoding=UTF8</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123456</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<!--可選配置
		#hibernate.show_sql true   自動打印sql在控制臺
		#hibernate.format_sql true  規範格式化打印在控制臺的sql-->
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		<!-- 配置hibernate操作數據庫隔離級別 -->
		<!-- #hibernate.connection.isolation  4 -->
		<property name="hibernate.connection.isolation">4</property>
		<!-- 獲得與當前線程綁定session對象時必須要配置的 -->
		<property name="hibernate.current_session_context_class">thread</property>
		<!-- 	如果沒有表就會自動生成表,如果有就會自動更新表	
		#hibernate.hbm2ddl.auto update -->
		<property name="hibernate.hbm2ddl.auto">update</property>
        <mapping resource="com/domain/Customer.hbm.xml"/>
        <mapping resource="com/domain/LinkMan.hbm.xml"/>
        <mapping resource="com/domain/User.hbm.xml"/>
        <mapping resource="com/domain/Role.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

  

JavaWeb學習之Hibernate框架(二)