目录
Hibernate介绍
Hibernate是一种ORM框架,全称为 Object_Relative DateBase-Mapping,在Java对象与关系数据库之间建立某种映射,以实现直接存取Java对象!
ORM概述
ORM是一种思想
- O代表的是Objcet
- R代表的是Relative
- M代表的是Mapping
ORM->对象关系映射…ORM关注是对象与数据库中的列的关系
Hibernate入门指南
入门hibernate,总结下来可以分为以下三个步骤,概括地说就是:
编写配置文件
在编写配置文件之前,我们需要首先编写一个实体类(举例)User对象,其路径在com.yhy.Entity包下>User.java
@javax.persistence.Entity @Table(name = "tb_user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY)//id 自动增长 private Long id; @Column(length = 50)//设定name长度为50 private String name; private int age; public User() { } //各种getter和seterr @Override public String toString() { return String.format("id=%d, name=%s, age=%d",id,name,age); } }
接下来便是编写hibernate.cfg.xml文件,该文件需要将其放在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> <!--1.数据库连接配置--> <property name="connection.driver_class">com.mysql.jdbc. Driver</property> <property name="connection.url">jdbc:mysql:///test</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!--数据库方法配置,hibernate在运行时,会根据不同的方言生成符合当 前数据库语法的sql--> <property name="dialect">org.hibernate.dialect.MySQL55Dialect</property> <!--2.其他相关配置--> <!--2.1 显示hibernate在运行时执行的sql语句--> <property name="show_sql">true</property> <!--2.2 格式化sql--> <property name="hibernate.format_sql">true</property> <!--2.3自动建表--> <property name="hbm2ddl.auto">update</property> <!--3.加载所有映射--> <mapping class="com.yhy.Entity.User"></mapping> </session-factory> </hibernate-configuration>
测试
在配置完成主配置文件hibernate.cfg.xml后,便可编写测试类来测试Hibernate了
package com.yhy.Test; import com.yhy.Entity.User; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.query.Query; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.List; public class T1 { private SessionFactory factory; @Before public void init(){ //获取加载配置管理类并默认加载hibernate.cfg.xml文件 Configuration cfg = new Configuration().configure(); factory = cfg.buildSessionFactory();//得到Session工厂对象 } @After public void close(){ factory.close(); } @Test public void createAndInsertUser(){ //得到Session对象 Session session = factory.openSession(); //String hql = "select u from User u"; //使用Hibernate操作数据库,都要开启事务,得到事务对象 Transaction transaction = session.getTransaction(); //开启事务 transaction.begin(); for (int i=0;i<=10;i++) { //创建对象 User user = new User(); user.setAge(i); user.setName("张"+i); //把对象添加到数据库中 session.saveOrUpdate(user); } //提交事务 transaction.commit();//多次添加,一次提交,不然会出现未关闭的情况 //关闭session session.close(); } @Test public void selectAll() { Session session = factory.openSession(); //查询全部 String hql = "FROM User"; Query query = session.createQuery(hql); List list = query.list(); System.out.println(list); session.close(); } @Test public void selectById(){ Session session = factory.openSession(); Transaction transaction = session.beginTransaction(); //根据ID查询 User user = session.get(User.class,10L); System.out.println(user); transaction.commit(); session.close(); } @Test public void updateById(){ Session session = factory.openSession(); Transaction transaction = session.beginTransaction(); //根据Id修改user信息 User user = session.get(User.class,8L); user.setName("李三"); session.saveOrUpdate(user); transaction.commit(); session.close(); } @Test public void deleteById(){ Session session = factory.openSession(); Transaction transaction = session.beginTransaction(); //根据Id删除user User user = new User(3L); session.remove(user); transaction.commit(); session.close(); } }
到此为止,关于Hibernate的配置以及使用Hibernate对数据库进行增删改查的基础入门操作已经介绍完毕,多多练手,才能更进一步的加深自己对Hibernate的学习。
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/231705.html原文链接:https://javaforall.net
