Hibernate基础:快速入门(6):修改数据
来源:互联网 发布:生产数据管理制度 编辑:程序博客网 时间:2024/06/08 03:29
数据库的操作增删改查,或者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; }}
使用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 logVersionINFO: 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 configureWARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)十二月 12, 2016 9:27:02 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreatorINFO: 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 buildCreatorINFO: HHH10001001: Connection properties: {user=hbtestuser01, password=****}十二月 12, 2016 9:27:02 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreatorINFO: 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.MySQLDialectSuccessfully 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 stopINFO: 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 logVersionINFO: 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 configureWARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)十二月 12, 2016 9:28:56 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreatorINFO: 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 buildCreatorINFO: HHH10001001: Connection properties: {user=hbtestuser01, password=****}十二月 12, 2016 9:28:56 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreatorINFO: 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.MySQLDialectSuccessfully got connection from session factory.十二月 12, 2016 9:28:57 下午 org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateServiceINFO: HHH000397: Using ASTQueryTranslatorFactoryHibernate: update hbtableuser set username='Michael', country='China' where userid=1001十二月 12, 2016 9:28:57 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stopINFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/hbtestdb?useSSL=false]
0 0
- Hibernate基础:快速入门(6):修改数据
- Hibernate基础:快速入门(4):增加数据
- Hibernate基础:快速入门(5):删除数据
- Hibernate基础:快速入门(7):查询数据
- Struts Spring Hibernate快速入门-Java基础-Java-编程开发
- Hibernate基础:快速入门(1):概要及准备
- Hibernate基础:快速入门(2):JDBC方式访问数据库
- Hibernate基础:快速入门(3):SessionFactory和Session
- Hibernate快速入门
- Hibernate 快速入门
- Hibernate快速入门
- Hibernate快速入门
- Hibernate快速入门
- Hibernate快速入门
- hibernate 快速入门
- Hibernate快速入门
- Hibernate快速入门
- Hibernate快速入门
- java Socket
- MVC模式
- spark core之java和scala电影受众分析系统--用户和年龄性别分布
- iOS的构造方法和实例方法的区分
- 如何通过点击链接触发jenkins构建
- Hibernate基础:快速入门(6):修改数据
- 435_画出小光点
- 前端开发中常见的性能优化
- 第十六周 --项目3-归并排序算法的改进
- 使用caffe训练并且测试一个自己的模型
- 解决在自定义AlertDialog中EditText无法弹出软键盘的问题
- C#实现文件读写
- Kaggle | Allstate Claims Severity比赛总结
- 436_画出圆周运动