Java基础学习记录之Hibernate单表的CRUD操作

    xiaoxiao2021-03-25  83

    1.hibernate和jdbc的优劣:

    jdbc的缺点 编程时很繁琐,会使用很多的try...catch语句,有上篇的jdbc的crud代码可以看出;没有做到面向对象编程;因为直接使用SQL语句,所以跨平台性很差;没有数据缓存。 jdbc的优点 效率比较高。 hibernate的优点 完全面向对象编程;hibernate的缓存很厉害,一级缓存,二级缓存,查询缓存;编程时候比较方便简单;跨平台性很强,适合企业内部的系统

    hibernate的缺点 效率低;如果表中数据量在千万级别,则不适合使用hibernate;如果表与表的关系特别复杂,则不适合使用hibernate。

    2.hibernate环境搭建

    因为我做的是单表的crud操作,所以可以是Java工程也可以是Java Web工程,然后导入jar包,如图:

    3.代码

    1. hibernate.cfg.xml文件,主要用途:设置链接数据库的相关信息。 <?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="connection.username">root</property> <!-- 代表数据库的密码 --> <property name="connection.password">hello</property> <!-- 代表数据库的驱动 --> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- 代表数据库的url --> <property name="connection.url"> jdbc:mysql://localhost:3306/hibernate_CRUD </property> <!-- 代表数据库的方言,即生成什么样的SQL语句 --> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- 这个hibernate.hbm2ddl.auto参数的作用主要用于:验证,更新,创建数据库表结构。如果不是此方面的需求建议set value="none"。, validate:每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。 update:最常用的属性,启动hibernate时检查表是否存在,不存在的话,创建表,存在的话就什么都不做。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会 create:每次加载hibernate时都会删除上一次的生成的表,然后根绝持久化类和映射文件生成新表。这就是导致数据库表数据丢失的一个重要原因。 create-drop:每次加载hibernate时根据持久化类生成表,但是结束后,表就自动删除。 --> <property name="hbm2ddl.auto">update</property> <mapping resource="hibernate/domain/Person.hbm.xml" /> </session-factory> </hibernate-configuration> 2,持久化类: import java.io.Serializable; /* * 持续化类 *实现序列化,方便以后类对象的传输 */ public class Person implements Serializable { private int id; private String name; private int age; 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; } } 3.映射文件 <?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:持久化类对应的数据库表名,不写就默认为类名。 catalog:数据库的名字,不建议写,因为前面hibernate。cfg.xml文件里面有 --> <class name="hibernate.domain.Person" table="person"> <!-- 用来描述主键,name:属性名称,column:属性对应的表的字段,不写默认与name一样, length 属性的名称对应的表的字段的长度,不写默认最大长度,建议写,不然影响数据库效率 type:指定 Hibernate映射类型.如果没有为某个属性显式设定映射类型, Hibernate 会运用反射机制先识别出持久化类 --> <id name="id" length="2" > <!-- 主键的产生器 mysql中可以使用的标识符生成器: identity:适用于代理主键。由底层数据库生成标识符。前提条件是底层数据库支持自动增长字段类型。 increment: 适用于代理主键。由Hibernate自动以递增的方式生成标识符,每次增加1。 优点:由于它的机制不依赖于底层数据库系统,因此它适合于所有的数据库系统。 缺点:只适合有单个Hibernate应用进程访问同一个数据库,在集群环境下不推荐使用它。 hiho:适用于代理主键。Hibernate根据high/low算法来生成标识符。 native:适用于代理主键。根据底层数据库对自动生成标识符的支持能力,来选择identity,sequence, hilo。很适合于跨平台开发,即同一个Hibernate应用需要连接多种数据库系统。 --> <generator class="increment"></generator> </id> <property name="name" length="20"></property> <property name="age" length="2"></property> </class> </hibernate-mapping> 4.CRUD的实现 import hibernate.domain.Person; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; public class PersonDao { @Test // 增加 public void addPerson(){ // 加载hibernate的配置文件 Configuration cfg = new Configuration(); cfg.configure(); SessionFactory sf = cfg.buildSessionFactory(); // 产生session Session session = sf.openSession(); // 产生事务 Transaction ts = session.beginTransaction(); // 创建对象 Person p = new Person(); p.setName("James"); p.setAge(33); // 保存对象 session.save(p); // 事务提交 ts.commit(); // session关闭 session.close(); } @Test // 查询 public void findPersonById(){ Configuration cfg = new Configuration(); cfg.configure(); SessionFactory sf = cfg.buildSessionFactory(); Session session = sf.openSession(); // 第一个参数是持久化类的class形式,第二个为主键的值 Person p = (Person)session.get(Person.class, 1); System.out.println(p.getAge()); session.close(); } @Test // 修改 public void updatePerson(){ Configuration cfg = new Configuration(); cfg.configure(); SessionFactory sf = cfg.buildSessionFactory(); Session session = sf.openSession(); Transaction ts = session.beginTransaction(); Person p = (Person)session.get(Person.class, 1); p.setName("Wade"); p.setAge(34); session.update(p); ts.commit(); session.close(); } @Test // 删除 public void deletePerson(){ Configuration cfg = new Configuration(); cfg.configure(); SessionFactory sf = cfg.buildSessionFactory(); Session session = sf.openSession(); Transaction ts = session.beginTransaction(); Person p = (Person)session.get(Person.class,1); session.delete(p); ts.commit(); session.close(); } }
    转载请注明原文地址: https://ju.6miu.com/read-35556.html

    最新回复(0)