步骤 2 : 创建数据库how2java 步骤 3 : pojo 步骤 4 : Product.hbm.xml 步骤 5 : hibernate.cfg.xml 步骤 6 : dao
这部分准备Hibernate方面的事情
create database how2java
create database how2java
Product有id,name,price3个字段
package com.how2java.pojo;
public class Product {
private int id;
private String name;
private 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;
}
}
映射Product类对应product_表
<?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="com.how2java.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>
<?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="com.how2java.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>
hibernate基本配置,指定数据库,账号,密码,方言等等
<?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/how2java?characterEncoding=GBK</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</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="com/how2java/pojo/Product.hbm.xml" />
</session-factory>
</hibernate-configuration>
为ProductDAO准备增,删,修改,查询,获取方法
package com.how2java.dao;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.how2java.pojo.Product;
public class ProductDAO {
public void add(Product p) {
List<Product> result = new ArrayList();
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
s.save(p);
s.getTransaction().commit();
s.close();
sf.close();
}
public Product get(int id) {
Product result = null;
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
Session s = sf.openSession();
result = (Product) s.get(Product.class, id);
s.close();
sf.close();
return result;
}
public void delete(int id) {
List<Product> result = new ArrayList();
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
Product p = (Product) s.get(Product.class, id);
s.delete(p);
s.getTransaction().commit();
s.close();
sf.close();
}
public void update(Product p) {
List<Product> result = new ArrayList();
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
s.update(p);
s.getTransaction().commit();
s.close();
sf.close();
}
public List<Product> listProduct() {
List<Product> result = new ArrayList();
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
Session s = sf.openSession();
Query q = s.createQuery("from Product p");
result = q.list();
s.close();
sf.close();
return result;
}
}
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
问答区域
2021-04-06
ProductDAO.add里List<Product> result = new ArrayList();是必要的代码吗?
2019-08-30
关于更新的问题
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
2019-02-19
除了查询,其他方法里面放个result意义是什么?后面也没有用到
2019-02-11
关于SessionFactory
2018-12-11
ProductDAO中可以把SessionFactory和Sesson放到成员变量么,这样就不用每次调用一个方法就,新建一个
提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 8 条以前的提问,请 点击查看
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|