mac下Myeclipse2015编写的第一个hibernate程序myHibernateUser及源码
来源:互联网 发布:大数据金融行业 编辑:程序博客网 时间:2024/05/21 15:38
1.新建java工程myHibernateUser
2.下载需要的hibernate jar包并引入工程
(1)下载jar包
进入下载链接:http://hibernate.org/orm/downloads/
(2)把如下jar包拷贝到工程路径下(有部分jar不在hibernate jar包中需另外下载)
(3)把jar包添加到工程
(4)添加单元测试jar包
3添加类和配置文件
(1)为工程添加实体类User,类代码如下
package com.lyh;import java.util.Date;public class User {private int id;private String username;private String sex;private Date birthday;private int age;private double salary;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}}
(2)配置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"><hibernate-mapping><class name="com.lyh.User" table="T_USER"><id name="id"><generator class="increment" /></id><property name="username" /><property name="sex"/><property name="birthday"/><property name="salary"/><property name="age"/></class></hibernate-mapping>
(3)配置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">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?createDatabaseIfNotExist=true</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">liu123</property><!-- 数据库方言 --><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><!-- 配置是否显示Hibernate生成的SQL语句 --><property name="show_sql">true</property><!-- 配置自动根据映射文件更新数据表 --><property name="hbm2ddl.auto">update</property><!-- 配置实体映射文件(或实体类) --><mapping resource="com/lyh/User.hbm.xml"/></session-factory></hibernate-configuration>
(4)编写测试类,类的内容如下
package test.lyh;import java.util.Date;import java.util.List;import com.lyh.User;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import junit.framework.TestCase;public class HibernateTest extends TestCase {// 测试五大API之,Configuration,SessionFactory,Session,Transactionpublic void test1() {User user = new User();user.setUsername("张三");user.setSex("男");user.setAge(20);user.setBirthday(new Date());user.setSalary(2000.23);// 解析hibernate.cfg.xml配置文件Configuration cfg = new Configuration().configure();// 创建SessionFactory(创建连接池)SessionFactory factory = cfg.buildSessionFactory();// 创建SessionSession session = factory.openSession();// 创建及开启事务对象Transaction trans = null;try {trans = session.beginTransaction();// 添加user实体对象session.save(user);trans.commit();} catch (HibernateException e) {trans.rollback();e.printStackTrace();} finally {if (session != null)session.close();}}// 根据ID查询public void test2() {// 解析hibernate.cfg.xml配置文件Configuration cfg = new Configuration().configure();// 创建SessionFactory(创建连接池)SessionFactory factory = cfg.buildSessionFactory();// 创建SessionSession session = factory.openSession();// 创建及开启事务对象Transaction trans = null;try {trans = session.beginTransaction();User user = (User) session.get(User.class, 1);System.out.println(user.getUsername());System.out.println(user.getBirthday());trans.commit();} catch (HibernateException e) {trans.rollback();e.printStackTrace();} finally {if (session != null)session.close();}}// 根据ID删除public void test3() {// 解析hibernate.cfg.xml配置文件Configuration cfg = new Configuration().configure();// 创建SessionFactory(创建连接池)SessionFactory factory = cfg.buildSessionFactory();// 创建SessionSession session = factory.openSession();// 创建及开启事务对象Transaction trans = null;try {trans = session.beginTransaction();User user = (User) session.get(User.class, 1);session.delete(user);trans.commit();} catch (HibernateException e) {trans.rollback();e.printStackTrace();} finally {if (session != null)session.close();}}// 根据ID更新public void test4() {// 解析hibernate.cfg.xml配置文件Configuration cfg = new Configuration().configure();// 创建SessionFactory(创建连接池)SessionFactory factory = cfg.buildSessionFactory();// 创建SessionSession session = factory.openSession();// 创建及开启事务对象Transaction trans = null;try {trans = session.beginTransaction();User user = (User) session.get(User.class, 3);user.setSex("女");user.setUsername("李四");// session.update(user);trans.commit();} catch (HibernateException e) {trans.rollback();e.printStackTrace();} finally {if (session != null)session.close();}}// 查询结果集public void test5() {// 解析hibernate.cfg.xml配置文件Configuration cfg = new Configuration().configure();// 创建SessionFactory(创建连接池)SessionFactory factory = cfg.buildSessionFactory();// 创建SessionSession session = factory.openSession();// 创建及开启事务对象Transaction trans = null;try {trans = session.beginTransaction();Query query = session.createQuery("from User");List<User> users = query.list();for(User user : users){System.out.println(user.getUsername());System.out.println(user.getBirthday());System.out.println();}trans.commit();} catch (HibernateException e) {trans.rollback();e.printStackTrace();} finally {if (session != null)session.close();}}}
4测试并查看结果
(1)进行单元测试
(2)查看数据库
5工程源码
myHibernateUser工程下载链接
0 0
- mac下Myeclipse2015编写的第一个hibernate程序myHibernateUser及源码
- 编写你的第一个Hibernate程序
- 编写你的第一个Hibernate程序
- mac下 Struts2 第一个程序的详细步骤(附带源码链接)
- 使用Eclipse编写第一个Hibernate程序
- vs2013下编写你的第一个CUDA程序
- MAC环境下Android Studio的第一个JNI程序
- Mac下的第一个C++程序—HelloWorld
- 第一个hibernate应用程序源码及详解
- Hibernate的第一个程序
- Ubuntu下编写第一个C程序
- 编写你的第一个Hibernate程序 (在Hibernate中使用oracle的sequence产生主键 )
- 编写你的第一个Hibernate程序 (在Hibernate中使用oracle的sequence产生主键 )
- 编写你的第一个Windows程序
- 自己编写的第一个程序
- 编写的第一个网络程序
- 一位菜鸟编写的第一个程序
- 第一个Java程序(HelloWorld)的编写
- Java反射机制剖析(二)-功能以及举例
- sed 简明教程
- Hash一致性算法理解
- linux--Xshell的使用
- mysql 存储过程
- mac下Myeclipse2015编写的第一个hibernate程序myHibernateUser及源码
- 静态链表实现
- Android Studio 安装
- k短路
- 使用Sass减少重复性代码
- 聚簇索引与非聚簇索引的区别
- java 垃圾回收机制
- 治标治本,彻底解决AVR单片机EEPROM数据丢失问题
- DateEdit