Hibernate Annotation 生成数据库表(UUId)
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="uu") // 表名和类名不一致的时候用,table是表名,当前类对应表的名字 public class User { //主键 @Id //自动增长(uuid是String类型的,所以id是String类型的) @GenericGenerator(name="tt",strategy="uuid" ) @GeneratedValue(generator="tt") // 让Id自动增长 @Column(name="id",length=32) private String id; //column指数据库表中的普通列(不是主键或外键的列), name里面写列名,当列名和属性名相同的时候,column可以不写,nullable=false 不为空 @Column(name="name",length=200,nullable=false) private String name; private Double money; public User() { super(); } public User(String id, String name, Double money) { super(); this.id = id; this.name = name; this.money = money; } 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 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屏蔽掉(表存在了再添加数据 ) // name是String类型,字符串是utf8,不然就报错 // 如果添加的时候报name列的错,那就去数据库修改name列的字符编码为utf8 User us = new User("12", "可可", 123.032); session.save(us); session.getTransaction().commit(); session.close(); factory.close(); } }
欢迎您的评论与补充