Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Hibernate

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how to use Hibernate, the article is very detailed, has a certain reference value, interested friends must read it!

The source of hibernate

Data flows between different levels, and a series of interactive problems such as data type conversion will occur in the process of data transfer. The interactive solution between java and html is OGNL (object Navigation Graph language) provided by struts2, while the technical solution between java and database is ORM (object relational mapping).

What is ORM?

Object-relational mapping: Object relation mapping

1. Object: refers to the java object and the entity bean

two。 Relationship: relationship is just a database, sqlserver,mysql,oracle belongs to relational database, and sql language is used to operate relational database, but sql has obvious shortcomings. It can only execute one statement at a time, and there is no basic logical judgment.

3. Mapping: one-to-one correspondence between attributes of objects in java and table fields in the database (including one-to-one, one-to-many, many-to-one, many-to-many).

Conditions that ORM needs to meet

The relationship between classful attributes and table fields mapped one by one

Transform the operation of the relational model (database) into that of the object model (pojo)

Advantages and disadvantages of hibernate

Advantages: do not program to write their own sql, simplify the development, improve the development speed, hibernate entry threshold is low, get started quickly.

Disadvantages: the sql automatically generated by hibernate is a standard sql, which is difficult to optimize, loses flexibility, and requires higher requirements for programmers, so it is necessary to understand a certain Omax R mapping.

Quick access to hibernate case list (maven)

Import related jar' packages in pom.xml

Org.hibernate

Hibernate-core

3.6.5.Final

If it cannot be created properly, you need to import other packages.

Org.hibernate

Hibernate-core

3.6.8.Final

Junit

Junit

4.7

Test

Org.javassist

Javassist

3.13.0-GA

Backport-util-concurrent

Backport-util-concurrent

2.2

Commons-logging

Commons-logging

1.1.1

Net.sf.ehcache

Ehcache

1.2.3

Hibernate.cfg.xml profile

"- / / Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

Oracle.jdbc.driver.OracleDriver

Jdbc:oracle:thin:@localhost:1521:orcl

Scott

Tiger

None

Org.hibernate.dialect.Oracle10gDialect

Thread

True

3.*.xml mapping file

Write an entity bean first

Package com.it.bean

Public class Student {

Private String stu_id

Private String stu_name

Private String stu_sex

Private String stu_birth

Private String stu_addr

Public String getStu_id () {

Return stu_id

}

Public void setStu_id (String stu_id) {

This.stu_id = stu_id

}

Public String getStu_name () {

Return stu_name

}

Public void setStu_name (String stu_name) {

This.stu_name = stu_name

}

Public String getStu_sex () {

Return stu_sex

}

Public void setStu_sex (String stu_sex) {

This.stu_sex = stu_sex

}

Public String getStu_birth () {

Return stu_birth

}

Public void setStu_birth (String stu_birth) {

This.stu_birth = stu_birth

}

Public String getStu_addr () {

Return stu_addr

}

Public void setStu_addr (String stu_addr) {

This.stu_addr = stu_addr

}

Public Student (String stu_id, String stu_name, String stu_sex, String stu_birth, String stu_addr) {

Super ()

This.stu_id = stu_id

This.stu_name = stu_name

This.stu_sex = stu_sex

This.stu_birth = stu_birth

This.stu_addr = stu_addr

}

Public Student () {

Super ()

/ / TODO Auto-generated constructor stub

}

Public Student (String stu_id, String stu_name) {

Super ()

This.stu_id = stu_id

This.stu_name = stu_name

}

}

The corresponding mapping file

"- / / Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

Writing of dao layer

BaseDao

Package com.it.dao

Import java.util.List

Import org.hibernate.Query

Import org.hibernate.Session

Public class BaseDao {

Private Session session

Public BaseDao (Session session) {

This.session=session

}

Public void add (E e) {

Session.save (e)

}

Public void delete (E e) {

Session.delete (e)

}

Public void update (E e) {

Session.update (e)

}

Public List finbBySplit (int currentPage,int currentSize,String hql,String...params) {

Query query=session.createQuery (hql)

/ / assign values to the question mark placeholder

For (int I = 0; I < params.length; iTunes +) {

Query.setString (I, params [I])

}

/ / paging parameters

Int first= (currentPage-1) * currentSize

Query.setFirstResult (first)

Query.setMaxResults (currentSize)

/ / query

Return query.list ()

}

}

Writing of StudentDao layer

Package com.it.dao

Import java.util.List

Import org.hibernate.Session

Import com.it.bean.Student

Public class StudentDao extends BaseDao {

Public StudentDao (Session session) {

Super (session)

/ / TODO Auto-generated constructor stub

}

Public void add (Student stu) {

Super.add (stu)

}

Public void delete (Student stu) {

Super.delete (stu)

}

Public void update (Student stu) {

Super.update (stu)

}

Public List find (int currentPage,int currentSize,Student stu) {

String hql= "from Student stu where stu.stu_id like? and stu.stu_name like? and stu.stu_sex like? and stu.stu_birth like? and stu.stu_addr like?

String [] params= {"%" + stu.getStu_id () + "%", "%" + stu.getStu_name () + "%", "%" + stu.getStu_sex () + "%", "%" + stu.getStu_birth () + "%", "%" + stu.getStu_addr () + "%"}

Return super.finbBySplit (currentPage, currentSize, hql, params)

}

}

It's time to test.

@ Test

Public void add () {

SessionFactory sessionFactory=null

Session session=null

Transaction tx=null

Try {

/ / create a sessionfactory

SessionFactory=new Configuration (). Configure (). BuildSessionFactory ()

/ / create a session reply-get the reply from the thread itself

Session=sessionFactory.getCurrentSession ()

/ / start a transaction

Tx=session.beginTransaction ()

/ / add data

Student stu=new Student ("0000", "Shen Lang", "male", "87", "Beijing")

/ / session.save (stu)

/ / modify data

/ / stu.setStu_name ("Shen Wansan")

/ / session.update (stu)

/ / Delete data

/ / session.delete (stu)

/ / commit transaction

/ / query a piece of data according to id

/ / Student stu1= (Student) session.get (Student.class, "0000")

/ / System.out.println (stu1.getStu_name ())

/ / use of hql

/ / query all information

/ * String hql= "from Student where stu_id like'% 2%'"

Query query=session.createQuery (hql)

List list=query.list ()

For (Student student: list) {

System.out.println (student.getStu_name ())

} * / Wuxi × × Hospital https://yyk.familydoctor.com.cn/20612/

/ / return vo object

/ * String hql= "select new Student (stu_id,stu_name) from Student where stu_id like'% 2%'"

Query query=session.createQuery (hql)

List list=query.list ()

For (Student student: list) {

System.out.println (student.getStu_name ())

} * /

/ / return parameters of type map

String hql= "select new map (count (*) as ct,max (stu_id) as maxid) from Student"

Query query=session.createQuery (hql)

Map map= (Map) query.uniqueResult ()

System.out.println (map.get ("ct"); * /

/ / placeholder?

/ * String hql= "from Student where stu_id like?"

Query query=session.createQuery (hql)

Query.setString (0, "2%")

List list=query.list ()

For (Student student: list) {

System.out.println (student.getStu_name () + "=")

} * /

/ /: placeholder

/ * String hql= "from Student where stu_sex=:uid"

Map params=new HashMap ()

Params.put ("uid", "1")

Query query=session.createQuery (hql)

/ / assign values to parameters

For (String key:params.keySet ()) {

System.out.println (key+ "=")

Query.setString (key,params.get (key))

}

List list=query.list ()

For (Student student: list) {

System.out.println (student.getStu_name () + "+")

} * /

/ / return parameters of type number

/ * String hql= "select count (*) from Student"

Query query=session.createQuery (hql)

Number ct= (Number) query.uniqueResult ()

System.out.println (ct.intValue ()); * /

/ / Fuzzy search element

/ * Student stu1=new Student ("1", "Wu")

String [] params= {"%" + stu1.getStu_id () + "%", "%" + stu1.getStu_name () + "%"}

String hql= "from Student where stu_id like? and stu_name like?"

/ / String hql= "from Student where stu_id like'% 1% 'and stu_name like'% Wu%'"

Query query=session.createQuery (hql)

/ / for? Sign assignment

For (int I = 0; I < params.length; iTunes +) {

Query.setString (iBI params [I])

System.out.println (params [I])

}

System.out.println (hql)

System.out.println (query.list (). Size ()); * /

/ / hibernate paging

String hql= "from Student where stu_id like'% 2%'"

Query query=session.createQuery (hql)

Int currentPage=1

Int pageSize=4

Int startPage= (currentPage-1) * pageSize

Query.setFirstResult (startPage); / / the location where the display starts

Query.setMaxResults (pageSize); / / displays the number of big data

System.out.println (query.list () .size ())

Tx.commit ()

} catch (Exception e) {

/ / TODO: handle exception

E.printStackTrace ()

/ / transaction rollback

Tx.rollback ()

}

}

Now a simple demo change has been created successfully.

The above is all the content of this article "how to use Hibernate". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report