hibernate 增删改查小测试
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate 增删改查小测试相关的知识,希望对你有一定的参考价值。
好久没有敲过了,来熟悉下hibernate,做了个小测试。
测试项目结构图,关于导入jar包,可以参考 http://zhuws.blog.51cto.com/11134439/1932652
2.hibernate.cfg.xml 配置
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="hibernate.connection.username">ssh</property> <property name="hibernate.connection.password">ssh</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:xe</property> <!-- 方言 --> <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- 可视化sql --> <property name="hibernate.show_sql">true</property> <!-- 格式化sql,自动对可视化sql换行,对齐 --> <property name="hibernate.format_sql">true</property> <!-- 为getCurrentSession添加配置,保证事务中公用义个session --> <property name="current_session_context_class">thread</property> <!-- 映射文件配置 --> <mapping resource="User.hbm.xml"/> </session-factory> </hibernate-configuration>
3.User.hbm.xml配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping> <!-- name:是实体 table是表名称 --> <class name="com.hib.entity.Users" table="TEST_USER"> <id name="id"> <!-- 主键自增策略,hibernate提供了几种,这里测试,先写一个,后面会补充 --> <!-- increment:主键自增策略 --> <generator class="increment"></generator> </id> <!-- name:是实体中属性的名称,column:是表中字段的名称,type是数据类型 在这里发现后面的 pass和 address 没有写column和type 如果表字段和实体属性名称一致,可以省略不写 --> <property name="name" column="name" type="java.lang.String"/> <property name="pass"/> <property name="address"/> </class> </hibernate-mapping>
4. 实体类Users
package com.hib.entity; public class Users { private Integer id; private String name; private String pass; private String address; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Users(Integer id, String name, String pass, String address) { super(); this.id = id; this.name = name; this.pass = pass; this.address = address; } public Users() { super(); } @Override public String toString() { return "Users [id=" + id + ", name=" + name + ", pass=" + pass + ", address=" + address + "]"; } }
5. 建表,这里只是测试,所以类型长度就不要在意了。
6.userDao
package com.hib.userDao; import com.hib.entity.Users; public interface UserDao { // 根据用户id查询用户信息 public Users queryUserById(Integer id); // 插入用户 public void insertUser(Users user); // 删除用户 public void deleteUser(Integer id); // 更新用户数据 public void updateUser(Users user); }
7.userDaoImpl
package com.hib.userDao.impl; import org.hibernate.Session; import com.hib.entity.Users; import com.hib.userDao.UserDao; import com.hib.util.HibUtil; public class UserDaoImpl implements UserDao{ // 根据用户ID查询 @Override public Users queryUserById(Integer id) { Session session = HibUtil.getSession(); Users users = session.get(Users.class, id); return users; } // 插入数据 @Override public void insertUser(Users user) { Session session = HibUtil.getSession(); session.save(user); } // 根据ID 删除 @Override public void deleteUser(Integer id) { Session session = HibUtil.getSession(); Users users = session.get(Users.class, id); session.delete(users); } // 更新 @Override public void updateUser(Users user) { Session session = HibUtil.getSession(); session.update(user); } }
8.userService
package com.hib.services; import com.hib.entity.Users; public interface UserServices { // 根据用户id查询用户信息 public Users queryUserById(Integer id); // 插入用户 public void insertUser(Users user); // 删除用户 public void deleteUser(Integer id); // 更新用户数据 public void updateUser(Users user); }
9.userServiceImpl
package com.hib.services.impl; import org.hibernate.Session; import org.hibernate.Transaction; import com.hib.entity.Users; import com.hib.services.UserServices; import com.hib.userDao.UserDao; import com.hib.userDao.impl.UserDaoImpl; import com.hib.util.HibUtil; public class UserServicesImpl implements UserServices{ private UserDao ud = new UserDaoImpl(); @Override public Users queryUserById(Integer id) { Session session = null; Transaction tx = null; Users user = null; try { session = HibUtil.getSession(); tx = session.beginTransaction(); user = ud.queryUserById(id); tx.commit(); } catch (Exception e) { tx.rollback(); throw new RuntimeException("查询出错!"); } return user; } @Override public void insertUser(Users user) { Session session = null; Transaction tx = null; try { session = HibUtil.getSession(); tx = session.beginTransaction(); ud.insertUser(user); tx.commit(); } catch (Exception e) { tx.rollback(); throw new RuntimeException("插入错误!"); } } @Override public void deleteUser(Integer id) { Session session = null; Transaction tx = null; try { session = HibUtil.getSession(); tx = session.beginTransaction(); ud.deleteUser(id); tx.commit(); } catch (Exception e) { tx.rollback(); throw new RuntimeException("删除错误!"); } } @Override public void updateUser(Users user) { Session session = null; Transaction tx = null; try { session = HibUtil.getSession(); tx = session.beginTransaction(); ud.updateUser(user); tx.commit(); } catch (Exception e) { tx.rollback(); throw new RuntimeException("更新错误!"); } } }
10.util 工具类
package com.hib.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; /** * * @author admin * 生产session */ public class HibUtil { private static Configuration cfg = new Configuration().configure(); private static SessionFactory factory = cfg.buildSessionFactory(); public static Session getSession(){ return factory.getCurrentSession(); } public static Session openSession(){ return factory.openSession(); } }
11.测试
package com.hib.test; import org.junit.Test; import com.hib.entity.Users; import com.hib.services.UserServices; import com.hib.services.impl.UserServicesImpl; public class TestUser { private UserServices us = new UserServicesImpl(); // 插入数据 @Test public void insert(){ us.insertUser(new Users(null, "小李飞刀", "xlfd", "china")); } // 测试查询 @Test public void query(){ Users users = us.queryUserById(1); System.out.println(users); } // 更新 @Test public void update(){ us.updateUser(new Users(1, "小李飞刀他妈", "xlfdtm", "AAAAA")); } // 删除 @Test public void delete(){ us.deleteUser(1); } }
12. dao 是和数据打交道
service 是控制业务,事务,实际开发业务就是在这里写
注意: 这里用的数据库是oracle,测试时候,需要在hibernate.cfg.xml 中把数据库名称和密码修改为自己的。
本文出自 “永恒之光” 博客,请务必保留此出处http://zhuws.blog.51cto.com/11134439/1933294
以上是关于hibernate 增删改查小测试的主要内容,如果未能解决你的问题,请参考以下文章