Hibernate基础:快速入门(7):查询数据
2016-12-12 21:34
579 查看
数据库的操作增删改查,或者CRUD是最常见的操作。这篇文章中介绍查询数据的方法
使用xml文件自然也可以,由于注解可能是今后的标准方向,相关的例子都回使用注解方式。
hibernate.cfg.xml
创建如下所示的hibernate的设定文件<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hbtestdb?useSSL=false</property> <property name="hibernate.connection.username">hbtestuser01</property> <property name="hibernate.connection.password">hbtestuser01</property> <property name="show_sql">true</property> <property name="current_session_context_class">thread</property> </session-factory> </hibernate-configuration>
Entity Class:User
我们将会创建一个Entity的User类。Entity类就是用来与数据库的Table进行Mapping的Class。import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Column; @Entity @Table (name="hbtableuser") public class User { @Id @Column(name="userid") private int userid; @Column(name="username") private String username; @Column(name="country") private String country; public User(){ System.out.println("Default User construction is called..."); } public User(int userid, String username, String country) { this.userid = userid; this.username = username; this.country = country; } public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } }
类型 | 注解 | import | 作用 |
---|---|---|---|
类注解 | @Entity | javax.persistence.Entity | 类注解用 |
类注解 | @Table | javax.persistence.Table | 类注解用:@Table (name=”Table名”) |
属性注解 | @Id | javax.persistence.Id | 主码注解 |
属性注解 | @Column | javax.persistence.Column | 所有字段都需要 |
创建Demo演示代码
import org.hibernate.Session; import org.hibernate.SessionFactory; public class HbDemo05 { public static void main(String[] args){ try{ SessionFactory sessionFactory = new org.hibernate.cfg.Configuration() .configure("hibernate.cfg.xml") .addAnnotatedClass(User.class) .buildSessionFactory(); Session session = sessionFactory.getCurrentSession(); System.out.println("Successfully got connection from session factory."); session.beginTransaction(); User result = session.get(User.class,1001); session.getTransaction().commit(); System.out.println("Username :" + result.getUsername()); System.out.println("Country: " + result.getCountry()); sessionFactory.close(); }catch (Exception e){ System.out.println("Exception happened..."); e.printStackTrace(); } } }
执行结果
十二月 12, 2016 9:32:54 下午 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.2.5.Final} 十二月 12, 2016 9:32:54 下午 org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found 十二月 12, 2016 9:32:55 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 十二月 12, 2016 9:32:55 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) 十二月 12, 2016 9:32:55 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hbtestdb?useSSL=false] 十二月 12, 2016 9:32:55 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {user=hbtestuser01, password=****} 十二月 12, 2016 9:32:55 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false 十二月 12, 2016 9:32:55 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init> INFO: HHH000115: Hibernate connection pool size: 20 (min=1) 十二月 12, 2016 9:32:55 下午 org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect Successfully got connection from session factory. Hibernate: select user0_.userid as userid1_0_0_, user0_.country as country2_0_0_, user0_.username as username3_0_0_ from hbtableuser user0_ where user0_.userid=? Default User construction is called... 十二月 12, 2016 9:32:56 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/hbtestdb?useSSL=false] Username :Michael Country: China
相关文章推荐
- Hibernate基础:快速入门(5):删除数据
- Hibernate基础:快速入门(4):增加数据
- Hibernate基础:快速入门(6):修改数据
- MS SQL入门基础:数据查询--SELECT语句
- Hibernate的查询语言之HQL(一)——快速入门
- hibernate入门--实现简单的数据查询
- Hibernate基础:快速入门(2):JDBC方式访问数据库
- Hibernate入门之基本数据查询
- R语言-数据分析-快速入门方便查询-excel表格形式学习笔记1
- hibernate分页查询,在自己写的s2sh注册登录的基础上添加对数据表的分页显示功能
- hibernate的基础学习--多表关联数据查询
- Hibernate入门05 - 基本数据查询
- [置顶] python爬虫实践——零基础快速入门(五)将爬取的数据存到本地
- MS SQL入门基础:数据查询--SELECT语句
- NHibernate 快速入门(四)使用 HQL 查询数据
- Java软件开发基础知识梳理之(7)------Hibernate查询数据
- Hibernate快速入门基础部分(一)
- Hibernate入门05 - 基本数据查询
- Hibernate基础:快速入门(3):SessionFactory和Session
- Netsharp快速入门(之8) 基础档案(工作区2 设置商品主列表、规格细列表、商品表单、查询)