Hibernate——简单的增删改查操作
Posted 卖萌儿的小二哥丶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate——简单的增删改查操作相关的知识,希望对你有一定的参考价值。
思路:
1.导入hibernate jar包
2.导入mysql jar包
3.创建数据库
4.创建java实体类
5.编写hibernate.cfg.xml配置文件
6.编写dao类
目录:
数据表:
实体类:
package online.shixun.model; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; /** * 实体类 * @author Tom * */ @Entity @Table(name = "commodity") public class Commodity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY)//主键自增 private Integer id; @Column(name = "createDate") private Date createDate;//创建日期 @Column(name = "described") private String describe;//商品描述 @Column(name = "modifyDate") private Date modifyDate;//修改日期 @Column(name = "name") private String name;//商品名称 //构造函数 public Commodity(){ } //带参构造函数 public Commodity(Date createDate,String describe,Date modifyDate,String name){ this.createDate = createDate; this.describe = describe; this.modifyDate = modifyDate; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public String getDescribe() { return describe; } public void setDescribe(String describe) { this.describe = describe; } public Date getModifyDate() { return modifyDate; } public void setModifyDate(Date modifyDate) { this.modifyDate = modifyDate; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Commodity [id=" + id + ", createDate=" + createDate + ", describe=" + describe + ", modifyDate=" + modifyDate + ", name=" + name + "]"; } }
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?> <!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> <!-- 数据库连接四项driverclass,url,username,password --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/qsx_shop?characterEncoding=UTF-8</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!-- Hibernate方言,Hibernateh会根据配置的方言产生对应的sql语句 --> <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> <!-- 配置数据实体类 --> <mapping class="online.shixun.model.Commodity" /> </session-factory> </hibernate-configuration>
DAO:
package online.shixun.test; //import java.sql.Date; import java.util.Date; import java.text.ParseException; import java.text.SimpleDateFormat; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import online.shixun.model.Commodity; public class HibernateDemo { public static void main(String[] args) throws ParseException { //获取配置文件 Configuration configuration = new Configuration().configure(); //注册服务对象 StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder(); StandardServiceRegistry serviceRegistry = serviceRegistryBuilder.applySettings(configuration.getProperties()).build(); //获取SessionFactory SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); //获取session Session session = sessionFactory.openSession(); //开启事务 session.beginTransaction(); //************开始操作*************// //1. 利用 get 方法获取 id 为 1 的用户数据 Commodity getCommodity = (Commodity) session.get(Commodity.class, 1); System.out.println("创建日期:" + getCommodity.getCreateDate() + "描述:" + getCommodity.getDescribe() + "修改日期:" + getCommodity.getModifyDate() + "名称:" + getCommodity.getName()); // Date date = new Date(0); String stringdate = "2019-05-07"; Date date = new SimpleDateFormat("YYYY-MM-DD").parse(stringdate); //2.增加商品 // Commodity newCommodity = new Commodity(date,"电器1",date,"电饭锅2"); // session.save(newCommodity); //3.删 // Commodity getCommodity1 = (Commodity) session.load(Commodity.class, 2); // session.delete(getCommodity1); // session.getTransaction().commit(); //4.改 // Commodity getCommodity2 = (Commodity) session.get(Commodity.class, 1); // getCommodity2.setName("洗脸盆"); // session.update(getCommodity2); // session.getTransaction().commit(); //************结束操作*************// //提交事务 session.getTransaction(); //关闭session session.close(); //关闭SessionFactory sessionFactory.close(); } }
Hibernate 配置文件有以下两种加载方式,如下:
//利用以下语句加载默认的 Hibernate 配置文件(名称:hibernate.cfg.xml)
Configuration configuration = new Configuration().configure();
//如果要自定义 Hibernate 配置文件名称,需要利用以下语句进行配置文件加载
Configuration configuration = new Configuration().configure("my-hibernate.cfg.xml");
以上是关于Hibernate——简单的增删改查操作的主要内容,如果未能解决你的问题,请参考以下文章