【java框架】0212笔记——Hibernate学习(一)
2018-02-12 22:48
537 查看
一、框架初步
1.创建数据库和表create database test;
use test;
CREATE TABLE product_ (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(30) ,
price float ,
PRIMARY KEY (id)
) DEFAULT CHARSET=UTF8;
2.创建所需的pojo实体类:
public class Product {
int id;
String name;
float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
3.配置Product.hbm.xml(hibernate所需的配置文件)
1)在包pojo下 新建一个配置文件Product.hbm.xml, 用于映射Product类对应数据库中的product_表(文件名 Product.hbm.xml P一定要大写,要和类保持一致)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="pojo">
<class name="Product" table="product_">
<id name="id" column="id">
<!-- 主键自增长方式 -->
<generator class="native"></generator>
</id>
<property name="name" />
<property name="price" />
</class>
</hibernate-mapping>
4.配置 hibernate.cfg.xml(数据库连接配置)
在src目录下创建 hibernate.cfg.xml,配置访问数据库要用到的驱动,url,账号密码等等
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- 表结构配置 -->
<mapping resource="pojo/Product.hbm.xml" />
</session-factory>
</hibernate-configuration>
5.创建TestHibernate进行测试
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import pojo.Product;
public class TestHibernate {
/*hibernate的基本步骤是:
1. 获取SessionFactory
2. 通过SessionFactory 获取一个Session
3. 在Session基础上开启一个事务
4. 通过调用Session的save方法把对象保存到数据库
5. 提交事务
6. 关闭Session
7. 关闭SessionFactory
* */
public static void main(String[] args) {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
Product p = new Product();
p.setName("iphone7");
p.setPrice(7000);
s.save(p);
s.getTransaction().commit();
s.close();
sf.close();
}
}
二、插入操作
public static void main(String[] args) {SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
for (int i = 0; i < 10; i++) {
Product p = new Product();
p.setName("iphone"+i);
p.setPrice(i);
s.save(p);
}
s.getTransaction().commit();
s.close();
sf.close();
}
三、Hibernate对象状态
1.实体类对象在Hibernate中有3种状态 :分别是瞬时,持久和脱管1)瞬时:指的是没有和hibernate发生任何关系,在数据库中也没有对应的记录,一旦JVM结束,这个对象也就消失了
2)持久:指得是一个对象和hibernate发生联系,有对应的session,并且在数据库中有对应的一条记录
3)脱管:指的是一个对象虽然在数据库中有对应的一条记录,但是它所对应的session已经关闭了
2.关于三种状态的实际操作
1)new 了一个Product();,在数据库中还没有对应的记录,这个时候Product对象的状态是瞬时的。
2)通过Session的save把该对象保存在了数据库中,该对象也和Session之间产生了联系,此时状态是持久的。
3)最后把Session关闭了,这个对象在数据库中虽然有对应的数据,但是已经和Session失去了联系,相当于脱离了管理,状态就是脱管的
四、通过id获取对象,进行简单CRUD操作
public static void main(String[] args) {SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
Product p =(Product) s.get(Product.class, 7);
System.out.println("id=7的产品名称是: "+p.getName());
//修改id为7的商品名称
p.setName("修改名称");
s.update(p);
//删除id=6的商品
s.delete(p);
s.getTransaction().commit();
s.close();
sf.close();
}
五、hibernate的三种查询方式
1.hql查询
public static void main(String[] args) {/**
* 1. 首先根据hql创建一个Query对象
2. 设置参数(和基1的PreparedStatement不一样,Query是基0的)
3. 通过Query对象的list()方法即返回查询的结果了。
*/
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
String name = "iphone1";
Query q =s.createQuery("from Product p where p.name like ?");
//模糊匹配查找
//q.setString(0, "%"+name+"%");
//精确查找
q.setString(0, name);
List<Product> ps= q.list();
for (Product p : ps) {
System.out.println(p.getName());
}
s.getTransaction().commit();
s.close();
sf.close();
}
2.使用Criteria 查询数据
1)通过session的createCriteria创建一个Criteria 对象2)Criteria.add 增加约束。 在本例中增加一个对name的模糊查询(like)
3)调用list()方法返回查询结果的集合
public static void main(String[] args) {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
String name = "iphone";
Criteria c= s.createCriteria(Product.class);
c.add(Restrictions.like("name", "%"+name+"%"));
List<Product> ps = c.list();
for (Product p : ps) {
System.out.println(p.getName());
}
s.getTransaction().commit();
s.close();
sf.close();
}
3.标准化SQL查询
public static void main(String[] args) {SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
String name = "iphone";
String sql = "select * from product_ p where p.name like '%"+name+"%'";
Query q= s.createSQLQuery(sql);
List<Object[]> list= q.list();
for (Object[] os : list) {
for (Object filed: os) {
System.out.print(filed+"\t");
}
System.out.println();
}
s.getTransaction().commit();
s.close();
sf.close();
}
相关文章推荐
- JAVA学习笔记09——Hibernate框架第三章
- JAVA学习笔记08——Hibernate框架第二章
- JAVA学习笔记07——Hibernate框架第一章
- 我的java框架学习笔记hibernate复…
- 黑马程序员-java学习笔记-集合框架
- 【转】Java集合框架学习笔记
- java学习笔记-集合框架-黑马
- Java框架学习笔记——Spring开发环境搭建
- Java学习笔记——JCF集合框架
- java 从零开始,学习笔记之基础入门<Hibernate_实战演练>(三十七)
- Java框架学习笔记——写在开始之前
- java 从零开始,学习笔记之基础入门<三大框架整合>(四十二)
- 深入浅出学习hibernate框架(三):java的反射机制
- [原]java专业程序代写(qq:928900200),学习笔记之基础入门<Hibernate_实战演练>(三十七)
- java之Collection框架学习笔记
- java笔记:自己动手写javaEE框架(五)--Spring事务管理学习
- java学习笔记:集合框架之TreeSet
- java笔记:自己动手写javaEE框架(五)--Spring事务管理学习
- 【Java学习笔记】集合框架的学习
- [原]java专业程序代写(qq:928900200),学习笔记之基础入门<Hibernate_配置详解>(三十六)