03 spring整合hibernate

本文将演示spring整合hibernate。

1、软件约束

  • mysql5.7
  • hibernate 5
  • idea 2018.1

2、前提约束

  • 创建一个maven项目
  • 加入以下依赖

        
            org.hibernate
            hibernate-core
            4.3.11.Final
        
        
            mysql
            mysql-connector-java
            5.1.47
        

        
            commons-dbcp
            commons-dbcp
            1.4
        

        
        
            org.springframework
            spring-context
            4.3.18.RELEASE
        
        
            org.springframework
            spring-beans
            4.3.18.RELEASE
        
        
            org.springframework
            spring-aspects
            4.3.18.RELEASE
        
        
            org.springframework
            spring-context-support
            4.3.18.RELEASE
        
        
            org.springframework
            spring-aop
            4.3.18.RELEASE
        
        
            org.springframework
            spring-orm
            4.3.18.RELEASE
        
  • 创建src/main/resources/hibernate.cfg.xml配置文件:



    
        org.hibernate.dialect.MySQL5Dialect
        true
        update
    

  • 创建src/main/resources/user.hbm.xml映射文件:



    
        
            
            
        
        
            
        
    

  • 创建src/main/resources/applicationContext-dao.xml配置文件:


    
    
        
        
        
        
    
    
        
        
        
    

    
        
    

    
    
        
            
            
        
    
    
    
        
        
    



  • 创建net.wanho.entity.User.java实体类:
package net.wanho.entity;

import java.io.Serializable;

public class User implements Serializable {
    private int id;
    private String name;

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public User() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
  • 创建net.wanho.dao.UserDao.java接口:
package net.wanho.dao;

import net.wanho.entity.User;

import java.util.List;

public interface UserDao {
    User findUserById(int id);

    List listUsers();

    void saveUser(User user);
}
  • 创建net.wanho.dao.impl.UserDaoImpl.java实现类:
package net.wanho.dao.impl;

import net.wanho.dao.UserDao;
import net.wanho.entity.User;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository(value = "userDao")
public class UserDaoImpl implements UserDao {
    @Resource
    private SessionFactory sessionFactory;

    private Session getSession()
    {
        return sessionFactory.getCurrentSession();
    }

    public User findUserById(int id)
    {
        String hql=" from User where id=?";
        Query query=getSession().createQuery(hql).setInteger(0, id);
        List list = query.list();
        return list.get(0);
    }

    public List listUsers() {
        String hql="select * from t_user";
        Query query=getSession().createSQLQuery(hql).addEntity(User.class);
        List list = query.list();
        return list;
    }

    public void saveUser(User user) {
        getSession().save(user);
    }
}
  • 创建net.wanho.service.UserService.java
package net.wanho.service;

import net.wanho.dao.UserDao;
import net.wanho.entity.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {

    @Resource
    UserDao userDao;

    public User getUserById(Integer id)
    {
        User user = userDao.findUserById(id);
        return user;
    }

    public List listUsers()
    {
        return userDao.listUsers();
    }

    public void addUser(User user)
    {
        userDao.saveUser(user);
    }
}
  • 创建Test.java测试类:
import net.wanho.entity.User;
import net.wanho.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-dao.xml");
        UserService userService = applicationContext.getBean("userService", UserService.class);

        //加入user
        User user = new User();
        user.setName("ali");
        userService.addUser(user);

        //按id查询user
        //User user = userService.getUserById(1);
        //列出所有user
        //List user = userService.listUsers();
        System.out.println(user);

    }
}

以上就是spring整合hibernate的过程。

你可能感兴趣的:(03 spring整合hibernate)