一个hibernate入门小程序 | |
| |
学习hibernate刚刚开始,一个小Demo,大家一起学习。 /********************************Module对象***********************************/ package wangzhongjie.hibernate.module; /** * * @author ice man * */ public class USER_INF{ private String id; private String username; private String password; /** * @return Returns the password. */ public String getPassword() { return password; } /** * @param password The password to set. */ public void setPassword(String password) { this.password = password; } /** * @return Returns the username. */ public String getUsername() { return username; } /** * @param username The username to set. */ public void setUsername(String username) { this.username = username; } /** * @return Returns the id. */ public String getId() { return id; } /** * @param id The id to set. */ public void setId(String id) { this.id = id; } } /********************************对应Module对象的配置文件***********************************/ http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> /********************************DDL***********************************/ CREATE TABLE `user_inf` ( `ID` varchar(100) NOT NULL default '0', `USER_NAME` varchar(100) default NULL, `PASSWORD` varchar(100) default NULL, PRIMARY KEY (`ID`) ) /********************************hibernate配置文件***********************************/ hibernate.dialet=net.sf.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.mysql.jdbc.Driver hibernate.connection.url=jdbc:mysql://localhost/test hibernate.connection.username=root hibernate.connection.password=root hibernate.show_sql=true; /********************************测试类***********************************/ package wangzhongjie.hibernate.test; import java.util.Iterator; import java.util.List; import org.hibernate.Query; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import wangzhongjie.hibernate.module.USER_INF; /** * * @author ice man * */ public class Main { public static SessionFactory sf = null; static{ try{ //读取hibernate.properties中的配置信息 Configuration config = new Configuration(); config.addClass(USER_INF.class); //创建sessionFactory工厂 sf = config.buildSessionFactory(); }catch(Exception e){ e.printStackTrace(); } } /** * * @param customer * @throws Exception */ public void saveCustomer(USER_INF user) throws Exception{ //取得connection Session session = sf.openSession(); //创建事务 Transaction tx = null; try { tx = session.beginTransaction(); session.save(user); tx.commit(); }catch (Exception e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } } public void findAllCustomers() throws Exception{ Session session = sf.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Query q=session.createQuery("from USER_INF"); for (Iterator it = q.list().iterator(); it.hasNext();) { USER_INF user = (USER_INF) it.next(); System.out.println("以下是wangzongjie的帐户信息 :"); System.out.println("帐户序号 : "+user.getId()); System.out.println("登陆帐号 : "+user.getUsername()); System.out.println("登陆密码 : "+user.getPassword()); } tx.commit(); }catch (Exception e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } } /** * @param args */ public static void main(String[] args) { USER_INF user = new USER_INF(); user.setPassword("12345678"); user.setUsername("wangzhongjie"); user.setId("000000001"); Main main = new Main(); try { main.saveCustomer(user); System.out.println("保存成功!"); main.findAllCustomers(); } catch (Exception e) { e.printStackTrace(); } } } /********************************运行结果***********************************/ 保存成功! 以下是wangzongjie的帐户信息 : 帐户序号 : 000000001 登陆帐号 : wangzhongjie 登陆密码 : 12345678 |