hibernate篇章六--demo(Hibernate之第1解之-hibernate_demo_1)
准备工作做好了,开始Hibernate:
1:建立包:package:dao、model、service、util包;
2:开始model实体类Student:id,sname生成getter/setter;
Java代码
1 package model; 2 3 public class Student { 4 private int id; 5 private String name; 6 7 public int getId() { 8 return id; 9 } 10 private void setId(int id) { 11 this.id = id; 12 } 13 public String getName() { 14 return name; 15 } 16 public void setName(String name) { 17 this.name = name; 18 } 19 }
3:在Hibernate源码下/project/搜索 *.xml复制以下文件:
hibernate.cfg.xml-->src下;
*.hbm.xml-->model下;
4:配置hibernate.cfg.xml
1 <session-factory> 2 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 3 <property name="connection.url">jdbc:mysql://localhost:3306/hibnie</property> 4 <property name="connection.username">root</property> 5 <property name="connection.password">1234</property> 6 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 7 <property name="show_sql">true</property> 8 9 <mapping resource="model/Student.hbm.xml"/> 10 </session-factory>
5:配置Student.hbm.xml
1 <hibernate-mapping package="model"> 2 <class name="Student" table="s" lazy="false"> 3 <id name="id" type="integer" column="sid"> 4 <generator class="identity"/> 5 </id> 6 <property name="name" type="string" column="sname" /> 7 </class> 8 </hibernate-mapping>
6:加入JUnit4 jar包,加入path;
7:写util.HibernateUtil
1 package util; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.cfg.Configuration; 6 7 public class HibernateUtil { 8 private static final SessionFactory sessionFactory=buildSessionFactory(); 9 10 /** init sessionFactory */ 11 private static SessionFactory buildSessionFactory() { 12 SessionFactory sessionFactory=null; 13 try{ 14 sessionFactory=new Configuration().configure().buildSessionFactory(); 15 }catch(Throwable te){ 16 System.err.println("init sessionFactory Error:"+te); 17 te.printStackTrace(); 18 } 19 return sessionFactory; 20 } 21 22 /** get session */ 23 public static Session getSession(){ 24 Session stion=sessionFactory.openSession(); 25 return stion; 26 } 27 28 }
8:测试HIbernateUtil
1 @Test 2 public void testGetSession() { 3 Session stion=HibernateUtil.getSession(); 4 System.out.println(stion); 5 }
9:dao层-->StudentDao
1 package dao; 2 3 import java.util.Iterator; 4 import org.hibernate.Criteria; 5 import org.hibernate.HibernateException; 6 import org.hibernate.Query; 7 import org.hibernate.SQLQuery; 8 import org.hibernate.Session; 9 import org.hibernate.Transaction; 10 import org.hibernate.criterion.Restrictions; 11 import util.HibernateUtil; 12 import model.Student; 13 14 public class StudentDao { 15 /** insert; */ 16 public void addStudent(Student stu){ 17 Session session=HibernateUtil.getSession(); 18 Transaction tx=null; 19 try{ 20 tx=session.beginTransaction(); 21 session.save(stu); 22 tx.commit(); 23 }catch(HibernateException he){ 24 tx.rollback(); 25 he.printStackTrace(); 26 }finally{ 27 session.close(); 28 } 29 } 30 /** select; */ 31 public Student getStudent(int sid){ 32 Session session=HibernateUtil.getSession(); 33 Transaction tx=null; 34 Student stu=null; 35 try{ 36 tx=session.beginTransaction(); 37 stu=(Student)session.load(Student.class,new Integer(sid)); 38 // stu=(Student)session.get(Student.class,new Integer(sid)); 39 tx.commit(); 40 }catch(HibernateException he){ 41 tx.rollback(); 42 he.printStackTrace(); 43 }finally{ 44 session.close(); 45 } 46 return stu; 47 } 48 /** update */ 49 public void updateStudent(String oldName,String newName){ 50 Session session=HibernateUtil.getSession(); 51 Transaction tx=null; 52 try{ 53 tx=session.beginTransaction(); 54 Query q=session.createQuery("from Student s where s.name= ?"); 55 q.setParameter(0, oldName); 56 Iterator<Student> it=q.list().iterator(); 57 while(it.hasNext()){ 58 Student s=it.next(); 59 s.setName(newName); 60 session.update(s); 61 } 62 tx.commit(); 63 session.close(); 64 }catch(HibernateException he){ 65 tx.rollback(); 66 he.printStackTrace(); 67 } 68 } 69 /** delete */ 70 public void deleteStudent(String str){ 71 Session session=HibernateUtil.getSession(); 72 Transaction tx=null; 73 try { 74 tx=session.beginTransaction(); 75 /* 1.Criteria查询; */ 76 Criteria criter=session.createCriteria(Student.class).add(Restrictions.like("name","%"+str+"%")); 77 Iterator<Student> it=criter.list().iterator(); 78 /* 2.Query查询 79 Query q=session.createQuery("from Student as s where s.name like ?"); 80 q.setParameter(0, "%"+str+"%"); 81 Iterator<Student> it=q.list().iterator(); */ 82 /* 3.Native sql查询 表名和表中的列名而不是实体类的类名和属性名了 83 SQLQuery sqlq=session.createSQLQuery("SELECT * FROM s WHERE sname like ?").addEntity(Student.class); 84 sqlq.setParameter(0, "%"+str+"%"); 85 Iterator<Student> it=sqlq.list().iterator(); */ 86 while(it.hasNext()){ 87 session.delete(it.next()); 88 } 89 tx.commit(); 90 } catch (HibernateException he) { 91 tx.rollback(); 92 he.printStackTrace(); 93 } finally { 94 session.close(); 95 } 96 } 97 }
10.业务层-->service:StudentManager:
1 package service; 2 3 import dao.StudentDao; 4 import model.Student; 5 6 public class StudentManager { 7 StudentDao dao=new StudentDao(); 8 public void save(Student stu){ 9 dao.addStudent(stu); 10 } 11 public Student get(int id){ 12 return dao.getStudent(id); 13 } 14 public void update(String oname,String nname){ 15 dao.updateStudent(oname, nname); 16 } 17 public void delete(String name){ 18 dao.deleteStudent(name); 19 } 20 }
11.测试业务逻辑:StudentManagerTest
1 package service; 2 3 import model.Student; 4 import org.junit.Before; 5 import org.junit.Test; 6 7 public class StudentManagerTest { 8 StudentManager manager; 9 @Before 10 public void init(){ 11 manager=new StudentManager(); 12 } 13 @Test public void testSave() { 14 Student stu1=new Student(); 15 stu1.setName("John woo"); 16 manager.save(stu1); 17 } 18 @Test public void testGet(){ 19 Student stu=manager.get(12); 20 System.out.println(stu.getName()); 21 } 22 @Test public void testUpdate(){ 23 manager.update("Mary","Nie"); 24 } 25 @Test public void testDelete(){ 26 manager.delete("ie"); 27 } 28 }
个基本hibernate就完成了,包括基本的CRUD还有3种查询方式。
备注:lib下的jar包:
1 antlr-2.7.6.jar 2 commons-collections-3.1.jar 3 dom4j-1.6.1.jar 4 hibernate3.jar 5 hibernate-jpa-2.0-api-1.0.0.Final.jar 6 javassist-3.12.0.GA.jar 7 jta-1.1.jar 8 junit-4.9b1.jar 9 mysql6.jar 10 slf4j-api-1.6.1.jar 11 slf4j-simple-1.6.1.jar
注意:
1.原作者:学不可以已
2.我做完之后发现一个问题;就是update不成功,不知道是什么原因
3.junit是进行单元测试用到的,可以去我的微博搜素相关资料,输入关键字“junit”,输入下图中的“查询框中”
4:附带我的代码,跟这文章的上一篇章0中分享的我自己修改的代码是一样的。链接:http://pan.baidu.com/s/1nt65KgD 密码:6uac
---- 动动手指关注我!或许下次你又能在我这里找到你需要的答案!ZZZZW与你一起学习,一起进步!