Hibernate用注解生成表

User.java实体来

package com.tao.pojo;
import javax.persistence.Column;
//用注解的方式生成表
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
@Entity        //标明当前类是受Hibernate管理的,相当于映射了一个.XML文件    
@Table(name="User")    //    表名和类名不一致的时候用,table是表名,当前类对应表的名字
public class User {
    //主键
    @Id
//    自动增长
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    //column指数据库表中的普通列(不是主键或外键的列),  name里面写列名,当列名和属性名相同的时候,column可以不写,nullable=false 不为空
    @Column(name="name1",length=200,nullable=false)
    private String name;
    private Double money;
    
    public User() {
        super();
    }
    public User(int id, String name, Double money) {
        super();
        this.id = id;
        this.name = name;
        this.money = money;
    }
    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 Double getMoney() {
        return money;
    }
    public void setMoney(Double money) {
        this.money = money;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", money=" + money + "]";
    }
}

hibernate.cfg.xml配置文件

<?xml version='1.0' encoding='utf-8'?>
<!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>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test1123?characterEncoding=utf-8</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <!-- 如果没有就创建,有就更新 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
 
        <mapping class="com.tao.pojo.User"/>
        
    </session-factory>

</hibernate-configuration>

Test001.java测试类
package com.tao.test;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.tao.pojo.User;

public class Test001 {
    public static void main(String[] args) {
        Configuration config = new Configuration().configure();
        SessionFactory factory = config.buildSessionFactory();
        Session session = factory.openSession();
        session.beginTransaction();
//        设置了自动增长之后,这里的Id不起作用,可以不写,按照1,2,3,4,5,的顺序走,如果把,5删了,下一个是6,
        //在创建表的时候先把save屏蔽掉,
//        User us = new User(32, "啊哈", 32.0);
//        session.save(us);        
        session.getTransaction().commit();
        session.close();
        factory.close();        
    }
}

 

posted @ 2018-01-08 20:15  理想的诱惑  阅读(255)  评论(0编辑  收藏  举报