Hibernate——简单的增、删、改、查操作
生活随笔
收集整理的這篇文章主要介紹了
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;}@Overridepublic 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();//獲取SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);//獲取sessionSession 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");
轉載于:https://www.cnblogs.com/yzp666/p/7529151.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Hibernate——简单的增、删、改、查操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bwapp之xss(blog)
- 下一篇: python中str的索引、切片