趣百科

Spring如何整合Hibernate

编辑:Simone 2024-11-24 03:36:02 533 阅读

Spring如何整合Hibernate

hibernate是一个全自动的ORM框架,很好的实现了JPA规范,是web应用中数据库操作的一大技术栈,如何掌握基于hibernate的数据库操作?这里我们从Spring整合Hibernate开始。

准备数据库

#创建数据库

CREATE SCHEMA `springdata`

CHARSET utf8

COLLATE utf8_general_ci;

#使用数据库

USE `springdata`;

创建项目

此处创建Maven项目来进行Spring整合Hibernate的学习

添加jdbc.properties、applicationContext.xml配置文件

导入Spring整合Hibernate所需的依赖

配置jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3308/springdata?useSSL=true&\

useUnicode=true&characterEncoding=UTF-8

jdbc.user=root

jdbc.password=你的数据库用户密码

编写Spring整合hibernate的配置文件application.xml

创建实体类User

package site.hclz.springdata.pojo;

import javax.persistence.*;

import java.io.Serializable;

@Entity

@Table(name = "t_user")

public class User implements Serializable {

@Id@Column(name = "u_id")

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Integer userId;

@Basic@Column(name = "u_name")

private String userName;

@Basic@Column(name = "u_age")

private Integer userAge;

@Basic@Column(name = "u_address")

private String userAddress;

public User(){}

public User(Integer userId, String userName, Integer userAge, String userAddress) {

this.userId = userId;

this.userName = userName;

this.userAge = userAge;

this.userAddress = userAddress;

}

//省略getter/setter方法 toString方法

}

UserDao接口

package site.hclz.springdata.dao;

import site.hclz.springdata.pojo.User;

import java.util.List;

public interface UserDao {

//增加

Integer insertUser(User user);

//删除

void deleteUser(Integer userId);

//修改

void updateUser(User user);

//查询

User selectUserById(Integer userId);

//HQL带条件查询

List selectUserListByAddressUseHQL(String address);

//SQL带条件查询

List selectUserListByAddressUseSQL(String address);

//QBC带条件查询(hibernate5.2中已过时,推荐使用jpaCriteria)

List selectUserListByAddressUseQBC(String address);

}

接口实现类UserDaoImpl

package site.hclz.springdata.dao.imp;

import org.hibernate.Criteria;

import org.hibernate.Session;

import org.hibernate.criterion.Restrictions;

import org.hibernate.query.NativeQuery;

import org.hibernate.query.Query;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.orm.hibernate5.HibernateTemplate;

import org.springframework.stereotype.Repository;

import site.hclz.springdata.dao.UserDao;

import site.hclz.springdata.pojo.User;

import java.util.List;

@Repository("userDao")

public class UserDaoImpl implements UserDao {

@Autowired

private HibernateTemplate hibernateTemplate;

@Override

public Integer insertUser(User user) {

return (Integer) hibernateTemplate.save(user);

}

@Override

public void deleteUser(Integer userId) {

hibernateTemplate.delete(hibernateTemplate.get(User.class,userId));

}

@Override

public void updateUser(User user) {

hibernateTemplate.update(user);

}

@Override

public User selectUserById(Integer userId) {

return hibernateTemplate.get(User.class,userId);

}

@Override

public List selectUserListByAddressUseHQL(String address) {

Session session = hibernateTemplate.getSessionFactory().getCurrentSession();

Query query = session.createQuery(

"from User where userAddress=:addr",

User.class).setParameter("addr", address);

return query.list();

}

@Override

@SuppressWarnings("unchecked")

public List selectUserListByAddressUseSQL(String address) {

Session session = hibernateTemplate.getSessionFactory().getCurrentSession();

NativeQuery nativeQuery = session.createSQLQuery(

"SELECT * FROM springdata.t_user WHERE u_address=:addr")

.addEntity(User.class)

.setParameter("addr",address);

return nativeQuery.list();

}

@Override

@SuppressWarnings("unchecked")

public List selectUserListByAddressUseQBC(String address) {

Session session = hibernateTemplate.getSessionFactory().getCurrentSession();

//@deprecated (since 5.2) for Session, use the JPA Criteria

Criteria criteria = session.createCriteria(User.class);

criteria.add(Restrictions.eq("userAddress",address));

return criteria.list();

}

}

编写测试类测试Spring整合Hibernate

版权声明:本站【趣百科】文章素材来源于网络或者用户投稿,未经许可不得用于商用,如转载保留本文链接:https://www.qubaik.com/life/78818.html

相关推荐