1.首先在项目的src目录下编写数据库连接配置文件
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!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>
<!-- 加载驱动类 ,注意要导包mysql-connector-java-5.1.45-bin.jar-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 配置数据库连接URL-->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/new_houserent</property>
<!-- 数据库连接用户名-->
<property name="hibernate.connection.username">root</property>
<!-- 数据库用户名的密码-->
<property name="hibernate.connection.password">123456</property>
<!-- 配置数据库方言-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property>
<!--配置数据库表自动生成-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置显示SQL语句-->
<property name="hibernate.show_sql">true</property>
<!-- 配置显示SQL语句-->
<property name="hibernate.format_sql">true</property>
<!--引入持久化类的映射,让程序能够找到资源 写资源的全路径-->
<mapping resource="com/ibeifeng/hibernate/moduels/User.hbm.xml"/>
<!--写完之后要检查是否能跳到该文件,证明映射是成功的 Ctrl+鼠标左键-->
</session-factory>
</hibernate-configuration>
2.编写持久化类
package com.ibeifeng.hibernate.moduels;
public class User {
private int id;
private String name;
private String password;
private String telephone;
private String username;
private String isAdmin; //Y | N
public User(int id, String name, String username) {
this.id = id;
this.name = name;
this.username = username;
}
public User(){}
public User(int id, String name, String password, String telephone, String username, String isAdmin) {
this.id = id;
this.name = name;
this.password = password;
this.telephone = telephone;
this.username = username;
this.isAdmin = isAdmin;
}
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getIsAdmin() {
return isAdmin;
}
public void setIsAdmin(String isAdmin) {
this.isAdmin = isAdmin;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", telephone='" + telephone + '\'' +
", username='" + username + '\'' +
", isAdmin='" + isAdmin + '\'' +
'}';
}
}
3.编写持久化类的对应数据库的mapping映射(文件编写完成后要在配置文件里指定mapping映射, 第一步的配置文件里有)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 需要映射的类全名 ORM对象映射关系-->
<class name="com.ibeifeng.hibernate.moduels.User" table="t_user">
<id name="id" type="int" >
<!--主键生成策略-->
<generator class="increment"/>
</id>
<property name="name" column="name"/>
<property name="password" column="password"/>
<property name="telephone" column="telephone"/>
<property name="username" column="username"/>
<!--其他属性,可以在这里设置类对应的属性在数据中的列名-->
<property name="isAdmin" column="is_admin"/>
<!---->
</class>
</hibernate-mapping>
4.完成之后,进行的基本测试
package test;
import com.ibeifeng.hibernate.moduels.User;
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;
import org.hibernate.service.Service;
import org.hibernate.service.ServiceRegistry;
import org.junit.Test;
public class HibernateTest {
@Test
public void test(){
System.out.println("测试Junit");
}
@Test
public void testBase(){
SessionFactory sessionFactory = null;
Session session = null;
//注册标准服务
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
try {
//创建SessionFactory
sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
//通过SessionFactory创建session
session = sessionFactory.openSession();
//开始事物
session.beginTransaction();
//创建User对象
User user = new User(2,"Aobama","123456","12345678910","aoao","no");
//保存用户
session.save(user);
//提交事物
session.getTransaction().commit();
}catch (Exception e){
e.printStackTrace();
//回滚事物
session.getTransaction().rollback();
//销毁标准服务
StandardServiceRegistryBuilder.destroy(registry);
}finally {
//释放资源
if(session != null ) session.close();
}
}
5.编写常用工具类 -getSession
package util;
/**
* hibernate util
* get session
*/
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 HibernateUtil {
private static SessionFactory factory = null ;
static{
//注册标准服务
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
//创建SessionFactory
factory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
}
//获取Session
public static Session getSession(){
return factory.openSession();
}
//销毁Session
public static void close(Session session){
if(session != null) session.close();
}
}
6.基本的增删改查
package test;
import com.ibeifeng.hibernate.moduels.User;
import org.hibernate.Session;
import org.junit.Test;
import util.HibernateUtil;
import java.util.List;
public class TestCRUD {
@Test
public void testAdd(){
Session session = null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
User user = new User(0,"yase","123456","11236885412","sese","yes");
session.save(user);
session.getTransaction().commit();
}catch (Exception e){
session.getTransaction().rollback();
e.printStackTrace();
}finally {
HibernateUtil.close(session);
}
}
@Test
public void testUpdate(){
Session session = null;
try {
session = HibernateUtil.getSession();
session.beginTransaction();
User user = new User();
user.setId(1);
user.setName("pujing");
session.update(user);
session.getTransaction().commit();
}catch (Exception e){
session.getTransaction().rollback();
}finally {
HibernateUtil.close(session);
}
}
@Test
public void testDel(){
Session session = null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
User user = new User();
user.setId(1);
session.delete(user);
session.getTransaction().commit();
}catch (Exception e){
session.getTransaction().rollback();
e.printStackTrace();
}finally {
HibernateUtil.close(session);
}
}
@Test
public void testLoad(){
Session session = null;
try{
session = HibernateUtil.getSession();
User user = session.get(User.class,5);
System.out.println(user);
}catch (Exception e){
e.printStackTrace();
}finally {
HibernateUtil.close(session);
}
}
@Test
public void testList(){
Session session = null ;
try{
session = HibernateUtil.getSession();
List<User> users = session.createQuery("from User").list();
for (User user : users
) {
System.out.println(user);
}
}catch(Exception e){
e.printStackTrace();
}finally {
HibernateUtil.close(session);
}
}
}