Hibernate使用mysql例子:
1) 新建一个bean: User.java
package com.my.bean; import java.util.Date; public class User { private int userid; private String username; private String password; private char status; private int isBuild; private Date createTime; private Date lastUpdateTime; public int getUserID() { return userid; } public void setUserID(int userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public char getStatus() { return status; } public void setStatus(char status) { this.status = status; } public int getIsBuild() { return isBuild; } public void setIsBuild(int isBuild) { this.isBuild = isBuild; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getLastUpdateTime() { return lastUpdateTime; } public void setLastUpdateTime(Date lastUpdateTime) { this.lastUpdateTime = lastUpdateTime; } }
2) 新建一个hibernate mapping xml file: User.hbm.xml
<?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> <class name="com.my.bean.User" table="sys_user"> <id name="UserID" type="int"> <column name="UserID" /> <generator class="assigned" /> </id> <property name="Username" type="java.lang.String" length="50"> <column name="Username" /> </property> <property name="Password" type="java.lang.String" length="50"> <column name="Password" /> </property> <property name="Status" type="char" length="1"> <column name="Status" /> </property> <property name="IsBuild" type="int"> <column name="IsBuild" /> </property> <property name="CreateTime" type="java.util.Date"> <column name="CreateTime" /> </property> <property name="LastUpdateTime" type="java.util.Date"> <column name="LastUpdateTime" /> </property> </class> </hibernate-mapping>
3)新建一个hibernate config file: hibernate.cfg.xml
<?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> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://127.0.0.1/obs</property> <property name="connection.username">root</property> <property name="connection.password"></property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- Enable Hibernate‘s automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping resource="com/my/hbm/User.hbm.xml"/> </session-factory> </hibernate-configuration>
4)新建一个类:HibernateUtil.java
package com.my.dao.util; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml Configuration configuration = new Configuration(); return configuration.configure().buildSessionFactory( new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build()); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
这是hibernate 4的sessionFactory方法。
5)测试:
package com.my.test; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import com.my.bean.User; import com.my.dao.util.HibernateUtil; public class ConsoleTest { @SuppressWarnings("unchecked") public static void main(String[] args) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); String hqlSelect = "from User where UserID=:UserID"; Query query = session.createQuery(hqlSelect); query.setParameter("UserID", 1); List<User> users = query.list(); session.getTransaction().commit(); HibernateUtil.getSessionFactory().close(); for (User user : users) { System.out.println("User name: " + user.getUsername()); } } }