造数据

一、 工具

  利用Hibernate 生成数据,现有一条数据,进修改部分字段,进行复制。

二、 代码

  hibernate.cfg.xml  在resources 目录下

<!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 >
         <!-- Mysql  -->
           <!-- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property> -->
        <property name="dialect">org.hibernate.dialect.OracleDialect</property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <property name="connection.username">puriew</property>
        <property name="connection.password">puriew</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <mapping class="com.cyd.helloworld.gateways.Gateways"/>
    </session-factory>
</hibernate-configuration>

pojo 实现clone 接口

@Table(name = "wd_iot_eqm_gateways")
@Entity
public class Gateways implements Cloneable {
    @Id
    private String id;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Gateways stu = null;
        try {
            stu = (Gateways) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return stu;
    }
}

Main

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class UpdataGateway {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        Transaction tx = null;
        Session session = null;
        SessionFactory sf = null;
        ServiceRegistry serviceRegistry = null;
        try {

            Configuration cfg = new Configuration().configure();
            serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
            sf = cfg.buildSessionFactory(serviceRegistry);
            session = sf.openSession();
            Gateways g=(Gateways) session.get(Gateways.class, "9B1CDEAA3BCD4B79B805A52E208B8572");
            tx = session.beginTransaction();
            Gateways way=null;
            for (int i = 2000; i < 3000; i++) {
                way=(Gateways) g.clone();
                way.setId("120020170626"+i);
                way.setCODE("120020170626"+i);
                session.save(way);
            }
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
            if (sf != null) {
                sf.close();
            }
        }

    }
}

 

posted @ 2017-11-29 19:34  litblack  阅读(408)  评论(0编辑  收藏  举报