浅谈hibernate
个人理解
关于hibernate,就是一个全自动的orm框架,没有多说的。不过在怎么全自动也不是一个人工智能,在当前计算机基础下,不可能出现人工智能,再怎么智能也需要人给出指令或者命令,才能给你执行。话不多说,进入正题
配置
首先就是建立工程,然后去pom导包
,导包的话百度上多了去了,自己找。我就随便放
(idea----idea----idea----idea----)
<!-- hibernate5 -->
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.12.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>4.2.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>5.2.12.Final</version>
</dependency>
然后再把你需要的其他的资源放进去,下载好就行了。
配置文件
配置文件获得数据源和配置SQL方言。生成SQL语句和映射实体
hibernate官网上多的是,我也随便放一个
<?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.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&characterEncoding=UTF-8</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- SQL 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- 在控制台输出sql语句 -->
<property name="show_sql">true</property>
<!-- 在启动时根据配置更新数据库 -->
<property name="hbm2ddl.auto">update</property>
<!-- hibernate加载映射的实体 -->
<mapping class="com.Alibaba.hibernate.entity.StudentEntity"></mapping>
</session-factory>
</hibernate-configuration>
得到session
写一个得到session的类,代码如下
package com.lovo.hibernate.db;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class GetSessionDb {
public static Session sessionOpen() {
SessionFactory sessionFactory=null;
// A SessionFactory is set up once for an application!
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure("hibernate.cfg2.xml") // configures settings from hibernate.cfg.xml
.build();
try {
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
}
catch (Exception e) {
// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
// so destroy it manually.
StandardServiceRegistryBuilder.destroy( registry );
}
return sessionFactory.openSession();
}
}
实体
写实体,注意映射关系,具体看代码,我注解给上
package com.lovo.hibernate.entity;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
@Entity //可以被hibernate管理的实体
@Table(name = "h_student") //映射表 name=表名
public class StudentEntity {
@Id //所有实体必须有@ID注解,这里使用UUID的话需要长度32位,不然报错,不要问我为什么,规定就这样
@Column(length = 32,name = "stu_id") //映射数据库的列
//自定义ID生成策略UUID
@GenericGenerator(name="stuuuid",strategy = "uuid")
// 这里的UUID是上面的
@GeneratedValue(generator = "stuuuid")
private String studentId;
// unique,主键唯一约束
@Column(length = 48,name = "user_name",unique = true)
private String userName;
// nullable是非空约束,默认true
@Column(length = 48,nullable = false)
private String pwd;
private float score;
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
}
最后
去你的test测试就好了。这个东西没有什么难的,注意一下就好了。
加油,程序员