hibernate课程 初探单表映射3-5 hibernate增删改查
本节简介:
1 增删改查写法
2 查询load和查询get方法的区别
3 demo
1 增删改查写法
增加 session.save()
修改 session.update()
删除 session.delete()
查询 session.get()/session.load()
2 查询load和查询get方法的区别
a get 不考虑缓存情况下,get会在调用之后立即向数据库发送sql语句,返回持久化对象。load 是调用后返回代理对象,该代理对象只保存实体对象id,只有使用对象非主键属性才发出sql语句。
b 查询不存在的数据,get返回null,load抛出异常。
3 demo
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/bendi</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource = "Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-12-20 0:42:12 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.ddwei.student.Student" table="STUDENT">
<id name="pid" type="int">
<column name="PID" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="sex" type="java.lang.String">
<column name="SEX" />
</property>
<property name="birthday" type="date">
<!-- <property name="birthday" type="time"> -->
<!-- <property name="birthday" type="timestamp"> -->
<column name="BIRTHDAY" />
</property>
<!-- <property name="address" type="java.lang.String"> -->
<!-- <column name="ADDRESS" /> -->
<!-- </property> -->
<property name="picture" type="java.sql.Blob">
<column name="Picture" />
</property>
<component name="address" class="com.ddwei.student.Address">
<property name="postcode" column="POSTCODE"/>
<property name="phone" column="PHONE"/>
<property name="address" column="ADDRESS"/>
</component>
</class>
</hibernate-mapping>
StudentTest.java
package hibernate_001;
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.Blob; import java.sql.SQLException; import java.util.Date;
import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test;
import com.ddwei.student.Address; import com.ddwei.student.Student;
public class StudentTest { private SessionFactory sessionFactory; private Session session; private Transaction trasaction; @Test public void testSaveStudent(){ // Student student =new Student(1,"周恩来","男",new Date(),"绍兴");//创建学生对象 Student student = new Student(); student.setName("秦始皇"); student.setSex("男"); student.setBirthday(new Date()); // student.setAddress("阿房宫"); Address address = new Address("105789","18912345678","河北敦煌教区"); student.setAddress(address); session.save(student);//会话保存学生对象进入数据库 } @Test //测试修改记录 public void testUpdate(){ Student student = (Student) session.get(Student.class, 1); student.setBirthday(new Date()); session.update(student); } @Test //测试查询记录 public void testLoad(){ Student student = (Student) session.load(Student.class, 1); String aaa = student.getName(); System.out.println("student1="+aaa); } @Test //测试查询记录 public void testGet(){ Student student = (Student) session.get(Student.class, 1); System.out.println("student2="+student);
} @Test //测试删除记录 public void testDelete(){ Student student = (Student) session.get(Student.class, 1); session.delete(student); } @Before public void init(){ //1 创建配置对象 Configuration config = new Configuration().configure(); //2 创建服务对象 ServiceRegistry serviceRe = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); //3 创建会话工厂 sessionFactory = config.buildSessionFactory(serviceRe); //4 打开会话 session = sessionFactory.openSession(); //5 创建事务 trasaction = session.beginTransaction(); } @After public void destroy(){ trasaction.commit(); session.close(); sessionFactory.close(); }
}
Student.java
package com.ddwei.student;
import java.sql.Blob; import java.util.Date;
public class Student {
// java beans 的设计原则 /** * 1 公有的类 2 共有不带参数构造方法 3 私有属性 4 属性setter/getter方法 */
private int pid;// 学号 private String name;// 姓名 private String sex;// 性别 private Date birthday;// 出生日期 private Address address;// 家庭地址 private Blob picture;//相片
public Student() {
}
public Student(int pid, String name, String sex, Date birthday) { // super(); this.pid = pid; this.name = name; this.sex = sex; this.birthday = birthday; // this.address = address; }
@Override public String toString() { return "Student [pid=" + pid + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address + "]"; }
public int getPid() { return pid; }
public void setPid(int pid) { this.pid = pid; }
public Date getBirthday() { return birthday; }
public void setBirthday(Date birthday) { this.birthday = birthday; }
public Address getAddress() { return address; }
public void setAddress(Address address) { this.address = address; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getSex() { return sex; }
public void setSex(String sex) { this.sex = sex; }
public Blob getPicture() { return picture; }
public void setPicture(Blob picture) { this.picture = picture; }
}
Address.java
package com.ddwei.student;
//地址类 public class Address { private String postcode;//邮编 private String phone;//电话 private String address;//地址 public String getPostcode() { return postcode; } public void setPostcode(String postcode) { this.postcode = postcode; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } //无参构造方法 public Address(){ } //有参构造方法 public Address(String postcode, String phone, String address) { super(); this.postcode = postcode; this.phone = phone; this.address = address; } }
hibernate课程 初探单表映射3-5 hibernate增删改查的更多相关文章
- hibernate课程 初探单表映射1-11 通过hibernate API访问编写第一个小例子
hibernate 业务流程 1 创建配置对象 Configuration config = new Configuration().configure(); 2 创建服务注册对象 Service ...
- hibernate课程 初探单表映射2-3 session简介
hibernate流程: 1 配置对象Configurateion 读取 hibernate.cfg.xml 2 会话工厂SessionFactory 读取 user.hbm.xml(创建销毁相当耗费 ...
- hibernate课程 初探单表映射4-1 课程总结
ORM是一种面向对象编程的方法,用这种方法来避免写数据库底层语言sql语句,这样有利于java的跨平台,扩展.维护.而hirenate是ORM的一种框架 hirbernate开发基本步骤编写配置文档h ...
- hibernate课程 初探单表映射3-1 hibernate单表操作简介
本章简介: 1 单一主键 2 基本类型 3 对象类型 4 组件属性 5 单表操作CRUD实例
- hibernate课程 初探单表映射1-3 hibernate简介
1 hibernate定义: Java领域一项开源的orm框架技术: hibernate对jdbc进行轻量级的封装. hibernate 作为持久层存在.就是通过对象关系映射把项目中的对象持久化到数据 ...
- hibernate课程 初探单表映射1-1 第一章
本章内容: 1 什么是orm 2 hibernate简介 3 编写第一个hibernate小例子
- hibernate课程 初探单表映射1-9 创建关系映射文件
创建关系映射文件:(把实体类映射成一个表) 1 右键src==>new==>other==>hibernate==>hbm.xml==>Student==>Fini ...
- hibernate课程 初探单表映射3-3 对象类型
本节简介: 1 简介对象类型(重点是音视频blob类型) 2 demo(对图片的写入数据库与读取) 1 简介对象类型 映射类型 java类型 标准sql类型 mysql类型 oracle类型 bina ...
- hibernate课程 初探单表映射2-7 hbm配置文件常用设置
本节主要简介hbm配置文件以下内容: 1 mapping标签 2 class标签 3 id标签 1 hibbernate-mapping标签 schema 模式名称 catalog 目录名称 defa ...
随机推荐
- last 列出登入系统的用户相关信息
Linux last 命令介绍 功能说明:列出目前与过去登入系统的用户相关信息. 语法: last [-adRx][-f <记录文件>][-n <显示列数>][帐号名称. ...
- service的生命周期以及两种service的差异
可以看到,两种service的生命周期都相对简单,有一点不同的是,Intentservice每次调用的时候都执行onstartcommand,而boundservice一旦启动了之后,就不会每次执行o ...
- mysql nginx redis 配置文件
https://github.com/superhj1987/awesome-config
- 【总结整理】JavaScript的DOM事件学习(慕课网)
事件:在文档或者浏览器窗口中发生的一些,特定的交互瞬间 HTML和JavaScript的交互通过事件 来实现 比如:1.滚动条向下滑动,加载图片 2.图片轮播,鼠标由2-5页调换 本章内容1.理解事件 ...
- ARC097C K-th Substring
传送门 题目 You are given a string s. Among the different substrings of s, print the K-th lexicographical ...
- centos7命令行和图形界面的相互切换(附centos7安装配置教程)
一.最近安装了centos7,发现在命令行和图形界面的相互切换命令上,与centos以往版本有很大不同,先整理如下,加深记忆. 1,centos7默认安装后,跟其他版本一样,启动默认进入图形界面: 2 ...
- Entity Framework Code-First(7):Inheritance Strategy
Inheritance Strategy in Code-First: We have seen in the Code First Conventions section that it creat ...
- Entity Framework Code-First(1):Introduction
Entity Framework Code-First: Learn Entity Framework Code-First in simple step-by-step tutorials. The ...
- CSS学习系列2 -- CSS中的清除浮动
CSS中有一个很常见的问题,就是元素的浮动. 那么,到底什么是元素的浮动呢,我们来看一个例子 举个例子,在一个div里面内部有浮动元素的话,这个浮动元素会让这个div的高度塌陷. .myDiv{ ba ...
- centos运行netcore error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.
Error: Another program is already listening on a port that one of our HTTP servers is configured to ...