首页 > 代码库 > HIbernate实现增、删、改、查。
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> <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="connection.username">sb</property> <property name="connection.password">sb</property> <!-- 输出所有 SQL 语句到控制台。 --> <property name="hibernate.show_sql">true</property> <!-- 在 log 和 console 中打印出 SQL。 --> <property name="hibernate.format_sql">true</property> <!-- 方言 --> <property name="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect</property> <!-- 关联小配置 --><property name="hbm2ddl.auto">update</property> //与小配置进行关联 <mapping resource="entity/Student.hbm.xml"/> </session-factory></hibernate-configuration>
//小配置<?xml version=‘1.0‘ encoding=‘utf-8‘?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="entity"> <class name="Student" table="Student"> <id name="id" type="int" column="id"> </id> <property name="name" type="string" column="name"/> <property name="age" type="int" column="age"/> </class> </hibernate-mapping>
package entity;//学生实体类public class Student { private Integer age; private String name; private Integer id;@Overridepublic String toString() { return "Student [age=" + age + ", name=" + name + ", id=" + id + "]";}public Integer getAge() { return age;}public void setAge(Integer age) { this.age = age;}public String getName() { return name;}public void setName(String name) { this.name = name;}public Integer getId() { return id;}public void setId(Integer id) { this.id = id;}}
//工具类 方便调用 更简洁package Util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtil { private static Configuration cf=new Configuration().configure(); private static SessionFactory sf=cf.buildSessionFactory(); public static Session getSession(){ return sf.openSession(); } public static void CloseSession(){ getSession().close(); }}
1 public static void main(String[]args){2 Class clazz=Student.class();3 System.out.println(clazz);4 }
Session session;Transaction tx;@Before //方法执行前public void beffore(){ session=HibernateUtil.getSession(); tx=Session.beginTransaction;}@After //方法之前后public void after(){tx.commit();HibernateUtil.CloseSession;}
1.增加
public void addtest(){//创建学生对象 Student stu=new Studetn(); stu.setId(1); stu.setName("李泽阳"); stu.sestage(23); //找到和数据库的接口 Session---SessionFactory---configur.buidSessionFactory(); Configuration cf=new Configurration().Configur("Hibernate.cfg.xml"); SessionFactory sf=cf.buildSessionFactory(); session=sf.openSession(); tx=Session.beginTransaction();//保存Session.save(stu);}
2.删除
public void deltest(){ Student stu=new Student(); stu.setId(1); Session.delect(stu); System.out.println("delect OK!");}
3.修改
public void updatetest(){ Student stu=(Student)Session.get(Student.class,1); stu.setName("阳阳阳"); Session.update(stu); System.out.println("update OK!");}
4.查询
public void selecttest(){ Student stu=(Student)Session.get(Student.class,1); System.out.println("select OK!");}
HIbernate实现增、删、改、查。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。