• 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();

     

  • 相关阅读:
    fastjson反序列化漏洞研究(上)
    csv注入复现代码
    day24-python之面向对象
    day23-python之日志 re模块
    day22-python之模块
    day21-python模块
    day20-python之装饰器
    day18-python之迭代器和生成器
    day17-python之文件操作
    day16-python之函数式编程匿名函数
  • 原文地址:https://www.cnblogs.com/kaiwen/p/6655316.html
Copyright © 2020-2023  润新知