【Hibernate篇】Hibernate实现基本增删改成功能
程序员文章站
2022-06-12 19:18:13
...
微信公众号:程序yuan
关注可了解更多的教程。问题或建议,请公众号留言;
使用Hibernate实现基本的增删改查
项目结构图
实体类
package com.ooyhao.domain;
import javax.persistence.*;
import java.io.Serializable;
/**
* Author : 阳浩
* Date : 2018/8/30
* Time : 16:49
* Annotation : 客户持久化类
*/
@Entity
@Table(name = "cst_customer")
public class Customer implements Serializable {
@Id
@GeneratedValue
@Column(name = "cust_id")
private Long custId;
@Column(name = "cust_name")
private String custName;
@Column(name = "cust_source")
private String custSource;
@Column(name = "cust_industry")
private String custIndustry;
@Column(name = "cust_level")
private String custLevel;
@Column(name = "cust_phone")
private String custPhone;
@Column(name = "cust_mobile")
private String custMobile;
public Customer() {
}
public Customer(String custName, String custSource, String custIndustry, String custLevel, String custPhone, String custMobile) {
this.custName = custName;
this.custSource = custSource;
this.custIndustry = custIndustry;
this.custLevel = custLevel;
this.custPhone = custPhone;
this.custMobile = custMobile;
}
public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustSource() {
return custSource;
}
public void setCustSource(String custSource) {
this.custSource = custSource;
}
public String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
public String getCustMobile() {
return custMobile;
}
public void setCustMobile(String custMobile) {
this.custMobile = custMobile;
}
@Override
public String toString() {
return "Customer{" +
"custId=" + custId +
", custName='" + custName + '\'' +
", custSource='" + custSource + '\'' +
", custIndustry='" + custIndustry + '\'' +
", custLevel='" + custLevel + '\'' +
", custPhone='" + custPhone + '\'' +
", custMobile='" + custMobile + '\'' +
'}';
}
}
Hibernate工具类
package com.ooyhao.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Author : 阳浩
* Date : 2018/8/30
* Time : 17:12
* Annotation : Hibernate工具类
*/
public class HibernateUtils {
private static SessionFactory sessionFactory = null;
static {
Configuration configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
}
/**
* 获得一个全新的session
* @return
*/
public static Session openSession(){
return sessionFactory.openSession();
}
/**
* 获得一个当前线程的session
* @return
*/
public static Session getCurrentSession(){
return sessionFactory.getCurrentSession();
}
}
Hibernate主配置文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!--主配置文件-->
<hibernate-configuration>
<session-factory>
<!--jdbc驱动-->
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!--数据库连接URL-->
<property name="hibernate.connection.url">
jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF8
</property>
<!--数据库连接用户名-->
<property name="hibernate.connection.username">
root
</property>
<!--数据库连接密码-->
<property name="hibernate.connection.password">
root
</property>
<!--设置方言-->
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!--显示sql输出至控制台-->
<property name="hibernate.show_sql">
true
</property>
<!--格式化sql-->
<property name="hibernate.format_sql">
true
</property>
<property name="hibernate.hbm2ddl.auto">
update
</property>
<!--如实体类不使用注释-->
<!--<mapping resource="com/ooyhao/domain/Customer.hbm.xml"/>-->
<!--实体类使用注释-->
<mapping class="com.ooyhao.domain.Customer"></mapping>
</session-factory>
</hibernate-configuration>
测试类
import com.ooyhao.domain.Customer;
import com.ooyhao.utils.HibernateUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import java.util.List;
/**
* Author : 阳浩
* Date : 2018/8/30
* Time : 16:44
* Annotation :
*/
public class HibernateTest {
//查询
@Test
public void testQuery(){
Session session = HibernateUtils.openSession();
Query query = session.createQuery("from Customer");
List<Customer> list = query.list();
for(Customer customer : list){
System.out.println(customer);
}
session.close();
}
//保存
@Test
public void testSave(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
Customer customer = new Customer();
customer.setCustName("林黛玉");
customer.setCustSource("红楼梦");
customer.setCustIndustry("大观园");
customer.setCustLevel("2");
customer.setCustMobile("11223344");
customer.setCustPhone("22331122");
session.save(customer);
transaction.commit();
session.close();
}
//更新
@Test
public void testUpdate(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
//根据id更新数据
Customer customer = session.get(Customer.class, 15L);
customer.setCustName("齐天大圣");
session.update(customer);
transaction.commit();
session.close();
}
//删除
@Test
public void testDelete(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
Customer customer = session.get(Customer.class, 15L);
session.delete(customer);
transaction.commit();
session.close();
}
}
如果实体类不使用注解的话,就需要加上下面的配置文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--配置表和实体对象的关系-->
<!--package属性:填写一个报名,在元素内部凡是需要书写完整类名的属性,可以直接写简单类名了-->
<hibernate-mapping package="com.ooyhao.domain">
<!--建立类和表的一个映射关系-->
<!--
class 标签:用来建立类和表的映射
name属性:类的全路径
table属性:表名(如果类名和表名是一致的,那么table属性可以忽略)
catalog属性:数据库名称,可以忽略
-->
<!--由于上面mapping配置了package,这里就可以简写了-->
<class name="Customer" table="cst_customer">
<!--建立类中的属性与表中的主键字段对应-->
<!--
id标签:用来建立类中的属性(id)和表中的主键字段对应
name属性:类中的属性名
column属性(可选):表中字段名(如果类的属性名与表中的字段名一致,那么省略column)列名默认会使用属性名
length属性(可选):字段的长度
type属性(可选):类型。写Java的数据类型,Hibernate数据类型(默认),SQL类型
not-null属性(可选):设置指定属性能否为空
-->
<id name="cust_id" column="cust_id">
<!--主键生成策略-->
<generator class="native"/>
</id>
<!--建立类中的普通属性与表中字段的映射
property标签:用来建立类中的普通属性(除id之外)与表中的字段对应
name属性:类中的属性名
column属性(可选):表中字段名(如果类中的属性名和表中的字段名一致,那么省略column)
length属性(可选):字段的长度
type属性(可选):类型,写java数据类型 ,Hibernate属性类型(默认),SQL类型
not-null属性(可选):设置指定属性能否为空
-->
<property name="cust_name" column="cust_name"/>
<property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
</class>
</hibernate-mapping>
------------------------------------------------
关注小编微信公众号获取更多资源