hibernate数据增删改查汇总

项目源码传送门:http://download.csdn.net/download/six_666666/10009874
项目框架 数据库表


思路很重要hibernate数据库增删改查需要哪些文件:
1、hibernate主配置文件hibernate.cfg.xml
2、hibernate的实体工具类
3、hibernate实体类对应的xml映射文件
4、剩下的就是数据库增删改查的代码

代码部分
hibernate.cfg.xml
<!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节点代表一个数据库 -->

<session-factory>

<!-- 1. 数据库连接配置 -->

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql:///test?useUnicode=true&amp;characterEncoding=UTF8</property>

<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password">root</property>

<!-- 数据库方法配置,hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql -->

<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

<!-- 2. 其他相关配置 -->

<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->

<property name="hibernate.show_sql">true</property>

<!-- 2.2 格式化sql -->

<property name="hibernate.format_sql">true</property>

<!-- 2.3 自动建表 -->

<property name="hibernate.hbm2ddl.auto">update</property>

<!-- 3. 加载所有映射 -->

<mapping resource="yw/Person.hbm.xml" />

</session-factory>

</hibernate-configuration>

Person.java
package yw;

public class Person {
private Integer id;
private String name;
private Integer password;
public Person(){
}
public Person(Integer id, String name, int password) {
super();
this.id = id;
this.name = name;
this.password = password;
}

public int getPassword() {
return password;
}

public void setPassword(int password) {
this.password = password;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

Person.hbm.xml
<?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="yw">

<class name="Person" table="person">

<id name="id" column="id">

<generator class="native" />

</id>

<property name="name" column="name" />

<property name="password" column="password" />

</class>

</hibernate-mapping>

增加数据
package yw;
import java.sql.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import yw.Person;

public class Add {
static Add add = new Add();

public static void main(String[] args) {
Configuration config = new Configuration().configure();
// 原来configure()方法默认会在classpath下面寻找hibernate.cfg.xml文件,如果没有找到该文件,系统会打印
// 如下信息并抛出HibernateException异常。
SessionFactory sessionFactory = config.buildSessionFactory();
// 此方法已过时4.0中已不再用被ServiceRegistry替代
Session session = sessionFactory.openSession();
// 打开一个新的session对象
Transaction tx = session.beginTransaction();
// 开启事务
session.save(add.add());
// 保存新创建的对象
tx.commit();
// 提交
// session.close();
sessionFactory.close();// 一定 不 要 关闭.这里只是写出来做例子
/*
* 注意:sessionFactory一定不要关闭.原因是: 它线程安全,所以一个程序只需要获取一次.如果每次都关闭,
* 重新获取执行效率降低!!!
*/
}

private Person add() {
Person p = new Person(1, "Tom", 987654);
return p;
}
}

删除数据
package yw;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Del {
static Del del = new Del();
public static void main(String[] args) {
Configuration config = new Configuration().configure();
// 原来configure()方法默认会在classpath下面寻找hibernate.cfg.xml文件,如果没有找到该文件,系统会打印
// 如下信息并抛出HibernateException异常。
SessionFactory sessionFactory = config.buildSessionFactory();
// 此方法已过时4.0中已不再用被ServiceRegistry替代
Session session = sessionFactory.openSession();
// 打开一个新的session对象
Transaction tx = session.beginTransaction();
// 开启事务
del.delete(session);
//删除数据
tx.commit();
// 提交
// session.close();
sessionFactory.close();// 一定 不 要 关闭.这里只是写出来做例子
/*
* 注意:sessionFactory一定不要关闭.原因是: 它线程安全,所以一个程序只需要获取一次.如果每次都关闭,
* 重新获取执行效率降低!!!
*/
}
public void delete(Session session) {
//第一种方法--持久化状态删除--建议使用
Person p = (Person) session.load(Person.class, 1);
session.delete(p);
}
}

修改数据
package yw;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Update {
static Update mt = new Update();
public static void main(String[] args) {
java.util.Date d = new java.util.Date();
System.out.println(d);
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Person p = new Person();
p.setId(1);
p.setName("Bon");
p.setPassword(987654);
session.update(p);
tx.commit();
session.close();
}
}

查询数据
package yw;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Select {
static Select select = new Select();
public static void main(String[] args) {
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
String hql = "FROM Person as p where p.name=:name";//sql语句中的Person是对象,不是表
Query query = session.createQuery(hql);
query.setString("name","Tom");//此name是对应where语句中,冒号后面的 name(p.name=:name)
List<Person> list = query.list();
for(Person p : list){
System.err.println(p.getName());
}
session.close();
}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值