一.Hibernate(开放源代码的对象关系映射框架)简介:java
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了很是轻量级的对象封装,它将POJO与数据库表创建映射关系,是一个全自动的orm框架,hibernate能够自动生成SQL语句,自动执行,使得Java程序员能够为所欲为的使用对象编程思惟来操纵数据库。 Hibernate能够应用在任何使用JDBC的场合,既能够在Java的客户端程序使用,也能够在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate能够在应用EJB的J2EE架构中取代CMP,完成数据持久化的重任。mysql
四.hibernate入门Demo程序员
该项目使用到的jar包sql
1.我使用的是mysql关系型数据库,先创建一个数据库,而且建立一个customer(客户信息)表数据库
2.建立一个Customer实体类(持久化类)编程
持久化类是应用程序中的业务实体类,这里的持久化是指类的对象可以被持久化保存到数据库中.缓存
Hibernate 使用普通的JAVA对象(POJO),即POJO的编程模式来进行持久化.session
该类中包含的是与数据库表相对应的各个属性,这些属性经过getter/setter方法来访问,对外部隐藏了内部的实现细节.架构
一般持久化类的编写应嘎遵循一些规则:app
1).持久化类中必须提供无参数public构造器(若是没有提供任何构造方法,虚拟机会自动提供默认构造方法,可是若是提供了其余有参数的构造方法的话
虚拟机再也不提供默认构造方法,必须手动编写无参构造方法).
2).持久化类中全部属性使用private修饰,提供public的setter/getter方法
3).必须提供标识属性OID,与数据库表中逐渐对应,例以下面Customer类id属性
4).持久化类属性应尽可能使用基本数据类型的包装类型,例如int换成Integer,long换成Long,目的是为了与数据库表的字段默认值null一致。(例如int是区分不开null跟0的,不赋值的int类型默认值为0)
5).持久化类不要用final修饰,使用final修饰将没法生成代理对象进行优化.
public class Customer { private Integer id; //主键id private String name; //客户姓名 private Integer age; //客户年龄 private String sex; //客户性别 private String city; //客户地址 /** * @return the id */ public Integer getId() { return id; } /** * @param id the id to set */ public void setId(Integer id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the age */ public Integer getAge() { return age; } /** * @param age the age to set */ public void setAge(Integer age) { this.age = age; } /** * @return the sex */ public String getSex() { return sex; } /** * @param sex the sex to set */ public void setSex(String sex) { this.sex = sex; } /** * @return the city */ public String getCity() { return city; } /** * @param city the city to set */ public void setCity(String city) { this.city = city; } //重写toString()方法 @Override public String toString() { return "Customer [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", city=" + city + "]"; } }
3.编写映射文件Customer.hbm.xml
实体类Customer目前还不具有持久化操做的能力,而Hibernate须要知道实体类Customer映射到数据库 Hibernate中的哪一个表,以及类中的哪一个属性
对应数据库表中的哪个字段,这些都须要在映射文件中配置.
在实体类Customer所在的包中,建立一个名称为Customer.hbm.xml的映射文件,在该文件中定义了实体类Customer的属性是如何映射到customer表的列上的
开始编写xml文件,该文件头部信息能够经过下面的步骤找到:
<?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> <!-- name表明的是实体类名,table表明的是表名 --> <class name="com.hck.entity.Customer" table="customer"> <!-- name=id表明的是customer类中属性 column=id表明的是table表中的字段 --> <id name="id" column="id"> <!-- 主键生成策略 --> <generator class="native"/> </id> <!-- 其余属性使用property标签来映射 --> <property name="name" column="name" type="string"/> <property name="age" column="age" type="integer"/> <property name="sex" column="sex" type="string"/> <property name="city" column="city" type="string"/> </class> </hibernate-mapping>
4.编写核心配置文件hibernate.cfg.xml
Hibernate的映射文件反映了持久化类和数据库表的映射信息,
而Hibernate的配置文件则主要用来配置数据库链接以及Hibernate运行时所须要的各个属性的值.
在项目的src目录下建立一个名称为hibernate.cfg.xml的文件
xml的头部编写能够按下图因此查找复制
编写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> <session-factory> <!--指定方言 --> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- 数据库驱动 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 链接数据库的url --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tb_test</property> <!-- 数据库用户名 --> <property name="hibernate.connection.username">root</property> <!-- 数据库密码 --> <property name="hibernate.connection.password">123456</property> <!-- 其余配置 --> <!-- 格式化sql --> <property name="format_sql">true</property> <!-- 用来关联hbm配置文件 --> <mapping resource="com/hck/entity/Customer.hbm.xml"/> </session-factory> </hibernate-configuration>
5.编写单元测试类代码以下:
1).解释一下
config=new Configuration().configure();是如何读取src目录下的hibernate.cfg.xml
跟踪代码能够看到:
默认读取src目录下的hibernate.cfg.xml文件,因此hibernate.cfg.xml文件放在src是基于约定优于配置原理的
public class HibernateTestDemo { //定义变量 Configuration config; SessionFactory sessionFactory; Session session; Transaction transaction; //before表示在方法执行前执行 @Before public void setUp() { //1.加载hibernate.cfg.xml配置 config=new Configuration().configure(); //2.获取SessionFactory sessionFactory=config.buildSessionFactory(); //3.得到一个session session=sessionFactory.openSession(); //4.开始事务 transaction=session.beginTransaction(); } //添加操做 @Test public void insert() { //5.操做 Customer customer=new Customer(); customer.setId(1); customer.setName("zhangsan"); customer.setAge(20); customer.setSex("m"); customer.setCity("guangzhou"); session.save(customer); } //删除操做 @Test public void delete() { //先查询 Customer customer=(Customer)session.get(Customer.class, 1); //再删除 session.delete(customer); } //查询操做 @Test public void select() { Customer customer=(Customer)session.get(Customer.class, 1); System.out.println(customer); } //更新操做 @Test public void update() { Customer customer=new Customer(); customer.setId(1); customer.setName("zhangsan"); customer.setAge(20); customer.setSex("m"); //修改地址为beijing customer.setCity("beijing"); //存在就更新,不存在就执行插入操做 session.saveOrUpdate(customer); } //After表示在方法执行结束后执行 @After public void closeTransaction() { //6.提交事务 transaction.commit(); //7.关闭资源 session.close(); sessionFactory.close(); } }
1)执行插入操做后,打开mysql使用select * from customer;获得的结果:
2)执行查询操做(查看控制台输出)
3.执行更新操做
4.删除操做
五.总结
超级完整版hibernate入门demo完成了,若有不懂或者问题请留言~