1.1什么是hibernate:
①是一个开放源代码的对象关系映射框架(ORM)
②它对JDBC进行了非常轻量级的对象封装(消耗资源少)
③它将POJO与数据库表建立映射关系,是一个全自动的orm框架,
④hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。
⑤Hibernate可以应用在任何使用JDBC的场合
⑥具有数据库兼容性
1.2关系型数据库(ORM):
①表和类关联
②行和对象关联
③字段和属性关联
1.3工作原理:
2.开发流程:
2.1第一步:搭建环境:
2.1新建项目
2.2导入项目依赖相关maven包:
<!--hibernate-->
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-osgi -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-osgi</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-envers -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-proxool -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-proxool</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-infinispan -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-infinispan</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!--数据库驱动,根据数据库的版本选择驱动版本-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!--日志包-->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
2.3项目目录结构:
2.2第二步:创建hibernate的核心配置文件
找到依赖包:
、
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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">
jdbc:mysql://127.0.0.1:3307/hibernate?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<!--扩展属性 必须指向数据库的方言 mysql版本低于5.5用MySQL5Dialect-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL55Dialect</property>
<!--自动创建表 一般不需要 常规项目先做数据库表-->
<property name="hbm2ddl.auto">update</property><!--update自动更新-->
<!--显示SQL-->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property><!--格式化 好看一点-->
<!--**********2.注册实体*************************-->
<!-- <mapping resource=""></mapping>-->
</session-factory>
</hibernate-configuration>
2.3第三步:创建ORM映射文件和对应的实体类
2.3.1实体类:
public class YtUser {
private long id;
private String user;
private String password;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
在
的build标签中添加:
<!--把java目录中的配置文件,也能够打包到java包中-->
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<!--是否替换资源中的属性-->
<filtering>false</filtering>
</resource>
</resources>
2.3.2ORM映射文件:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--1.类和表映射--> <!--ytuser:数据库的表名-->
<class name="com.lemon.pojo.YtUser" table="ytuser">
<!--必须有主键,对象和行关联 唯一标识 OID(对象ID)-->
<id name="id" column="id">
<!--主键生成方式 identity自增-->
<generator class="identity"/>
</id>
<!--属性映射-->
<property name="username" column="username"/>
<property name="password" column="password"/>
</class>
</hibernate-mapping>
在
中注册实体:
2.3.3测试:
新建数据库:
测试类:
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class App
{
public void initTable(){
//加载配置,初始化
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();
}
public static void main( String[] args )
{
App a =new App();
a.initTable();
}
}
运行成功后:(生成表)
2.4第四步:操作数据
2.4.1插入:
import com.lemon.pojo.YtUser;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class App
{
public void initTable(){
//加载配置,初始化
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();
}
public void addUser(){
//第一步:加载配置
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
//第二步:得到工厂
SessionFactory sessionFactory = configuration.buildSessionFactory();
//第三步:得到Session
Session session = sessionFactory.openSession();
//第四步:开启事务
Transaction transaction = session.beginTransaction();
//第五步:执行工作
YtUser pojo = new YtUser();
pojo.setUsername("测试1");
pojo.setPassword("测试1");
session.save(pojo);
//第六步:提交事务
transaction.commit();
//关闭连接
session.close();
sessionFactory.close();
}
public static void main( String[] args )
{
App a =new App();
// a.initTable();
a.addUser();
}
}
运行:
数据库:
为了方便,addUser封装成工具类:
import com.lemon.pojo.YtUser;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class YtUserUtils {
public static void addUser(String userName, String password){
//第一步:加载配置
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
//第二步:得到工厂
SessionFactory sessionFactory = configuration.buildSessionFactory();
//第三步:得到Session
Session session = sessionFactory.openSession();
//第四步:开启事务
Transaction transaction = session.beginTransaction();
//第五步:执行工作
YtUser pojo = new YtUser();
pojo.setUsername(userName);
pojo.setPassword(password);
session.save(pojo);
//第六步:提交事务
transaction.commit();
//关闭连接
session.close();
sessionFactory.close();
}
}
测试:
把上面的测试类的mian函数改为:
public static void main( String[] args )
{
// App a =new App();
// a.initTable();
// a.addUser();
YtUserUtils.addUser("测试3","测试3");
}
2.4.2更新:
封装session:
private static SessionFactory sessionFactory;
static {
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
sessionFactory = configuration.buildSessionFactory();
}
public static Session openSession(){
return sessionFactory.openSession();
}
更新:
//更新
public void updateT(){
Session session = YtUserUtils.openSession();
Transaction transaction = session.beginTransaction();
//更新方式1;
/* //如果更新的是部分字段,先得到再更新,否则使用HQL
YtUser ytUser = session.get(YtUser.class,2L);
ytUser.setPassword("123456");*/
//更新方式2:
//这种必须全部更新 否则不更新的为null
YtUser ytUser = new YtUser();
ytUser.setId(2L);
ytUser.setPassword("789");
session.update(ytUser);
transaction.commit();
session.close();
}
public static void main( String[] args )
{
App a =new App();
// a.initTable();
// a.addUser();
// YtUserUtils.addUser("测试3","测试3");
// a.findById(2L);
a.updateT();
}
2.4.3:删除:
//删除public void deletId(Long id){
Session session = YtUserUtils.openSession();
Transaction transaction = session.beginTransaction();
YtUser ytUser = session.get(YtUser.class,id);
session.delete(ytUser);
transaction.commit();
session.close();
}
public static void main( String[] args ){
App a =new App();
// a.initTable();
// a.addUser();
// YtUserUtils.addUser("测试3","测试3");
// a.findById(2L);
//更新
// a.updateT();
//删除
a.deletId(2L);
}
2.4.4:查找
//查找 通过id找到对应的username和password
public void findById(Long id){
Session session = YtUserUtils.openSession();
YtUser ytUser = session.get(YtUser.class,id);
System.out.println(ytUser.getUsername() + "\t" + ytUser.getPassword());
}