hibernate搭建入门(新手必看)
2017-07-25 18:15
351 查看
新手搭建hibernate 首次成功。
在这里分享给大家。
1.我这里是一个web project项目,首先把hibernate所用的核心包导入项目WEB-INF下的lib文件夹里,下载hibernate核心包。
2.演示的项目所用数据库为sql server2008。 jdbc驱动包在这里就不多说了,接着往下看。
3.上图为hibernate核心配置文件 hibernate.cfg.xml (详细配置如下,针对于sql server数据库配置)
4.新建Users实体类
5.提供Users.hbm.xml文件,完成实体类的映射
其中property字段标签是将要生成是数据库表中的字段,在这里不用关心各个字段是什么类型的。因为Hibernate会根据上面的实体类中属性的类型来决定将来表中字段的类型。
6.配置hibernate.cfg.xml文件 完成映射
7.编写测试类main方法 完成自动建表
到这里就可以生成Users表了。
--向表中添加数据
在这里分享给大家。
1.我这里是一个web project项目,首先把hibernate所用的核心包导入项目WEB-INF下的lib文件夹里,下载hibernate核心包。
2.演示的项目所用数据库为sql server2008。 jdbc驱动包在这里就不多说了,接着往下看。
3.上图为hibernate核心配置文件 hibernate.cfg.xml (详细配置如下,针对于sql server数据库配置)
<!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.microsoft.sqlserver.jdbc.SQLServerDriver</property> <!-- 数据库名称 --> <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=HibernateTest</property> <!-- 数据库账号 --> <property name="hibernate.connection.username">sa</property> <!-- 数据库密码 --> <property name="hibernate.connection.password">123456</property> <!-- hibernate数据库方言 --> <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property> </session-factory> </hibernate-configuration>
4.新建Users实体类
package com.entity; public class Users { private String id; private String username; private String password; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
5.提供Users.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> <class name="com.entity.Users"> <id name="id"> <generator class="uuid"></generator> </id> <property name="username"></property> <property name="password"></property> </class> </hibernate-mapping>
其中property字段标签是将要生成是数据库表中的字段,在这里不用关心各个字段是什么类型的。因为Hibernate会根据上面的实体类中属性的类型来决定将来表中字段的类型。
6.配置hibernate.cfg.xml文件 完成映射
<!DOCTYPE hibernate-configuration PUBLIC "-//Hiber 4000 nate/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.microsoft.sqlserver.jdbc.SQLServerDriver</property> <!-- 数据库名称 --> <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=HibernateTest</property> <!-- 数据库账号 --> <property name="hibernate.connection.username">sa</property> <!-- 数据库密码 --> <property name="hibernate.connection.password">123456</property> <!-- hibernate数据库方言 --> <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property> <mapping resource="com/entity/xml/User.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
7.编写测试类main方法 完成自动建表
public static void main(String[] args) { //默认读取hibernate.cfg.xml文件 Configuration cfr = new Configuration().configure(); SchemaExport export = new SchemaExport(cfr); export.create(true, true); }
到这里就可以生成Users表了。
--向表中添加数据
package com.cs; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; import com.entity.Users; public class cs { /** * @param args */ public static void main(String[] args) { Configuration cfr = new Configuration().configure(); SessionFactory factory=cfr.buildSessionFactory(); Session session=null; SchemaExport export=new SchemaExport(cfr); export.create(true, true); try { session=factory.openSession(); session.beginTransaction(); Users u=new Users(); u.setUsername("测试1"); u.setPassword("123456"); Users u1=new Users(); u1.setUsername("测试2"); u1.setPassword("654321"); session.save(u); session.save(u1); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally{ if (session!=null) { if (session.isOpen()) { session.close(); } } } } }
相关文章推荐
- vue2.0 新手入门 — 从环境搭建到发布以及demo讲解
- javascript入门之string对象【新手必看】
- Hibernate环境搭建基础入门
- ui设计入门新手必看:8招教你玩转u…
- Hibernate快速入门—新手Hibernate详细配置步骤介绍
- Leap Motion新手入门必看教程
- struct hibernate spring 经典框架 搭建 入门实例
- Hibernate入门BLOG [一、搭建hibernate的开发环境和测试案例]
- Vue2.0 新手入门 — 从环境搭建到发布
- 【Hibernate】入门搭建之XML方式
- echarts入门新手必看
- hibernate入门及环境搭建
- 新手必看 ASP.NET编程入门随想之信客
- Hibernate入门-搭建框架实现基本的增删改查
- java 新手入门必看的30题
- Robotium环境搭建与新手入门教程
- PHP常用特殊运算符号和函数总结(php新手入门必看)
- iPhone入门知识普及(新手必看)
- Android开发环境搭建【超详细-Android初学者入门必看】
- Robotium环境搭建与新手入门教程