Hibernate

1.开发流程

项目配置好后的结构:

1.下载源码: 版本:hibernate-distribution-3.6.0.Final

2.引入hibernate需要的开发包(3.6版本),如果没有引用成功,在jar包上右键执行:add as library

3.编写实体对象及对象的映射xml文件

实体类:

package com.eggtwo.test;

import java.util.Date;

public class Student {
private int id;
private String name;
private int age;
private Date birthday;
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} }

数据库表结构:

映射xml文件:(放在包下面)

<?xml version="1.0"?>
<!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.eggtwo.test"> <class name="Student" table="student">
<!-- 主键 ,映射-->
<id name="id" column="id">
<generator class="native"/>
</id>
<!-- 非主键,映射 -->
<property name="name" column="name"></property>
<property name="age" column="age"></property>
<property name="birthday" column="birthday"></property>
</class> </hibernate-mapping>

4.配置主配置文件:src/hibernate.cfg.xml

<!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>
<!-- 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.show_sql">true</property> <!-- 加载所有映射 -->
<mapping resource="com/eggtwo/test/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>

5.测试添加一条数据:

package com.eggtwo.test;
import java.util.Date; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test; public class TestApp {
@Test
public void testHello() throws Exception {
// 创建对象:主键自增长
Student s = new Student();
s.setAge(12);
s.setName("班长");
s.setBirthday(new Date()); // 获取加载配置文件的管理类对象
Configuration config = new Configuration();
config.configure(); // 默认加载src/hibenrate.cfg.xml文件
// 创建session的工厂对象
SessionFactory sf = config.buildSessionFactory();
// 创建session (代表一个会话,与数据库连接的会话)
Session session = sf.openSession();
// 开启事务
Transaction tx = session.beginTransaction();
//保存-数据库
session.save(s);
// 提交事务
tx.commit();
System.out.println("save success");
// 关闭
session.close();
sf.close();
} }

2.映射详解

最新文章

  1. sstream
  2. BZOJ 1455 罗马游戏 ——左偏树
  3. 空间不支持openssl解决办法
  4. Linux:去除认证,加速 SSH登录
  5. wireshark解密本地https流量笔记
  6. C++ string 用法详解
  7. JS来操作hover
  8. java总结第四次//常用类
  9. [转]https方式使用git保存密码的方式
  10. CSS3每日一练之选择器-结构性伪类选择器
  11. swift 与 指针初级使用
  12. java学习笔记_GUI(1)
  13. linux服务器时间同步
  14. Python之路第十一天,高级(3)-线程池
  15. Spring2.5学习3.3_@Autowire注解实现手动装配
  16. Python入门 - 容器类型
  17. js基本包装类型和引用类型
  18. mysql 状态锁 连接数
  19. Monkey工具
  20. AJAX实现注册

热门文章

  1. C# 删除文件到回收站
  2. MVC+Ninject+三层架构+代码生成 -- 总结(三、實體類)
  3. python 库 PrettyTabble 使用与错误
  4. Service Mesh服务网格新生代--Istio
  5. php中,5行代码实现无限级分类
  6. 基于YOLO3对图像加框的函数draw_image()
  7. 正则表达式,匹配非本站图片网址去掉img标签内容实例
  8. 《2017年-2018年中国MES软件及服务市场研究报告》正式发布!
  9. MES助力伊利集团打造智慧工厂
  10. OKGo vs RxHttpUtils ...