在eclipse中搭建hibernate开发环境
来源:互联网 发布:专业医学翻译软件 编辑:程序博客网 时间:2024/06/05 20:58
1.先准备好需要的jar包
2.导入需要的hibernate包
3.在src目录下编写hibernate.cfg.xml配置文件
hibernate中最重要的操作都是通过session对象来完成的,在hibernate.cfg.xml配置文件中主要配置session-factory和实体类映射文件的位置,我用的是mysql数据库,其他数据库的配置大同小异。
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql://localhost:port/database?useUnicode=true&characterEncoding=UTF-8</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">password</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><property name="hibernate.hbm2ddl.auto">update</property><mapping resource="haotu/bean/User.hbm.xml"/><!-- User.hbm.xml是实体类的映射文件 --!></session-factory></hibernate-configuration>4.创建实体类和实体类的映射文件
实体类:User.java
package haotu.bean;public class User {public Integer userId;public String userName;public String userPassword;public Integer userInfoId;public String userIp;public Integer getUserId() {return userId;}public void setUserId(Integer userId) {this.userId = userId;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getUserPassword() {return userPassword;}public void setUserPassword(String userPassword) {this.userPassword = userPassword;}public Integer getUserInfoId() {return userInfoId;}public void setUserInfoId(Integer userInfoId) {this.userInfoId = userInfoId;}public String getUserIp() {return userIp;}public void setUserIp(String userIp) {this.userIp = userIp;}}映射文件: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 package="haotu.bean"> <class name="User" table="User"> <id name="userId" column="userId" type="java.lang.Integer"> <generator class="native"/><!-- 设置userId为主键 --!> </id> <property name="userName" column="userName" type="java.lang.String"/> <property name="userPassword" column="userPassword" type="java.lang.String"/> <property name="userInfoId" column="userInfoId" type="java.lang.Integer"/> <property name="userIp" column="userIp" type="java.lang.String"/> </class> </hibernate-mapping>5.配置到这一步hibernate的环境其实已经搭建好了,现在来测试一番
5.1 因为hibernate中操作大多是由session对象来完成的,所以我们在对数据的各种操作之前需要得到一个session对象。session对象又是依靠前面配置的session-factory来生成的。在这里我自己写了一个MySessionFactory来读取配置文件并生成一个session对象。
package haotu.dao;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class MySessionFactory {public static Session getSession(){Configuration cfg = new Configuration().configure("hibernate.cfg.xml");SessionFactory factory = cfg.buildSessionFactory();return factory.openSession();}}5.2 通过这个MySessionFactory我们可以得到一个session对象,然后就可以通过这个sesion对象来进行具体的操作了
比如我们写一个UserDao,其中实现User对象的添加和查找
package haotu.dao;import haotu.bean.User;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.Transaction;public class UserDao extends MySessionFactory{/** * 保存一个User对象 * */public boolean save(User user){Session session = getSession();<span style="font-family: Arial, Helvetica, sans-serif;">//这里继承了MySessionFactory类的getSession方法</span>if(session!=null&&user!=null){Transaction tr = session.beginTransaction();try{session.save(user);tr.commit();return true;}catch(Exception e){e.printStackTrace();tr.rollback();return false;}finally{session.close();}}return false;}/** * 根据User的userName来查找User * */public User selectByName(String userName){Session session = getSession();if(session!=null&&!userName.equals("")){String hql = "from User where userName=?";try{Query query = session.createQuery(hql);query.setParameter(0, userName);List<User> list = query.list();if(list!=null&&list.size()>0)return list.get(0);}catch(Exception e){e.printStackTrace();return null;}finally{session.close();}}return null;}}
5.3 调用这个UserDao
package haotu.test;import haotu.bean.User;import haotu.dao.UserDao;public class Test {public static void main(String[] args){UserDao userDao = new UserDao();User u = new User();u.setUserInfoId(3);u.setUserName("Hibernate");u.setUserPassword("pass");u.setUserIp("10.0.2.2");System.out.println(userDao.save(u));System.out.println(userDao.selectByName("Hibernate"));}}输出结果: true
haotu.bean.User@5136d012
6.hibernate环境搭建成功
1 0
- 在eclipse中搭建hibernate开发环境
- 在Eclipse中搭建Python开发环境
- 在Eclipse中搭建Python开发环境
- 在ubuntu中搭建eclipse开发环境。
- 在eclipse开发环境中搭建struts2
- 在Eclipse中搭建Python开发环境
- 在eclipse中搭建struts2开发环境
- 练手在eclipse中搭建spring+struts+hibernate开发框架
- 手把手在MyEclipse中搭建Hibernate开发环境
- 在eclipse中搭建cocos2d-x的android开发环境
- 在Eclipse/MyEclipse中搭建Android开发环境
- 在Eclipse中开发OSGi(1)搭建Felix环境
- 在eclipse中android NDK开发环境的搭建
- 在eclipse中android NDK开发环境的搭建
- 在Eclipse中搭建ArcGIS API for JavaScript 开发环境
- Eclipse下搭建struts、spring、hibernate开发环境 搭建环境
- eclipse hibernate环境搭建
- 在Eclipse搭建(Nokia)J2ME开发环境
- 解决ST-LINK进行硬件调试时,进不了main()函数的问题
- 虚拟机相关知识,以及myeclipse内存调优的实战
- Java虚拟机(一)——Java内存区域划分
- 欢迎使用CSDN-markdown编辑器
- JDK 自带调试工具以及用法
- 在eclipse中搭建hibernate开发环境
- HTML+CSS基础 border-top在指定p标签的文本上方添加双细实线
- DELL_LATITUDE_E5440安装ubuntu12.04不能上网问题的解决方案
- 相对路径和绝对路径
- SAP QM BAPI : Main BAPI in SAP Quality Management
- strlen、sizeof
- 子类创建对象
- Servlet 实现目标
- java中创建对象的几种方法