• Eclipse+Maven+Hibernate5


    依赖:

      Mysql  8.0.11

      Hibernate 5.2.6 Final

    1、pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.wyl</groupId>
      <artifactId>Hibernate5Demo</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>Hibernate5Demo</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.6.Final</version>
        </dependency>
      
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        
      </dependencies>
    </project>
    

      2、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">
    <!-- Version 8 MySQL hiberante-cfg.xml example for Hibernate 5 -->
    <hibernate-configuration>
      <session-factory>
        <property name ="connection.driver_class"> com.mysql.cj.jdbc.Driver </property>
        <!-- property name="connection.driver_class">com.mysql.jdbc.Driver</property -->
        <property name ="connection.url">jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true</property>
        <property name ="dialect"> org.hibernate.dialect.MySQLDialect </property>
        <property name ="connection.username"> root </property>
        <property name ="connection.password"> password </property>
        <property name ="connection.pool_size">3</property>
        
        <!--property name="dialect">org.hibernate.dialect.MySQLDialect</property-->
        <property name ="current_session_context_class"> thread </property> 
        <property name ="show_sql"> true </property>
        <property name ="format_sql"> true </property>
        <property name ="hbm2ddl.auto"> update </property>
        
        <mapping resource="door.hbm.xml" />
      </session-factory>
    </hibernate-configuration>
    

      3、door.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 package="com.wyl">
        <class name="com.wyl.entity.Door" table="tb_door">
            <id name="id">
                <column name="id"></column>
                <generator class="native"></generator>
            </id>
            
            <property name="name">
                 <column name="name"></column>
            </property>
            
            <property name="tel" type="java.lang.String">
                <column name="tel"></column>
            </property>
            
            <property name="addr" type="java.lang.String">
                <column name="addr"></column>
            </property>
        </class>
    
    </hibernate-mapping>
    

      4、在com.wyl.entity包下新建Door.java

    package com.wyl.entity;
    
    public class Door {
        private Integer id;
        private String name;
        private String tel;
        private String addr;
    	public Integer getId() {
    		return id;
    	}
    	public void setId(Integer id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getTel() {
    		return tel;
    	}
    	public void setTel(String tel) {
    		this.tel = tel;
    	}
    	public String getAddr() {
    		return addr;
    	}
    	public void setAddr(String addr) {
    		this.addr = addr;
    	}
    	@Override
    	public String toString() {
    		return "Door [id=" + id + ", name=" + name + ", tel=" + tel + ", addr=" + addr + "]";
    	}
    	
        
    }
    

      5、测试App.java

    package com.wyl;
    
    import java.util.List;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.query.Query;
    
    import com.wyl.entity.Door;
    
    public class App {
    	public static void main(String[] args) {
    		Configuration config = new Configuration().configure();
    		SessionFactory sessionFactory = config.buildSessionFactory();
    		Session session = sessionFactory.openSession();
    		
    		Query<Door> query = session.createQuery("from Door");
    		List<Door> list = query.getResultList();
    		
    		System.out.println(list);
    		
    		session.close();
    		sessionFactory.close();
    		
    	}
    }
    

      6、结果

  • 相关阅读:
    白话经典算法系列之六 高速排序 高速搞定
    簡單SQL存儲過程實例
    ACE编译运行错误解决
    shell之变量与read
    Java实现第八届蓝桥杯魔方状态
    Java实现第八届蓝桥杯魔方状态
    Java实现第八届蓝桥杯魔方状态
    Java实现第八届蓝桥杯魔方状态
    Java实现第八届蓝桥杯购物单
    Java实现第八届蓝桥杯购物单
  • 原文地址:https://www.cnblogs.com/wylwyl/p/13298264.html
Copyright © 2020-2023  润新知