在Eclipse搭建Hibernate框架
来源:互联网 发布:杭州sql培训班 编辑:程序博客网 时间:2024/06/06 01:19
今天说下在Eclipse中搭建Hibernate框架
第一步:安装Hibernate插件,有两种方式,一种是在线的,一种是离线,我在上一篇博客详细的写了,
博客地址是:http://blog.csdn.net/name_fay/article/details/77456045,我比较建议在线安装Hibernate插件
第二步:导入相应的jar文件
我用的maven,jar文件有这些,这是我用的
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 加载servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b07</version>
<scope>provided</scope>
</dependency>
<!-- Hibernate依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- 加载mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
第三步:创建Hibernate配置文件:hibernate.cfg.xml
1.先创建一个hibernate.cfg.xml文件
创建好之后先在配置下,打开Eclipse之后,Window-->preferences-->Editors-->File Associations
<4>创建实体类(和数据库表相对应)还有创建映射文件:*hbm.xml,我这里就一起了哈,省事
我这里就贴下User.hbm.xml和hibernate.cfg.xml的代码
User.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-8-22 10:37:16 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.zking.entity.User" table="USER">
<id name="sid" type="int">
<column name="SID" />
<generator class="assigned" />
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME" />
</property>
<property name="ssex" type="java.lang.String">
<column name="SSEX" />
</property>
<property name="sage" type="int">
<column name="SAGE" />
</property>
</class>
</hibernate-mapping>
这些列名的排序要和数据库的排序一致哈
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- session工程 这里的session等于Connection-->
<session-factory>
<!-- 配置数据库的连接信息 -->
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="connection.url">jdbc:mysql://localhost:3306/student</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 显示SQL语句,show_sql为true就会展示所有的SQL语句 -->
<property name="show_sql">true</property>
<!-- 关联映射文件 -->
<mapping resource="com/zking/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
最后就是访问数据库的操作:
1.加载Hibernate配置文件:Configuration configuration=new Configuration().configure();
2.创建SessionFactory:SessionFactory sessionFactory=configuration。buildSessionFactory();
3.创建Session:Session session=sessionFactory.openSession();
4.开启事务:Transaction transaction=session.beginTransaction();
5.执行数据库操作(简单的数据库操作)
package com.zking.test;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.zking.entity.Student;
public class TestCRUD {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void before(){
//1.读取hibernate配置文件
Configuration configuration=new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
@After
public void after(){
//提交事务
transaction.commit();
session.close();
sessionFactory.close();
}
@Test
public void testAdd(){
Student student=new Student("哥哥", 18);
session.save(student);
}
@Test
public void testGet(){
//查询单个
// Student student=session.get(Student.class, 1);
// System.out.println("查询单个:"+student.getSid()+" "+student.getSname());
//查询所有
List<Student> students=session.createCriteria(Student.class).list();
for (Student student : students) {
System.out.println(student.getSname());
}
}
}
6.提交事务:transaction.commit();
7.关闭Session
8.关闭SessionFactory
这就是在Eclipse开始用Hibernate框架了
- 在Eclipse搭建Hibernate框架
- 在Eclipse下搭建Hibernate框架
- Eclipse搭建Hibernate框架
- eclipse搭建Hibernate框架
- 练手在eclipse中搭建spring+struts+hibernate开发框架
- Eclipse在线搭建Hibernate框架
- 在Eclipse上搭建SSH框架(Struts 2.5.1 + Spring 4.3.0 + Hibernate 4.3.11)
- Eclipse搭建Hibernate逆向工程 搭建SSH了框架
- 在项目中搭建Hibernate框架步骤
- 在eclipse中搭建hibernate开发环境
- 在eclipse中快速搭建SSH框架
- 在eclipse中搭建SSH框架
- 在eclipse 上搭建ssh框架
- 在Eclipse下手动搭建SSH框架,并使用Hibernate插件反向生成实体类和关系映射文件。
- 用eclipse搭建ssh框架 struts spring hibernate
- Eclipse搭建SSH(Struts2+Spring+Hibernate)框架教程
- 基于Eclipse搭建SSH框架:第三篇 整合Hibernate
- eclipse搭建SSH框架(Spring+SpringMVC+Hibernate)
- 剑指Offer [13] 调整数组顺序使奇数位于偶数前面
- ES性能调优权威指南(篇三)
- Eclipse如何安装插件
- 深入浅出HTTP
- Leetcode#21. Merge Two Sorted Lists (归并排序之单链表)
- 在Eclipse搭建Hibernate框架
- PHP中的scalar
- 658. Find K Closest Elements(快排+折半查找)
- float和double的精度和字节数各是多少
- 闲谈高可用与负载均衡
- JAVA企业面试题精选 Hibernate 11-20
- [PAT]1002. A+B for Polynomials (25)
- zboj 2301 Problem b (莫比乌斯反演)
- 性能测试之linux