hibernate

需要配置hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
<!-- 数据库连接URL -->
<property name="connection.url">
jdbc:mysql://localhost:3306/j2ee
</property>
<!-- 数据库连接驱动 -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- 数据库用户名 -->
<property name="connection.username">root</property>
<!-- 数据库用户密码 -->
<property name="connection.password"></property>
<!-- 数据库方言 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 指定映射文件 -->
<mapping resource="com/gsww/kingreturns/struts2/excise/Product.hbm.xml"/>
</session-factory>
</hibernate-configuration>
映射文件 Product.hbm.xml
<hibernate-mapping>
<!-- 每个class对应一个持久化对象 -->
<class name="com.gsww.kingreturns.struts2.excise.product">
<!-- id元素用来定义主键标识,并指定主键生成策略 -->
<id name="id">
<generator class="assigned"></generator>
</id>
<!-- 定义其他属性 -->
<property name="name"></property>
<property name="price"></property>
</class>
</hibernate-mapping>


product文件

public class product {
//产品ID
private String id;
//产品名称
private String name;
//产品价格
private double price;
//获得产品ID
public String getId() {
return id;
}
//设置产品ID
public void setId(String id) {
this.id = id;
}
//获得产品名称
public String getName() {
return name;
}
//设置产品名称
public void setName(String name) {
this.name = name;
}
//获得产品价格
public double getPrice() {
return price;
}
//设置产品价格
public void setPrice(double price) {
this.price = price;
}
}
创建表
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class creatdb {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Configuration
/* Configuration cfg=new Configuration().configure();
SchemaExport sExport=new SchemaExport(cfg);
sExport.create(true, true);*/
Configuration cfg = new Configuration().configure();
SchemaExport sExport = new SchemaExport(cfg);
sExport.create(true, true);
}
}


写入数据

链接文件

public class hibernateutil {
private static SessionFactory factory;
static{
//读取配置文件hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
//创建SessionFactory
factory = cfg.buildSessionFactory();
}
//获得SessionFactory实例
public static SessionFactory getSessionFactory(){
return factory;
}
//获得Session实例
public static Session getSession() {
return factory.openSession();
}
//关闭指定Session
public static void closeSession(Session session) {
if(session != null) {
if(session.isOpen()) {
session.close();
}
}
}
}
 
public class adddate {
public static void main(String[] args) {
// TODO Auto-generated method stub
Session session=hibernateutil.getSession();
/* product pro = new product();
pro.setId("1");
pro.setName("1");
pro.setPrice(1000.00);
session.delete(pro);*/   删除 添加为save  更新为update
/* String hql="DELETE product WHERE id=?";
Query q=session.createQuery(hql);
q.setString(0,"0");
q.executeUpdate();*/
product p=null;
/* String hql="FROM product as pro";*/
String hql="FROM product as pro WHERE pro.id=?";  //hql语句
Query q=session.createQuery(hql);
q.setString(0,"0");
List list =q.list();
Iterator iter=list.iterator();
while(iter.hasNext())
{
p=(product) iter.next();
System.out.println("id:"+p.getId()+"name:"+p.getName()+"price:"+p.getPrice());
}
hibernateutil.closeSession(session);
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值