Hibernate基礎:快速入門(6):修改資料
資料庫的操作增刪改查,或者CRUD是最常見的操作。這篇文章中介紹修改資料的方法
hibernate.cfg.xml
建立如下所示的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 >
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hbtestdb?useSSL=false</property >
<property name="hibernate.connection.username">hbtestuser01</property>
<property name="hibernate.connection.password">hbtestuser01</property>
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property >
</session-factory>
</hibernate-configuration>
Entity Class:User
我們將會建立一個Entity的User類。Entity類就是用來與資料庫的Table進行Mapping的Class。
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
@Entity
@Table (name="hbtableuser")
public class User {
@Id
@Column(name="userid")
private int userid;
@Column(name="username")
private String username;
@Column(name="country")
private String country;
public User(){
System.out.println("Default User construction is called...");
}
public User(int userid, String username, String country) {
this.userid = userid;
this.username = username;
this.country = country;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
型別 | 註解 | import | 作用 |
---|---|---|---|
類註解 | @Entity | javax.persistence.Entity | 類註解用 |
類註解 | @Table | javax.persistence.Table | 類註解用:@Table (name=”Table名”) |
屬性註解 | @Id | javax.persistence.Id | 主碼註解 |
屬性註解 | @Column | javax.persistence.Column | 所有欄位都需要 |
使用xml檔案自然也可以,由於註解可能是今後的標準方向,相關的例子都回使用註解方式。
事前確認
建立Demo演示程式碼
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class HbDemo04 {
public static void main(String[] args){
try{
SessionFactory sessionFactory = new org.hibernate.cfg.Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(User.class)
.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
System.out.println("Successfully got connection from session factory.");
session.beginTransaction();
User user = session.get(User.class,1001);
user.setUsername("liumiao");
user.setCountry("PRC");
session.getTransaction().commit();
sessionFactory.close();
}catch (Exception e){
System.out.println("Exception happened...");
e.printStackTrace();
}
}
}
執行結果
十二月 12, 2016 9:27:02 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.5.Final}
十二月 12, 2016 9:27:02 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
十二月 12, 2016 9:27:02 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
十二月 12, 2016 9:27:02 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
十二月 12, 2016 9:27:02 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hbtestdb?useSSL=false]
十二月 12, 2016 9:27:02 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=hbtestuser01, password=****}
十二月 12, 2016 9:27:02 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
十二月 12, 2016 9:27:02 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
十二月 12, 2016 9:27:03 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Successfully got connection from session factory.
Hibernate: select user0_.userid as userid1_0_0_, user0_.country as country2_0_0_, user0_.username as username3_0_0_ from hbtableuser user0_ where user0_.userid=?
Default User construction is called...
Hibernate: update hbtableuser set country=?, username=? where userid=?
十二月 12, 2016 9:27:03 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/hbtestdb?useSSL=false]
建立Demo演示程式碼
刪除和更新還可以使用另外一種方法來進行。程式碼如下
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class HbDemo04 {
public static void main(String[] args){
try{
SessionFactory sessionFactory = new org.hibernate.cfg.Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(User.class)
.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
System.out.println("Successfully got connection from session factory.");
session.beginTransaction();
session.createQuery("update User set username='Michael' , country='China' where userid=1001 ").executeUpdate();
session.getTransaction().commit();
sessionFactory.close();
}catch (Exception e){
System.out.println("Exception happened...");
e.printStackTrace();
}
}
}
注意update語句的Table名稱
執行結果
十二月 12, 2016 9:28:56 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.5.Final}
十二月 12, 2016 9:28:56 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
十二月 12, 2016 9:28:56 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
十二月 12, 2016 9:28:56 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
十二月 12, 2016 9:28:56 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hbtestdb?useSSL=false]
十二月 12, 2016 9:28:56 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=hbtestuser01, password=****}
十二月 12, 2016 9:28:56 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
十二月 12, 2016 9:28:56 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
十二月 12, 2016 9:28:57 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Successfully got connection from session factory.
十二月 12, 2016 9:28:57 下午 org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: update hbtableuser set username='Michael', country='China' where userid=1001
十二月 12, 2016 9:28:57 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/hbtestdb?useSSL=false]
相關推薦
Hibernate基礎:快速入門(6):修改資料
資料庫的操作增刪改查,或者CRUD是最常見的操作。這篇文章中介紹修改資料的方法 hibernate.cfg.xml 建立如下所示的hibernate的設定檔案 <?xml version="1.0" encoding="utf
Spring基礎:快速入門spring boot(7):spring boot 2.0簡單介紹
從這篇文章開始以spring boot2為主要版本進行使用介紹。 Spring boot 2特性 spring boot2在如下的部分有所變化和增強,相關特性在後續逐步展開。 特性增強 基礎元件升級: JDK1.8+ tomcat 8+ Thymeleaf 3
Spring基礎:快速入門spring boot(4):使用slf4j輸出日誌
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
Spring基礎:快速入門spring boot(2):SPRING INITIALIZR
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
Spring基礎:快速入門spring cloud(1):Spring Cloud介紹
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
Spring基礎:快速入門spring cloud(2):服務發現之eureka
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
Spring基礎:快速入門spring cloud(4):API閘道器之Zuul
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
十五分鐘快速入門系列:Python基礎
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
Spring基礎:快速入門spring boot(10):spring boot + sonarqube +jacoco
上篇文章我們瞭解到瞭如何使用SonarQube對建立的SpringBoot的應用進行分析,這篇文章來接著確認一些如何視覺化地確認測試覆蓋率。 SpringBootTest 需要測試覆蓋率,自然,在此之前需要有測試用例,在前面的例子中已經簡單講述了在SpringBoot應用中進行
Spring基礎:快速入門spring boot(9):使用sonarqube來檢查技術債務
作為程式碼質量檢查的流行工具,比如Sonarqube能夠檢查程式碼的“七宗罪”,跟程式碼結合起來能夠更好地提高程式碼的質量,讓我們來看一下,剛剛寫的Springboot2的HelloWorld的程式碼有什麼“罪”。 Sonarqube Sonarqube可以使用docker
Spring基礎:快速入門spring boot(8):使用Junit進行測試
使用Junit或者TestNG可以進行單體測試,這篇文章簡單說明一下如何在Spring boot的專案中使用Junit進行單體測試。 pom設定 pom中需要新增spring-boot-starter-test <dependency> <g
Spring基礎:快速入門spring cloud):API閘道器之Zuul
Spring Cloud是Spring總多的Project中的一個,它提供了一整套的工具幫助系統架構師們在進行分散式設計的時候可以拿來即用, 在建立和釋出微服務時極為便捷和有效。 本系列文章將會使用最簡單的例子和最為容易的方式來學習Spring Cloud。本文將會介紹如何引入Zuul在
Python 快速入門知識點:基礎語法!
Python 是一種高層次的結合瞭解釋性、編譯性、互動性和麵向物件的指令碼語言。Python 由 Guido van Rossum 於 1989 年底在荷蘭國家數學和電腦科學研究所發明,第一個公開發行版發行於 1991 年。 ''' 作者:韭白 源自: https://www.cnblogs.c
Spring基礎:快速入門spring(1):基礎概念
作為流行了10年以上的老將,spring依然精神矍鑠,影響不減。本文將對spring很基礎的概念進行介紹以及為學習spring最核心和基礎的知識作環境搭建的準備。 Spring官網 簡介 Spring為JAVA企業級應用提供了
Spring Boot 2.x基礎教程:快速入門
開發十年,就只剩下這套架構體系了! >>>
day39-Spring 12-Spring的JDBC模板:快速入門
pri 哪些 困難 ces 5.0 使用 只需要 common commons Spring AOP的關鍵是它的底層的原理和思想,配置和使用並不是十分困難.AOP本身就是一個思想,是面向對象的延伸,不是用來替換面向對象的,而是用來解決面向對象中的一些問題的.在最初的時候提出
AndroidStudio快速入門四:打造你的開發工具,settings必備
ack src paste libs box 下載 解釋 繼續 odi http://blog.csdn.net/jf_1994/article/details/50085825 前言:這裏是使用AS的基本設置,適合新入手的朋友閱讀,將這裏介紹的設置完基本使用無憂啦。 1、
EJB:快速入門
man gpo logic socket通信 pub 說明 失去 動態 服務端 1、EJB概念 2、EJB體系結構 3、SessionBean 3.1 SessionBean 服務端組件 3.2 Remote 與 Local 模式 3.3 Cl
ubuntu快速入門教程:初次見面
切換 優化 第三方軟件 技術分享 計算 shu 做的 基本 移動設備 1 什麽是ubuntu? Ubuntu(友幫拓、優般圖、烏班圖)是一個以桌面應用為主的開源GNU/Linux操作系統,Ubuntu 是基於Debian GNU/Linux,支持x86、amd64(即x6
springboot(一):快速入門
pom.xml www. 負責 格式 一個 tools 實體 簡化 mvc 麽是spring boot Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。 使用spring boot有什麽