使用Eclipse编写第一个Hibernate程序
2017-07-19 14:53
591 查看
本次最终目的是使用Hibernate保存一条数据到数据库中
学习地址:http://www.imooc.com/course/programdetail/pid/48
新建一个Java Project导入Hibernate、Junit、mysql jar包
创建Hibernate的配置文件,new -> others -> Hibernate下cfg.xml文件
创建持久化类,生成get、set方法。
`
/**
遵循javabean原则
*/
public class Students {
private int sid; // 学号
private String sname; // 名字
private String gender;
private Date birthday;
private String address;
`
创建对象-关系映射文件,生成Students.hbm.xml文件。生成方法类似配置文件那样
通过HibernateAPI编写访问数据库的代码
public class StudentsTest {
// 导入Hibernate中jar包
private Session session;
private SessionFactory sessionFactory;
private Transaction transaction; `
@Before
public void init(){
// 创建配置对象
Configuration config = new Configuration().configure();
// 创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
}
@After
public void destory(){
// 提交事务
transaction.commit();
// 关闭session
session.close();
// 关闭工厂会话
sessionFactory.close();
}
@Test
public void testSaveStudents(){
Students students = new Students(1, “zhangsan”, “男”, new Date(), “wudangshan”);
// 保存数据库中
session.save(students);
}
}
7.Junit启动
注意jar包版本:
学习地址:http://www.imooc.com/course/programdetail/pid/48
为什么使用Hibernate
编写大量的SQL语句有诸多缺陷:语法不同、实现方式不同、移植和维护麻烦。抛弃sql思想Hibernate就是一种ORM(Object / Relationship Mapping)框架技术。下图是Hibernate在程序中所处位置:编写第一个Hibernate程序
help -> Install New SoftWare ->粘贴【http://download.jboss.org/jbosstools/neon/stable/updates/】->点击add然后等等->选择Jboss Web and Java EE Development目录下的Hibernate Tools新建一个Java Project导入Hibernate、Junit、mysql jar包
创建Hibernate的配置文件,new -> others -> Hibernate下cfg.xml文件
<session-factory> <property name="connection.username">root</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url"><![CDATA[jdbc:mysql://127.0.0.1:3306/hibernate?useUnicode=true&characterEncoding=UTF-8]]></property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping resource="Students.hbm.xml"/>
创建持久化类,生成get、set方法。
`
/**
遵循javabean原则
*/
public class Students {
private int sid; // 学号
private String sname; // 名字
private String gender;
private Date birthday;
private String address;
`
创建对象-关系映射文件,生成Students.hbm.xml文件。生成方法类似配置文件那样
通过HibernateAPI编写访问数据库的代码
public class StudentsTest {
// 导入Hibernate中jar包
private Session session;
private SessionFactory sessionFactory;
private Transaction transaction; `
@Before
public void init(){
// 创建配置对象
Configuration config = new Configuration().configure();
// 创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
// 创建会话工厂对象 sessionFactory = config.buildSessionFactory(serviceRegistry); // 会话对象 session = sessionFactory.openSession(); // 开启事务 transaction = session.beginTransaction();
}
@After
public void destory(){
// 提交事务
transaction.commit();
// 关闭session
session.close();
// 关闭工厂会话
sessionFactory.close();
}
@Test
public void testSaveStudents(){
Students students = new Students(1, “zhangsan”, “男”, new Date(), “wudangshan”);
// 保存数据库中
session.save(students);
}
}
7.Junit启动
注意jar包版本:
相关文章推荐
- 搭建java web开发环境、使用eclipse编写第一个java web程序
- 编写你的第一个Hibernate程序 (在Hibernate中使用oracle的sequence产生主键 )
- 简单使用Eclipse编写第一个Java程序
- 如何使用eclipse编写第一个jQuery程序实例
- 使用eclipse编写hibernate程序 (alluser的专栏)
- 简单使用Eclipse编写第一个Java程序
- 搭建java开发环境、使用eclipse编写第一个java程序
- 使用Eclipse编写第一个“Hello World!”程序
- 编写你的第一个Hibernate程序 (在Hibernate中使用oracle的sequence产生主键 )
- 简单使用Eclipse编写第一个Java程序
- eclipse + JBoss 5 + EJB3开发指南(6):编写第一个实体Bean程序
- ubuntu下使用Eclipse CDT插件编写多线程程序
- 编写你的第一个Hibernate程序
- ubuntu下使用eclipse调用libpcap库编写一个小程序时出的错误:pcap.h没有参与编译
- 【嵌入式linux】(第六步):使用eclipse集成开发环境开发第一个嵌入式Linux程序,并测试LED驱动
- 使用XFire编写第一个javaee上的web service程序
- 使用emacs编写第一个hello的C程序
- 纯手工编写第一个Hibernate程序 推荐
- Linux下使用Eclipse编写MapReduce程序的配置
- Windows下使用Eclipse+CDT编写C/C++程序