SSH整合,第二篇。
创建工程
这里只是测试和理解hibernate。建立Java工程就好了。
1、hibernate-4.2.21.jar
hibernate包下的required,即hibernate-release-4.2.21.Final equired。
2、hibernate.cfg.xml
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 8 <session-factory> 9 10 <!-- Database connection settings --> 11 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 12 <property name="connection.url">jdbc:mysql://localhost:3306/dbfortest</property> 13 <property name="connection.username">root</property> 14 <property name="connection.password">root</property> 15 16 <!-- JDBC connection pool (use the built-in) --> 17 <!-- <property name="connection.pool_size">1</property> --> 18 19 <!-- SQL dialect --> 20 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 21 22 <!-- Enable Hibernate's automatic session context management --> 23 <property name="current_session_context_class">thread</property> 24 25 <!-- Disable the second-level cache --> 26 <!-- <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> --> 27 28 <!-- Echo all executed SQL to stdout --> 29 <property name="show_sql">true</property> 30 31 <!-- format_sql --> 32 <property name="format_sql">true</property> 33 34 <!-- Drop and re-create the database schema on startup 35 validate |create|create-drop|update 36 --> 37 <!-- <property name="hbm2ddl.auto">update</property> --> 38 39 <!-- mapping 注解的是class="....." ---> 40 <mapping class="com.xzw.ssh.pojo.User" /> 41 <mapping class="com.xzw.ssh.pojo.Permission" /> 42 <mapping class="com.xzw.ssh.pojo.UserAndRole" /> 43 <mapping class="com.xzw.ssh.pojo.Role" /> 44 <mapping class="com.xzw.ssh.pojo.RoleAndPermission" /> 45 46 </session-factory> 47 48 </hibernate-configuration>
注意在classpath下的,不然注意configure设置路径。上面只打开部分要用到属性
3、编写hbm.xml
创建表后,可以用逆向工程来生成annotation的实体类和*.hbm.xml。
4、创建数据库表
可以在创建好*.hbm.xml后,通过<property name="hbm2ddl.auto">create</property>来建表。
5、 最后
由于hibernate的正向和逆向工程的存在,建表或建xml可以自动生成,可以分别生成,然后对比一下,如果不符合要求的列,或属性,可以手动修改。半自动化操作………… -_-
本次的表和关系可以参考上篇。
1 @Test 2 public void test1() { 3 SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); 4 Session session = sessionFactory.openSession(); 5 6 session.beginTransaction(); 7 User user = (User) session.get(User.class,"40ec81e856a45e190156a45e1b600000"); 8 System.out.println(user.getGender()); 9 Set<UserAndRole> userAndRoles =user.getUserAndRoles(); 10 Iterator it = userAndRoles.iterator(); 11 while(it.hasNext()){ 12 UserAndRole uar = (UserAndRole) it.next(); 13 Role role = uar.getRole(); 14 System.out.println(role.getName()); 15 } 16 session.getTransaction().commit(); 17 } 18 @Test 19 public void test2(){ 20 SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); 21 Session session = sessionFactory.openSession(); 22 String hql = "from User user where user.username='reader1'"; 23 Query q = session.createQuery(hql); 24 System.out.println("Query"); 25 List<User> users = q.list(); 26 for(User u : users){ 27 String psd = u.getPassword(); 28 System.out.println(psd); 29 } 30 session.close(); 31 sessionFactory.close(); 32 } 33