1,利用配置文件类生成表
1)先建一个类,get和set方法
@Data public class Salary { private Integer workcode; private String uname; private String dept; private BigInteger baseSalary; private Long extract; private Long leav; private Long sick; private Long pk; @Override public String toString() { return "Salary{" + "workcode='" + workcode + '\'' + ", uname='" + uname + '\'' + ", dept='" + dept + '\'' + ", baseSalary=" + baseSalary + ", extract=" + extract + ", leav=" + leav + ", sick=" + sick + ", pk=" + pk + '}'; }
3)配置文件
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.url">jdbc:mysql://localhost:3306/salary</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">root</property> <property name="connection.password">12345678</property> <property name="hbm2ddl.auto">update</property>
<mapping resource="com/oracle/bean/Salary.hbm.xml"/> <!-- DB schema will be updated if needed --> </session-factory> </hibernate-configuration>
<property name="hbm2ddl.auto">update</property>没有表就创建表,没有字段就创建字段,只能添加不能修改和删除
create是创建一个表,
validate只能校验
2)配置Salary.hbm.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.oracle.bean.Salary" table="salary" schema="salary"> <id name="workcode" column="workcode"> <generator class="uuid"></generator>主键生成策略如uuid是String类型 </id> <property name="uname" column="uname"></property> <property name="dept" column="dept"></property> <property name="baseSalary" column="base_salary"></property> <property name="extract" column="extract"></property> <property name="leav" column="leav"></property> <property name="sick" column="sick"></property> <property name="pk" column="pk"></property> </class> </hibernate-mapping>
二,用代码生成表
public static void main(String[] args) { createby3610(); } //5.1.0版本 public void createby510() { Configuration config = new Configuration().configure(); StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .configure().build(); Metadata metadata = new MetadataSources(serviceRegistry) .buildMetadata(); SchemaExport schemaExport = new SchemaExport(); schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata); } //3.6.0版本 public static void createby3610() { // Configuration cfg=new Configuration(); // cfg.configure(); // SchemaExport export=new SchemaExport(cfg); // //script 表示是否打印出脚本 // //export 表是否执行生成数据表 // export.create(true,true); } }
三,集合映射
一个人有多个别名
@Data public class User { private String usid; private String usname; private List<String> otherNames; }
User.hbm.xml把文件映射到Hibernate.cfg.xml中
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.oracle.bean"> <class name="User" table="user" > <id name="usid" > <generator class="uuid"></generator> </id> <property name="usname" ></property> <list name="otherNames" table="othername" > <key column="nameid"></key><!--主表和子表之间关联的字段--> <list-index column="nameindex"></list-index><!--排序的字段--> <element column="othername" type="java.lang.String"></element><!--真正存放数据的字段--> </list> </class> </hibernate-mapping>
package com.oracle.core; import com.oracle.bean.User; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.ArrayList; import java.util.List; public class CreateListData { private Session session; private SessionFactory sessionFactory; @Before public void begin(){ Configuration cfg=new Configuration(); cfg.configure(); SessionFactory sessionFactory = cfg.buildSessionFactory(); session= sessionFactory.openSession(); session.beginTransaction(); } @Test public void main() { //添加别名 List<String> namesList=new ArrayList<String>(); namesList.add("333"); namesList.add("444"); namesList.add("555"); User user=new User(); user.setOtherNames(namesList); user.setUsname("aaa"); session.save(user); } @Test public void edit(){ //在原有的基础上再添加几个别名用update,先查询再添加 User user=session.get(User.class,"8a8aac94635ea6c801635ea6d3160000"); List<String> nameList=new ArrayList<>(); nameList.add("123"); nameList.add("12345"); nameList.add("123456"); user.setOtherNames(nameList); session.update(user); } @After public void end(){ session.getTransaction().commit(); session.close(); sessionFactory.close(); } }