Hibernate(一)简单CURD
Person.java
package com;import java.util.Date;public class Person {private String id;private String name;private Date birthday;public Person(){}public Person(String id,String name, Date birthday) {this.id=id;this.name = name;this.birthday = birthday;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}}
?Person.hbm.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com"> <class name="Person" table="PERSON"> <id name="id" column="ID"> <generator column="NAME"/> <property name="birthday" column="BIRTHDAY"/> </class></hibernate-mapping>
?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.OracleDriver</property> <property name="connection.url">数据库URL</property> <property name="connection.username">数据库用户名</property> <property name="connection.password">密码</property> <property name="connection.pool_size">1</property> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <property name="current_session_context_class">thread</property> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <property name="show_sql">true</property> <mapping resource="com/Person.hbm.xml"/> </session-factory></hibernate-configuration>
?测试Main.java
package com;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class Main {private static SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();private static Session session=sessionFactory.getCurrentSession();public static void main(String[] args) {// get a person// System.out.println(getPerson("xiao_id"));// add a person// addPerson("xiao_id","xiao_name",new Date());// update a person// updatePerson("xiao_id","xiao_name_01",new Date());// delete a person// deletePerson("xiao_id");}// get a person public static Person getPerson(String id){session.beginTransaction();Person person = (Person) session.get(Person.class, id);session.getTransaction().commit();return person;}// add a personpublic static void addPerson(String id,String name,Date birthday){Person person = new Person(id,name,birthday);session.beginTransaction();session.save(person);session.getTransaction().commit();}// update a personpublic static void updatePerson(String id,String name,Date birthday){Person person = new Person(id,name,birthday);session.beginTransaction();session.update(person);session.getTransaction().commit();}// delete a personpublic static void deletePerson(String id){session.beginTransaction();Person person = new Person();person.setId(id);session.delete(person);session.getTransaction().commit();}}
?
1 楼 lijiejava 前天 Antlove ,万岁,万岁,万万岁!