Hibernate实现增删改查
来源:互联网 发布:ed2k樱井知香 喷泉 编辑:程序博客网 时间:2024/04/29 20:17
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">oracle.jdbc.driver.OracleDriver</property><property name="connection.url">jdbc:oracle:thin:@localhost8080:1521:orcl</property><property name="connection.username">hr</property><property name="connection.password">123456</property><property name="dialect">org.hibernate.dialect.OracleDialect</property><property name="show_sql">true</property><property name="format_sql">true</property><mapping resource="net/yedward/db/mapping/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"><hibernate-mapping package="net.yedward.db.domain"> <class name="Student" table="student"> <id name="id" column="id"></id> <property name="name" column="name"></property> <property name="age" column="age"></property> </class></hibernate-mapping>
Student.java中的代码如下:
package net.yedward.db.domain;import java.io.Serializable;public class Student implements Serializable {private static final long serialVersionUID = 5631142352656445262L;private int id; // 注意在表中id是主键private String name;private int age;public int getAge() {return age;}public int getId() {return id;}public String getName() {return name;}public void setAge(int age) {this.age = age;}public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}}
StudentDAO.java中的代码如下:
package net.yedward.db.dao;import net.yedward.db.domain.Student;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class StudentDao {// main方法,用于测试public static void main(String[] args) {delete(3);}// 增加public static void add(Student student) {Configuration cfg = new Configuration();SessionFactory sf = cfg.configure().buildSessionFactory(); // 加载配置文件去构建Session工厂Session session = sf.openSession();session.beginTransaction();session.save(student);session.getTransaction().commit();session.close();sf.close();}// 查询,id是主键public static Student findById(int id) {Configuration cfg = new Configuration();SessionFactory sf = cfg.configure().buildSessionFactory(); // 加载配置文件去构建Session工厂Session session = sf.openSession();// 查询不需要事务transactionStudent student = (Student) session.get(Student.class, id);session.close();sf.close();return student;}// 修改public static void update(Student student) {Configuration cfg = new Configuration();SessionFactory sf = cfg.configure().buildSessionFactory(); // 加载配置文件去构建Session工厂Session session = sf.openSession();session.beginTransaction();session.update(student);session.getTransaction().commit();session.close();sf.close();}// 删除public static void delete(int id) {Configuration cfg = new Configuration();SessionFactory sf = cfg.configure().buildSessionFactory(); // 加载配置文件去构建Session工厂Session session = sf.openSession();session.beginTransaction();Student student = new Student();student.setId(id);session.delete(student);session.getTransaction().commit();session.close();sf.close();}}
上面的这些代码中,StudentDAO.java中的获得session的代码可以直接封装成一个工具类来调用,这里没有这样做了,以及hibernate的映射也可以用Annotation来写,这里也没这样做了,以后补充。
0 0
- Hibernate实现增删改查
- Hibernate实现增删改查
- hibernate中的增删改查实现代码
- strut2+hibernate实现的增删改查
- 实现hibernate的增删查改。
- Hibernate实现简单的增删改查
- Hibernate实现增删查改CURD例子
- Hibernate实现增删改查 ---- Hibernate_3
- Spring + Hibernate实现简单增删改查
- SpringMVC + Hibernate实现增删改查
- Hibernate配置以及增删改查实现
- Hibernate 实现增删改查案例
- hibernate增删查改
- hibernate增删改查
- Hibernate增删改查
- hibernate 增删改查
- hibernate 增删改查
- Hibernate 增删改查
- HashMap是如何组织数据的
- NumPy简明教程之二
- 我的网易博客http://wmzjzwlzs.blog.163.com/
- 我的网易博客http://wmzjzwlzs.blog.163.com/
- 我的网易博客http://wmzjzwlzs.blog.163.com/
- Hibernate实现增删改查
- 我的网易博客http://wmzjzwlzs.blog.163.com/
- 我的网易博客http://wmzjzwlzs.blog.163.com/
- 我的网易博客http://wmzjzwlzs.blog.163.com/
- SVN Unable to connect to a repository at URL
- Android Studio生成自定义jar包(同时将assets目录打入jar包)(一)(by 星空武哥)
- ORA-12519错误的解决方案
- B-Tree
- Swift访问控制