hibernate是较为常用的ORM框架,本专辑将介绍hibernate的使用。这一节将展示如何搭建一个hibernate工程。
1、软件约束
- mysql5.7
- hibernate 5
2、前提约束
- 安装mysql数据库,账号密码为root/zhangli,有一个数据库实例hibernate
3、操作步骤
- 创建普通maven项目,名称为hibernate-core
- 加入依赖
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.6.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
- 在resources文件夹下创建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.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">zhangli</property>
<!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL57Dialect</property>
<!-- 全局 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping resource="net/wanho/user.hbm.xml"/>
</session-factory>
</hibernate-configuration>
- 在resources文件夹下创建net/wanho文件夹,在该文件夹下创建user.hbm.xml:
<?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="net.wanho.User" table="t_user">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"></generator>
</id>
<property name="name" column="name" type="java.lang.String" />
</class>
</hibernate-mapping>
- 在java文件夹下创建net.wanho包
- 在net.wanho包下面创建User.java
package net.wanho;
import java.io.Serializable;
public class User implements Serializable {
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public User() {
}
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
- 在net.wanho包下面创建HibernateMainTest.java
package net.wanho;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class HibernateMainTest {
public static void main(String[] args) {
Configuration config = new Configuration();
config.configure("hibernate.cfg.xml");
SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();
Transaction tran = session.beginTransaction();
User user0 = new User();
user0.setName("ali");
session.save(user0);
tran.commit();
// 查询方式1
User user = (User) session.get(User.class, 1);
System.out.println(user);
/**
* 查询方式2
*/
Query query = session.createQuery(" from User ");
List ls = query.list();
System.out.println(ls.get(0));
/**
* 查询方式3
*/
User user1 = session.load(User.class,1);
System.out.println(user1);
/**
* 查询方式4
*/
SQLQuery sqlQuery = session.createSQLQuery("select * from t_user where id=1").addEntity(User.class);
User user2 = (User) sqlQuery.uniqueResult();
System.out.println(user2);
factory.close();
}
}
以上就是hibernate的搭建和入门程序。
网友评论