Hibernate-基本建立步骤(创建一个表)
来源:互联网 发布:目前游戏的编程语言 编辑:程序博客网 时间:2024/05/19 04:02
项目结构:
(1)新建项目
(2)导入jar包
(3)创建实体对象User.java
/* *@Author swxctx *@time 2016年9月12日 */package com.sw.hibernate.domain;import java.util.Date;public class User {private int id;private String name;private Date birthday;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}}
(4)创建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 package="com.sw.hibernate.domain"><class name="User"><id name="id"><generator class="native"/></id><property name="name"/><property name="birthday"/></class></hibernate-mapping>
(5)创建hibernate.cfg.xml文件
<!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="dialect">org.hibernate.dialect.MySQLDialect</property><!-- 数据库url --><property name="connection.url">jdbc:mysql:///hibernate</property><!-- 数据库用户名 --><property name="connection.username">root</property><!-- 用户密码 --><property name="connection.password">****</property><!-- 创建表 --><property name="hbm2ddl.auto">create</property><!-- 映射文件位置 --><mapping resource="com/sw/hibernate/domain/User.hbm.xml"/></session-factory></hibernate-configuration>
(6)创建测试类
/* *@Author swxctx *@time 2016年9月12日 */package com.sw.hibernate.test;import java.util.Date;import javax.transaction.HeuristicMixedException;import javax.transaction.HeuristicRollbackException;import javax.transaction.RollbackException;import javax.transaction.SystemException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import com.sw.hibernate.domain.User;public class Base {public static void main(String[] args) {// TODO Auto-generated method stub//需要进行一次初始化Configuration cfg=new Configuration();cfg.configure();//初始化后得到sessionfactorySessionFactory sf=cfg.buildSessionFactory();//类似于DriverManager//操作数据Session s=sf.openSession();//等价于connectionTransaction tx=(Transaction) s.beginTransaction();User user=new User();user.setName("name");user.setBirthday(new Date());//保存对象s.save(user);tx.commit();s.close();//关闭System.out.println("end");}}
(7)log4j.properties文件可从hibernate解压包下拷贝
0 0
- Hibernate-基本建立步骤(创建一个表)
- gulp创建一个文件基本步骤
- Hibernate环境搭建(一个基本的Hibernate应用的实现步骤)
- PHP中建立一个CURL的基本步骤
- XCode 4 中建立一个Empty Application步骤(创建并关联xib视图文件)
- Hibernate基本步骤
- Hibernate基本开发步骤
- hibernate 基本步骤
- Hibernate开发基本步骤
- Hibernate开发基本步骤
- 建立一个web项目的步骤之一(建立数据库)
- hibernate学习笔记(1)hibernate基本步骤
- 使用Hibernate的基本步骤
- hibernate的基本使用步骤
- hibernate配置二级缓存基本步骤
- Service Broker的基本概念及建立一个Service Broker应用程序的基本步骤
- Hibernate 基本创建
- Hibernate基本框架创建
- Android ScrollView 滚动监听
- thinking in java test chapter9接口(1)~(10)
- Green-Ampt Infiltration Model
- java web中图片验证码功能实现
- ReactNative常用命令行
- Hibernate-基本建立步骤(创建一个表)
- sql sql*plus
- weblogic/c3p0配置mysql数据源出现乱码问题
- iOS开发:电池电量监测
- 第 5章 Jenkins – Maven Setup
- leetcode 38. Count and Say
- 游戏开发入门03
- Java web图片验证功能实现二
- iscsi endless conn error investigation