嘿嘿,生活就是这样,你多么的努力,没人看到,但如果你不努力,那么你就更没人看到了
哈哈,说了些不必要的。在这里我把Hibernate 3.0 做个小小的例子
lib:
antlr-2.7.6.jar
commons-collections-3.1.jar
commons-logging-1.1.1.jar
dom4j-1.6.1.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.12.0.GA.jar
hibernate3.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
mysql-connector-java-5.0.6-bin.jar
slf4j-api-1.6.1.jar
slf4j-api-1.6.2.jar
slf4j-ext-1.6.2.jar
slf4j-jcl-1.6.2.jar
slf4j-jdk14-1.6.2.jar
slf4j-log4j12-1.6.2.jar
slf4j-nop-1.6.2.jar
slf4j-simple-1.6.2.jar
package com.chenhailong; import java.io.Serializable; public class TUser implements Serializable { /** * */ private static final long serialVersionUID = -1886893835045076202L; private Integer id; private String name; public TUser() { } /** * @return the id */ public Integer getId() { return id; } /** * @param id * the id to set */ public void setId(Integer id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name * the name to set */ public void setName(String name) { this.name = name; } }
<?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.chenhailong"> <class name="com.chenhailong.TUser" table="TUser"> <id name="id" column="id" type="java.lang.Integer"> <generator class="native" /> </id> <property name="name" column="name" type="java.lang.String" not-null="true" /> </class> </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration> <session-factory> <!-- properties --> <property name="connection.username">root</property> <property name="connection.url">JDBC:MySQL://localhost:3306/test</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.password">chenhailong</property> <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property> <!-- mapping files --> <mapping resource="com/chenhailong/TUser.hbm.xml" /> </session-factory> </hibernate-configuration>
/** * */ package com.chenhailong.main; import java.io.File; 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; import com.chenhailong.TUser; /** * @author chenhailong *上午10:09:22 *com.chenhailong.main */ public class Main { /** * Hibernate 3 dialect modify net.sf.hibernate -> org.hibernate */ public static void main(String[] args) { Session session = getSession(); //insertTUser(session); //selectTUser(session); deleteTUser(session); session.close(); } public static Session getSession() { Configuration cfg = new Configuration().configure(); SessionFactory sessionFactory = cfg.buildSessionFactory(); Session session = sessionFactory.openSession(); return session; } public static Session getSession(String fileName) { File file = new File(fileName); Configuration cfg = new Configuration().configure(file); SessionFactory sessionFactory = cfg.buildSessionFactory(); Session session = sessionFactory.openSession(); return session; } public static void insertTUser(Session session) { Transaction tx = session.beginTransaction(); TUser t_user = new TUser(); t_user.setName("chenhailong"); session.save(t_user); session.flush(); tx.commit(); } @SuppressWarnings("unchecked") public static void selectTUser(Session session) { String hql = " from TUser where name='chenhailong'"; try { List<TUser> userList = session.createQuery(hql).list(); for (TUser user : userList) { int id = user.getId(); String name = user.getName(); System.out.println(id + "\t" + name); } } catch (Exception e) { e.printStackTrace(); } } public static void deleteTUser(Session session) { Transaction tx = session.beginTransaction(); TUser user = (TUser) session.get(TUser.class, new Integer(1)); session.delete(user); tx.commit(); } public static void deleteTUserUseHQL(Session session) { Transaction tx = session.beginTransaction(); session.delete("from TUser where id = 1"); tx.commit(); } public static void deleteTUseHQLByQuery(Session session) { String hql = "delete TUser where id = 1"; Query query = session.createQuery(hql); query.executeUpdate(); } }