已经通宵三天撸代码了,现在的我已经养成晚上修仙写代码的节奏了.....最近 刚刚复习到了 hibernate 谈谈 这篇文章就谈谈我对这货的理解吧.
在看这篇文章之前希望你 知道sessionfactory 和session 还有 transaction(事物) 诶嘿不造的可以去看看http://blog.csdn.net/u012377333/article/details/48086193
OK正题开始 步骤 我们需要一个可以持久化的bean类 还有它的附属映射 hbm.xml文件 以及dao(重要) and 测试类
首先开始是bean类代码如下:
package qy.java.bean; public class Userbean { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } @Override public String toString() { return "Userbean{" + "id=" + id + ", username=‘" + username + ‘\‘‘ + ", password=‘" + password + ‘\‘‘ + ‘}‘; } }
其次是他的映射文件
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="qy.java.bean"> <class name="Userbean" table="x_user"> <id name="id" type="int" column="id"> <generator class="native"/> </id> <property name="username" type="string" column="username" length="20"/> <property name="password" type="string" column="password" length="20"/> </class> </hibernate-mapping>
这里要说一个 在主配置文件里面我没有配置自动建表的那货= = 所以我是直接手动 建表 有兴趣的同学可以自己去配置
然后是dao类 里面包括增删改查 已经苟 对象id算是买4送一吧,哦对了 我这种初始方法是比较老的一种方法 新的初始化方法在下面
package qy.java.dao; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.query.Query; import qy.java.bean.Userbean; import java.util.List; public class Userdao { private static SessionFactory sf; static { Configuration cfg = new Configuration().configure("hibernate.cfg.xml"); sf = cfg.buildSessionFactory();// 获得会话工厂 } // 增加 public void add(Userbean user) { // 读配置文件,会 Session s = sf.openSession(); Transaction t = null; try { t = s.beginTransaction(); s.save(user); //持久化 对象 t.commit(); //提交 } catch (Exception e) { t.rollback(); //回滚 throw new RuntimeException(e); } finally { s.close(); } } // 更新 public void update(Userbean user) { // 读配置文件,会 Session s = sf.openSession(); Transaction t = null; try { t = s.beginTransaction(); s.update(user); // s.update("username", user); t.commit(); } catch (Exception e) { t.rollback(); throw new RuntimeException(e); } finally { s.close(); } } //删除 public void delete(int id) { // 读配置文件,会 Session s = sf.openSession(); Transaction t = null; try { t = s.beginTransaction(); // User user = (User) s.get(User.class, id); Userbean user = new Userbean(); user.setId(id); s.delete(user); t.commit(); } catch (Exception e) { t.rollback(); throw new RuntimeException(e); } finally { s.close(); } } //查询所有 public List<Userbean> findALL() { // 读配置文件,会 Session s = sf.openSession(); List<Userbean> list = null; try { list = s.createQuery("FROM Userbean").list(); /*Criteria c = s.createCriteria(User.class); list = c.list();*/ } catch (Exception e) { throw e; } finally { s.close(); } return list; } //返回 id = = public long findUserCount() { //int double ,integer Double char Charset Session s = sf.openSession(); Long count = null; try { count = (Long) s.createQuery("SELECT COUNT(*) FROM Userbean ")// .uniqueResult(); } catch (Exception e) { throw e; } finally { s.close(); } return count; } }
这个是新的初始化方法
package qy.java.dao; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import qy.java.bean.Userbean; public class Foxdao { private static SessionFactory sf; static { try { Configuration configuration = new Configuration(); configuration.configure(); sf = configuration.buildSessionFactory(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } // 增加 public void add(Userbean user) { // 读配置文件,会 Session s = sf.openSession(); Transaction t = null; try { t = s.beginTransaction(); s.save(user); //持久化 对象 t.commit(); //提交 } catch (Exception e) { t.rollback(); //回滚 throw new RuntimeException(e); } finally { s.close(); } } }
然后了是测试 方法= = 仁慈的我还给发测试.....
package test.qy.java.dao; import org.junit.Test; import org.junit.Before; import org.junit.After; import qy.java.bean.Userbean; import qy.java.dao.Userdao; import java.util.Iterator; import java.util.List; /** * Userdao Tester. * * @author <Authors name> * @since <pre>???? 20, 2017</pre> * @version 1.0 */ public class UserdaoTest { @Before public void before() throws Exception { } @After public void after() throws Exception { } /** * * Method: add(Userbean user) * */ @Test public void testAdd() throws Exception { Userbean user = new Userbean(); user.setPassword("wqerwqer"); user.setUsername("zhangsan"); new Userdao().add(user); } @Test public void updel(){ Userbean userbean = new Userbean(); userbean.setId(1); userbean.setUsername("滑稽"); userbean.setPassword("10086"); new Userdao().update(userbean); } @Test public void delet(){ new Userdao().delete(2); } @Test public void file(){ List<Userbean> list =new Userdao().findALL(); Iterator<Userbean> it = list.iterator(); Userbean user = null; while (it.hasNext()){ user = it.next(); System.out.println(user.getUsername()); } } @Test public void testFindAllUserCount(){ long count =new Userdao().findUserCount(); System.out.println(count); } }
增删改查虽然简单 但是这些都是基础 就像 功夫里面的蹲马步 跑步一样 灵活运用 才能发挥他们真正的实力 相互之间的配合 结合自己的所需才能 实现作用 最大化..
emmm写完了 刚开始搭建环境的时候出现 hibernate的不兼容我这个 jdk1.7原因是hibernate的版本太高了 = =然后我又懒 所以我就去下了一个1.8明明 十多分钟 能搞定的事情用了 一个多小时...气死欧蕾.
时间: 2024-12-15 07:14:51