• hibernate基础简单入门1---helloword


    1:目录结果

    2:实体类(student.java)

    package com.www.entity;
    public class Student {
    private int id;
    private String    name;
    private int age;
    
    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 int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    }

    3:实体类映射表(Student.hbm.xml)

    <!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.www.entity.Student" table="stu_tab2">
            <id name="id" column="stu_id">
                <generator class="native"></generator>
            </id>
            <property name="name" column="stu_name"></property>
            <property name="age" column="stu_age"></property>
        </class>
    </hibernate-mapping>

    4:hibernate配置文件(hibernate.cfg.xml)

    <!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="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"></property>
    <mapping resource="com/www/entity/Student.hbm.xml"/>
    </session-factory>
    </hibernate-configuration>

    5:日志文件(log4j.properties)

    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    ### direct messages to file hibernate.log ###
    #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    ### set log levels - for more verbose logging change 'info' to 'debug' ###
    log4j.rootLogger=warn, stdout
    #log4j.logger.org.hibernate=info
    log4j.logger.org.hibernate=debug
    log4j.logger.org.hibernate.type=info
    ### log schema export/update ###
    log4j.logger.org.hibernate.tool.hbm2ddl=debug

    6:单元测试文件(StudentTest.java)

    package com.www.test;
    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;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    import org.junit.Test;
    import com.www.entity.Student;
    public class StudentTest {
        // 1.自动创建数据库表,不用创建表,它能自动帮你创建表。
        @Test
        public void createTable() {
            Configuration configuration = new Configuration().configure();
            SchemaExport se = new SchemaExport(configuration);
            se.create(true, true);
    
        }
        //添加
        @Test
        public void add() {
            Configuration cfg = new Configuration().configure();
            StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());
            ServiceRegistry service = ssrb.build();
            SessionFactory factory = cfg.buildSessionFactory(service);
    
            Session session = factory.openSession();
            Transaction tx = session.beginTransaction();
            Student stu = new Student();
            stu.setAge(20);
            stu.setName("wowo");
            try {
                session.save(stu);
                tx.commit();
            } catch (Exception e) {
                // TODO: handle exception
                tx.rollback();
            } finally {
                session.close();
            }
        }
        //查看
    @Test
        public void get() {
            Configuration cfg = new Configuration().configure();
            StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());
            ServiceRegistry service = ssrb.build();
            SessionFactory factory = cfg.buildSessionFactory(service);
    
            Session session = factory.openSession();
            Transaction tx = session.beginTransaction();
            try {
                Student stu = (Student) session.get(Student.class, 1);
                System.out.println(stu.getId() + stu.getName() + stu.getAge());
                tx.commit();
            } catch (Exception e) {
                // TODO: handle exception
                tx.rollback();
            } finally {
                session.close();
            }
        }
    
    }

    7:另一种hibernate连接数据库的配置文件(hibernate.properties)

    目录结构:

    hibernate.dialect=org.hibernate.dialect.MySQLDialect
    hibernate.connection.driver_class=com.mysql.jdbc.Driver
    hibernate.connection.url=jdbc:mysql://localhost:3306/hibernate
    hibernate.connection.username=root
    hibernate.connection.password=
    hibernate.show_sql=true

    总结:hibernate创建基本都是这六步,下面的记死

    Configuration cfg = new Configuration().configure();
            StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());
            ServiceRegistry service = ssrb.build();
            SessionFactory factory = cfg.buildSessionFactory(service);
    
            Session session = factory.openSession();
            Transaction tx = session.beginTransaction();

     

  • 相关阅读:
    HDFS被设计成能够在一个大集群中跨机器可靠地存储超大文件
    NameNode实现了三个接口
    MongoDB Connector for Hadoop
    Java Virtual Machine (JVM) objects 虚拟机实例的产生 退出 两种线程
    Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data.
    配置hadoop用户SSH无密码登陆 的2种方式 落脚点是 可以ssh免密进入的主机名写入动作发出主机的 known_hosts,而被无密进入主机的authorized_keys文件 免密登录
    Java Virtual Machine Process Status Tool
    Problem binding to [bigdata-server-01:9000] java.net.BindException: Cannot assign requested address;
    a completely rewritten architecture of Hadoop cluster
    curl is a tool to transfer data from or to a server curl POST
  • 原文地址:https://www.cnblogs.com/kaiwen/p/6655316.html
Copyright © 2020-2023  润新知