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;
}

}

实体类的映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
	<!--创建类和表之间的一个映射关系 -->
	<!-- class标签:用来建立类和表的映射 *name属性:类的全路径 *table属性:表名(如果类名和表名是一致的,那么table属性客户省略) 
		*catalog属性:数据库的名称,可以省略 -->
	<class name="cn.com.pack.Customer" table="Customer" catalog="lf">
		<!--建立类中属性和表中主键的映射 -->
		<!--id标签:用来建立类中属性和表中属性的主键字段对应 *name属性:类中的属性名; *column属性:表中字段名(如果类中的属性名和表中的字段一致,那么省略) 
			*length:字段的长度 *type属性:类型;写java的数据类型 -->
		<id name="cust_id" column="id" length="255" type="long">
			<!-- 主键生成策略 -->
			<generator class="native" />
		</id>
		<!--建立类中普通属性与表中字段的映射关系 -->
		<!-- property标签:用来 建立类中普通属性与表中的字段对应 *name属性:类中的属性名 *column属性:表中字段名(如果类中属性名与表中字段名一致的话,那么这个可以省略)\ 
			*length属性:字段的长度 *type属性:写java的字段类型 -->
		<property name="cust_name" column="name" length="255" type="string" />
		<property name="cust_source" column="source" length="255"
			type="string" />
		<property name="cust_industry" column="industry" length="255"
			type="string" />
		<property name="cust_level" column="level" length="255" type="string" />
		<property name="cust_phone" column="phone" length="255" type="string" />
		<property name="cust_moblie" column="moblie" length="255"
			type="string" />
	</class>
</hibernate-mapping>

hibernate的核心文件、

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
	<session-factory>
		<!-- 必要的配置信息:连接数据库的基本操作 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<!-- hibernate连接数据库的名称 -->
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/lf</property>
		<!-- hibernate连接数据库的用户名 -->
		<property name="hibernate.connection.username">root</property>
		<!-- hibernate连接数据库的密码 -->
		<property name="hibernate.connection.password">123456</property>
		<!-- hibernate的属性 -->
		<!--hibernate的方言:作用,根据配置的方言生成相应的sql语句 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- hibernate显示sql语句 -->
		<property name="hibernate.show_Sql">true</property>
		<!-- hibernate格式化sql语句 -->
		<property name="hibernate.formate_sql">true</property>
		<!-- hibernate的hbm2ddl(数据定义语言:create drop alter ...属性) -->
		<!-- hbm2ddl.auto的取值 *none:不用hibernate自动生成表 *create:每次都会创建一个新的表(测试); *create-drop:每次都会创建一个新的表,程序执行结束以后就会删除这个表(测试) 
			*update:如果数据库中有表,就使用原来的表,如果没有就创建一个表,可以更新表的结构、 *validate:只会只用原来的表,对映射关系进行校验 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		<!--hibernate加载映射 -->
		<mapping resource="cn/com/pack/Customer.hbm.xml" />
	</session-factory>
</hibernate-configuration>

 

 

测试类:

 

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();
	}
}

结果截图如下:

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_37591637

请给我持续更新的动力~~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值