hibernate (二) 实现简单的CRUD
实体类:
public class User { private int id; private String username; private String password; private String nickname; private Date born; 省略get,set方法 }
User.hbm.xml
<hibernate-mapping package="org.th.model"> <class name="User" table="t_user" > <id name="id"> <!-- native是oracle和mysql合体(或者说是选择器):当使用oracle是会选择sequence,mysql时会选择increment --> <generator class="native"></generator> </id> <property name="username"></property> <property name="password"></property> <property name="nickname"></property> <property name="born" type="timestamp"></property> </class> </hibernate-mapping>
hibernate.cfg.xml
<session-factory> <!-- hibernate的方言,用来确定连接的数据库 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 数据库驱动类 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 数据库的连接字符串和用户名密码 --> <property name="connection.url">jdbc:mysql://localhost:3306/test_hibernate</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!--在使用hibernate,会现在sql语句 --> <property name="show_sql">true</property> <!-- 自动完成类到数据表的转换 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 加入实体类的映射文件 --> <mapping resource="org/th/model/User.hbm.xml"/> </session-factory>
测试类TestCRUD :
public class TestCRUD { private static final SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-ddd"); @Test public void testAdd() { Session session = null; try { session = HibernateUtil.openSession(); session.beginTransaction(); User user = new User(); user.setBorn(format.parse("1978-12-21")); user.setUsername("李四"); user.setPassword("123"); user.setNickname("lisi"); session.save(user); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); if (session != null) session.getTransaction().rollback(); } finally { if (session != null) HibernateUtil.close(session); } } @Test public void testLoad() { Session session = null; try { session = HibernateUtil.openSession(); User user = (User) session.load(User.class, 1); System.out.println(user); } catch (Exception e) { e.printStackTrace(); } finally { if (session != null) HibernateUtil.close(session); } } @Test public void testUpdate() { Session session = null; try { session = HibernateUtil.openSession(); session.beginTransaction(); User user = (User) session.load(User.class, 2); user.setUsername("小黑哥"); session.update(user); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); if (session != null) session.getTransaction().rollback(); } finally { if (session != null) HibernateUtil.close(session); } } @Test public void testDelete() { Session session = null; try { session = HibernateUtil.openSession(); session.beginTransaction(); // User user=(User)session.load(User.class, 2); User user = new User(); user.setId(3); session.delete(user); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); if (session != null) session.getTransaction().rollback(); } finally { if (session != null) HibernateUtil.close(session); } } /** * * @Title: testList * @Description: 通过hibernate 的hql语句查询 返回list集合 * @param * @return void 返回类型 * @throws */ @Test public void testList() { Session session = null; try { session = HibernateUtil.openSession(); List<User> list = session.createQuery("from User").list(); for (User user : list) { System.out.println(user); } } catch (Exception e) { e.printStackTrace(); } finally { if (session != null) HibernateUtil.close(session); } } /** * * @Title: testPaging * @Description: 通过hibernate 的hql语句查询 返回 分页之后 list集合, * @param * @return void 返回类型 * @throws */ @Test public void testPaging() { Session session = null; try { session = HibernateUtil.openSession(); List<User> list = session.createQuery("from User") .setFirstResult(2).setMaxResults(2).list(); for (User user : list) { System.out.println(user); } } catch (Exception e) { e.printStackTrace(); } finally { if (session != null) HibernateUtil.close(session); } } }