hibernate正向生成简单教程
来源:互联网 发布:vb中对象的属性 编辑:程序博客网 时间:2024/05/20 13:17
所谓正向生成就是通过实体类生成表。
本教程以一个实体类为例演示正向生成的方法。
实体类User.java
package tm.change.domain;public class User {private String id;private String name;private String phone;private String email;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + ", phone=" + phone + ", email=" + email + "]";}}
实体类相应的映射文件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-2-16 14:56:38 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping> <class name="tm.change.domain.User" table="USERS"> <id name="id" type="java.lang.String"> <column name="ID" /> <generator class="assigned" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" not-null="true" length="20"/> </property> <property name="phone" type="java.lang.String"> <column name="PHONE" length="20"/> </property> <property name="email" type="java.lang.String"> <column name="EMAIL" length="20"/> </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://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.password">root</property> <property name="hibernate.connection.url">jdbc:mysql://localhost/goods</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.show_sql">true</property> <!-- 面试题:create update 表已经存在时对表进行更新--> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 资源配置 --> <mapping resource="tm/change/domain/User.hbm.xml"/> </session-factory></hibernate-configuration>
用一个测试类来测试一下吧
Test.java
package tm.change.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.boot.MetadataSources;import org.hibernate.boot.registry.StandardServiceRegistry;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import tm.change.domain.User;public class Test {@org.junit.Testpublic void saveTest() {// 1.加载配置文件 hibernate.cfg.xmlStandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure() // configures settings from hibernate.cfg.xml.build();try {// 2.SessionFactory工厂对象SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();System.out.println(sessionFactory);} catch (Exception e) {e.printStackTrace();// The registry would be destroyed by the SessionFactory, but we had// trouble building the SessionFactory// so destroy it manually.StandardServiceRegistryBuilder.destroy(registry);}}}
如果你的配置没有错误的话,数据库中应该已经生成表了。
0 0
- hibernate正向生成简单教程
- hibernate正向生成数据库
- hibernate正向工程生成数据库
- hibernate正向工程生成数据库
- Hibernate正向工程生成oracle数据库表,
- hibernate正向生成数据库表以及配置——hibernate.cfg.xml
- Hibernate正向工程hbm2ddl
- Hibernate正向工程hbm2ddl
- Hibernate正向工程实例
- hibernate的正向使用
- Hibernate正向工程
- ssh框架 使用spring的注解以及hibernate的正向生成
- hibernate正向生成数据库表以及配置——Student.java
- hibernate正向生成数据库表以及配置——Teacher.java
- hibernate正向生成数据库表以及配置——Student.hbm.xml
- hibernate正向生成数据库表以及配置——Teacher.hbm.xml
- hibernate正向生成数据库表以及配置——TestStu.java
- hibernate 正向和反向工程
- JavaScript模拟手势翻页动作
- PX EM REM之间的区别
- python2.7中用numpy.reshape 对图像进行切割
- USACO 3.3解题思路报告
- CodeForces-149D Coloring Brackets(区间dp)
- hibernate正向生成简单教程
- 数据库表间关系
- linux驱动由浅入深系列:驱动程序的基本结构概览之一(第一个驱动程序)
- springmvc 用拦截器+token防止重复提交
- 诺博源PHP前端学习第一天
- 跟我学STM32107RC--- STM32中的按键输入实验总结
- Webview的各种坑
- elastic5.2和kibana5.0安装配置问题简介(centos6.5)
- MySql增加用户、授权、修改密码等语句