此代码以书本教程代码为基础修改,仅作参考
数据库操作(方便查找问题,需提前建立数据库mysqldb,表会自动生成)
/*创建数据库并打开*/
create database mysqldb;
use javaeedb;
/*创建表格*/
create table user(
user_id int(11),
name varchar(50),
password varchar(12),
type varchar(6),
primary key(USER_ID));
配置文件hibername.cfg.xml,方言只有使用MySQL8Dialect才能自动生成表
<?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="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">bai19991228</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mysqldb?useSSL=false&serverTimezone=UTC</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="connection.pool_size">1</property>
<!-- 配置数据库方言 --><!-- 与上面的hibernate.dialect重复,无影响,我的方言试过MySQL5Dialect,MySQL8Dialect,MySQLDialect,都能用 ,但是使用MySQL8Dialect才能自动生成表-->
<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
<!-- 输出运行时生成的SQL语句-->
<property name="show_sql">true</property>
<!-- 如果不存在数据库表,则自动生成一个 -->
<property name="hbm2ddl.auto">update</property>
<!-- 列出所有的映射文件 -->
<mapping resource="org/hibernate/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
User.java
package org.hibernate.entity;
public class User{
// Fields
private int id;
private String name;
private String password;
private String type;
// Constructors
/** default constructor */
public User() {
}
/** full constructor */
public User(int id, String name, String password, String type) {
this.id = id;
this.name = name;
this.password = password;
this.type = type;
}
// Property accessors
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
}
编写接口UserDAO
package org.hibernate.dao;
import java.util.List;
import org.hibernate.entity.User;
//创建UserDAO接口
public interface UserDAO {
void save(User user); //添加用户
User findById(int id); //根据用户标识查找指定用户
void delete(User user); //删除用户
void update(User user); //修改用户信息
}
实现类UserDAOImpl
package org.hibernate.dao;
import org.hibernate.*;
import org.hibernate.entity.*;
public class UserDAOImpl implements UserDAO {
//添加用户
public void save(User user){
Session session= HibernateUtil.getSession(); //生成Session实例
Transaction tx = session.beginTransaction(); //创建Transaction实例
try{
session.save(user); //使用Session的save方法将持久化对象保存到数据库
tx.commit(); //提交事务
} catch(Exception e){
e.printStackTrace();
tx.rollback(); //回滚事务
}finally{
HibernateUtil. closeSession(); //关闭Session实例
}
}
//根据用户标识查找指定用户
public User findById(int id){
User user=null;
Session session= HibernateUtil.getSession(); //生成Session实例
Transaction tx = session.beginTransaction(); //创建Transaction实例
try{
user=(User)session.get(User.class,id); //使用Session的get方法获取指定id的用户到内存中
tx.commit(); //提交事务
} catch(Exception e){
e.printStackTrace();
tx.rollback(); //回滚事务
}finally{
HibernateUtil. closeSession(); //关闭Session实例
}
return user;
}
//删除用户
public void delete(User user){
Session session= HibernateUtil.getSession(); //生成Session实例
Transaction tx = session.beginTransaction(); //创建Transaction实例
try{
session.delete(user); //使用Session的delete方法将持久化对象删除
tx.commit(); //提交事务
} catch(Exception e){
e.printStackTrace();
tx.rollback(); //回滚事务
}finally{
HibernateUtil. closeSession(); //关闭Session实例
}
}
//修改用户信息
public void update(User user){
Session session= HibernateUtil.getSession(); //生成Session实例
Transaction tx = session.beginTransaction(); //创建Transaction实例
try{
session.update(user); //使用Session的update方法更新持久化对象
tx.commit(); //提交事务
} catch(Exception e){
e.printStackTrace();
tx.rollback(); //回滚事务
}finally{
HibernateUtil. closeSession(); //关闭Session实例
}
}
}
映射文件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="org.hibernate.entity.User" table="USER">
<id name="id" type="java.lang.Integer" column="USER_ID">
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="20"></column>
</property>
<property name="password" type="java.lang.String" >
<column name="PASSWORD" length="12"></column>
</property>
<property name="type" type="java.lang.String" >
<column name="TYPE" length="6"></column>
</property>
</class>
</hibernate-mapping>
测试类UserTest(测试类一般使用JUnit创建比较方便)
package org.hibernate.test;
import org.hibernate.dao.*;
import org.hibernate.entity.User;
import org.junit.Before;
import org.junit.Test;
public class UserTest {
@Before
public void setUp() throws Exception {
}
@Test //test注释表明该方法为一个测试方法
public void testSave() {
UserDAO userdao=new UserDAOImpl();
try{
User u=new User(); //创建User对象
//设置User对象中的各个属性值
u.setId(20);
u.setName("zhangsan");
u.setPassword("456");
u.setType("admin");
userdao.save(u); //使用userdaoimpl的save方法将User对象存入数据库
}catch(Exception e){
e.printStackTrace();
}
}
}
成功运行结果
选择Java控制台查看