eclipse中使用Hibernate

1. 导入jar包

  1. 导入mysql驱动包
  2. 导入Hibernate需要的jar包(将Hibernate解压后\lib\required下的所有包)
  3. 导入log4j需要的包(分别包含接口类,实现类,桥梁类

2. 创建表结构,创建表对应的Entity

	CREATE TABLE `cst_customer` (
	  `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
	  `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
	  PRIMARY KEY (`cust_id`)
	) ENGINE=InnoDB AUTO_INCREMENT=94 DEFAULT CHARSET=utf8;
public class Customer {
	private Long cust_id;
	private String cust_name;	
}

3. 编写表结构和实体类的映射文件

该映射文件与实体类放在一起

<?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.daben.entity.Customer" table="cst_customer">
		<!-- 配置id -->
		<id name="cust_id" column="cust_id">
			<!-- 主键生成策略 -->
			<generator class="native"></generator>
		</id>
		<!-- 配置其他属性 -->
		<property name="cust_name" column="cust_name"/>
	</class>
</hibernate-mapping>

4. 编写Hibernate的核心配置文件

核心配置文件放在src目录下,名称为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,一个数据库对应一个SeesionFactory标签 -->
	<session-factory>
		<!-- 必须要配置的参数有5个, 4大参数, 数据库的方言 -->
		<!-- 加载驱动 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<!-- 创建连接,///可以省略localhost -->
		<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>
		<!-- 用户 -->
		<property name="hibernate.connection.username">root</property>
		<!-- 密码 -->
		<property name="hibernate.connection.password">366464 </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>
		<!-- 映射配置文件, 需要引入映射的配置文件 -->
		<mapping resource="com/daben/entity/Customer.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

编写测试类

public class Test {
    @Test
	public void save() {
        // 加载配置文件
        Configuration connf = new Configuration().configure();
        // 创建SessionFactory
        SessionFactory sessionFactory = connf.buildSessionFactory();
        // 创建Session
        Session session = sessionFactory.openSession();
        // 创建事务
        Transaction transaction = session.beginTransaction();
        // 执行增删改
        /**
        // 新增
        Customer c = new Customer();
		c.setCust_name("zhangsan");
        session.save(c);
        // 删除
        // 根据id获取对象
		Customer customer = session.get(Customer.class, 1L);
        session.delete(customer);
        // 修改
        Customer customer = session.get(Customer.class, 1L);
        customer.setCust_name("lisi");
        session.update(customer);
        **/
        // 提交事务
        transaction.commit();
        // 释放资源
        session.close();        
    }
}

声明:
有一些博文是看的黑马程序员视频,然后跟着老师做的笔记
Spring是跟子路老师学的
特此感谢,写这些文章的目的是为了自己方便查阅

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值