Hibernate 系列教程3-单表操作

时间:2024-09-30 20:06:32

工程截图

Hibernate 系列教程3-单表操作

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>
<!-- 必须配置的属性 -->
<!-- 连接数据库信息 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<!-- 方言属性:这个是实现跨数据库关键类 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 可选配置属性 -->
<!-- 是否自动生成表:这里选用create便于测试 -->
<property name="hbm2ddl.auto">create</property>
<!-- 是否显示sql -->
<property name="show_sql">true</property>
<!-- 是否格式化sql -->
<!-- <property name="format_sql">true</property> --> <!-- 以文件路径的方式加载映射文件 -->
<mapping resource="com/jege/hibernate/single/table/User.hbm.xml" />
</session-factory>
</hibernate-configuration>

User.hbm.xml

<?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 package="com.jege.hibernate.single.table">
<!-- class表示由hibernate管理的一个持久对象,对应数据库的一个表 ,table表示数据库的表名 -->
<class name="User" table="t_user">
<!-- id元素是必须的,对应数据库是表的主键 -->
<id name="id">
<!-- generator表示数据库表的主键生成方式,一般都是使用native -->
<!-- native表示自动选择数据库本地的策略: -->
<!-- 对于mysql是AUTO_INCREMENT自增 -->
<!-- 对于oracle是序列 -->
<generator class="native" />
</id>
<!-- 使用property配置非主键属性 -->
<!-- 可以column配置数据库表的不同于模型User的属性,如下username属性对应表的name字段 -->
<property name="username" column="name" />
<property name="password" />
</class>
</hibernate-mapping>

User.java

package com.jege.hibernate.single.table;

/**
* @author JE哥
* @email 1272434821@qq.com
* @description: 模型对象
*/
public class User {
private Long id;
private String username;
private String password; public User() { } public User(Long id) {
this.id = id;
} public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} @Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
} }

CrudTest

package com.jege.hibernate.single.table;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Before;
import org.junit.Test; import com.jege.hibernate.util.HibernateUtils; /**
* @author JE哥
* @email 1272434821@qq.com
* @description: crud测试类
*/
public class CrudTest { // 在每个@Test之前自动执行,先保存数据
@Before
public void before() throws Exception {
User user = new User();
user.setUsername("jege");
user.setPassword("1272434821"); Session session = HibernateUtils.INSTANCE.getSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
session.close();
} @Test
public void save() throws Exception { } @Test
public void update() throws Exception {
Session session = HibernateUtils.INSTANCE.getSession();
session.beginTransaction(); User user = (User) session.get(User.class, 1L);
user.setUsername("JavaEEGe");
user.setPassword("1272434821@qq.com"); session.update(user);
session.getTransaction().commit();
session.close();
} @Test
public void delete() throws Exception { Session session = HibernateUtils.INSTANCE.getSession();
session.beginTransaction(); User user = (User) session.get(User.class, 1L);
if (user != null) {
session.delete(user);
} session.getTransaction().commit();
session.close();
} @Test
public void get() throws Exception {
Session session = HibernateUtils.INSTANCE.getSession();
User user = (User) session.get(User.class, 1L);
System.out.println(user);
session.close();
} @Test
public void getAll() throws Exception {
Session session = HibernateUtils.INSTANCE.getSession();
Query query = session.createQuery("select u from User u");
List<User> list = query.list();
for (User user : list) {
System.out.println(user);
}
session.close();
} }

源码地址

https://github.com/je-ge/hibernate

如果觉得我的文章对您有帮助,请予以打赏。您的支持将鼓励我继续创作!谢谢!

Hibernate 系列教程3-单表操作

Hibernate 系列教程3-单表操作