hibernate连接数据库 往表中插入数据

实体类

package cn.com.pack;

public class Customer {
	//实体类
private long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_phone;
private String cust_moblie;
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 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_phone() {
	return cust_phone;
}
public void setCust_phone(String cust_phone) {
	this.cust_phone = cust_phone;
}
public String getCust_moblie() {
	return cust_moblie;
}
public void setCust_moblie(String cust_moblie) {
	this.cust_moblie = cust_moblie;
}
public Customer(long cust_id, String cust_name, String cust_source,
		String cust_industry, String cust_level, String cust_phone,
		String cust_moblie) {
	super();
	this.cust_id = cust_id;
	this.cust_name = cust_name;
	this.cust_source = cust_source;
	this.cust_industry = cust_industry;
	this.cust_level = cust_level;
	this.cust_phone = cust_phone;
	this.cust_moblie = cust_moblie;
}

}

实体类的映射文件




	
	
	
		
		
		
			
			
		
		
		
		
		
		
		
		
		
	

hibernate的核心文件、




	
		
		com.mysql.jdbc.Driver
		
		jdbc:mysql://localhost:3306/lf
		
		root
		
		123456
		
		
		org.hibernate.dialect.MySQLDialect
		
		true
		
		true
		
		
		update
		
		
	

 

 

测试类:

 

package cn.com.pack;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class Demo {
//使用ibernate来保存数据
	@Test
	public  void test(){
		//1.加载文件
		Configuration cfg=new Configuration().configure();
		//2.创建一个sessionfactory;
		SessionFactory sessionfactory=cfg.buildSessionFactory();
		//3.创建session对象.sesssion类似于connection
		Session session=sessionfactory.openSession();
		//4.开启事务
		Transaction tx=session.beginTransaction();
		//5.执行相关操作
		Customer c=new Customer(1, "田江南", "88", "力凡", "A", "15656215623", "0562");
		session.save(c);
		//6.事务提交
		tx.commit();
		//7.释放资源
		session.close();
	}
}

结果截图如下:

hibernate连接数据库 往表中插入数据_第1张图片

你可能感兴趣的:(java之ssh框架专栏)