图1:Hibernate 目录结构图
Hibernate 文件夹中,包含以下 3 个目录:
图2:Hibernate 必须 jar 包
图3:新建 java 项目
图4:IDEA Hibernate 依赖包
图5:Project Structure
图6:工程结构依赖视图
图7:Attach Files or Directories
图8:保存配置
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int NOT NULL AUTO_INCREMENT, `user_id` varchar(255) DEFAULT NULL, `user_name` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, `email` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; INSERT INTO `user` VALUES ('1', '001', 'admin', 'admin', '12345678@qq.com'); INSERT INTO `user` VALUES ('2', '002', 'user', '123456', '98765432@qq.com');
package net.biancheng.www.po; /** * 实体类 * 与 bianchegnbang_jdbc 数据库中的 user 表对应 */ public class User { private int id; private String userId; private String userName; private String password; private String email; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "net.biancheng.www.po.User{" + "id=" + id + ", userId='" + userId + '\'' + ", userName='" + userName + '\'' + ", password='" + password + '\'' + ", email='" + email + '\'' + '}'; } }
[实体类名].hbm.xml例如,实体类 User 的映射文件就可以命名为 User.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> <!-- name:类的全路径:--> <!-- table:表的名称:(可以省略的.使用类的名称作为表名.)--> <class name="net.biancheng.www.po.User" table="user" schema="bianchengbang_jdbc"> <!-- 主键--> <id name="id" column="id"> <!--主键生成策略--> <generator class="native"></generator> </id> <!--type:三种写法--> <!--Java类型 :java.lang.String--> <!--Hibernate类型:string--> <!--SQL类型 :不能直接使用type属性,需要子标签<column>--> <!--<column name="name" sql-type="varchar(20)"/>--> <property name="userId" column="user_id" type="java.lang.String"/> <property name="userName" column="user_name"/> <property name="password" column="password"/> <property name="email" column="email"/> </class> </hibernate-mapping>
<?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> <!--使用 Hibernate 自带的连接池配置--> <property name="connection.url">jdbc:mysql://localhost:3306/bianchengbang_jdbc</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!--hibernate 方言--> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!--打印sql语句--> <property name="hibernate.show_sql">true</property> <!--格式化sql--> <property name="hibernate.format_sql">true</property> <!-- 加载映射文件 --> <mapping resource="net/biancheng/www/mapping/User.hbm.xml"/> </session-factory> </hibernate-configuration>
package net.biancheng.www.test; import net.biancheng.www.po.User; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.query.Query; import org.junit.Test; import java.util.List; public class MyTest { /** * 查询数据库数据 */ @Test public void testQuery() { //Hibernate 加载核心配置文件(有数据库连接信息) Configuration configuration = new Configuration().configure(); //创建一个 SessionFactory 用来获取 Session 连接对象 SessionFactory sessionFactory = configuration.buildSessionFactory(); //获取session 连接对象 Session session = sessionFactory.openSession(); //开始事务 Transaction transaction = session.beginTransaction(); //根据主键查询 user 表中的记录 User user = session.get(User.class, 1); System.out.println(user); //提交事务 transaction.commit(); //释放资源 session.close(); sessionFactory.close(); } }
七月 19, 2021 12:24:28 下午 org.hibernate.Version logVersion INFO: HHH000412: Hibernate ORM core version 5.5.3.Final 七月 19, 2021 12:24:29 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {5.1.2.Final} 七月 19, 2021 12:24:30 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) 七月 19, 2021 12:24:30 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/bianchengbang_jdbc] 七月 19, 2021 12:24:30 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {user=root, password=****} 七月 19, 2021 12:24:30 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false 七月 19, 2021 12:24:30 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init> INFO: HHH000115: Hibernate connection pool size: 20 (min=1) Mon Jul 19 12:24:30 CST 2021 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. 七月 19, 2021 12:24:31 下午 org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect 七月 19, 2021 12:24:32 下午 org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform] Hibernate: select user0_.id as id1_0_0_, user0_.user_id as user_id2_0_0_, user0_.user_name as user_nam3_0_0_, user0_.password as password4_0_0_, user0_.email as email5_0_0_ from user user0_ where user0_.id=? net.biancheng.www.po.User{id=1, userId='001', userName='admin', password='admin', email='12345678@qq.com'} 七月 19, 2021 12:24:32 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PoolState stop INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/bianchengbang_jdbc]
本文链接:http://task.lmcjl.com/news/14457.html