spring整合hibernate实例(上)

这篇主要是展示spring和hibernate的整合的实例(上)——自己也是第一弄,有些内容还不是很规范,就是项目能跑通,有好的建议欢迎提出来~

1、数据库表:(简单的用来测试的表,age是主键int类型,name是varchar类型)

2、项目工程图:

接下来的代码和配置文件就按照上图的顺序给出

3、PersonService:

package service;

import java.sql.SQLException;

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

import spring.hibernate.dao.PersonDao;
import spring.hibernate.entity.Person;

@Component
public class PersonService {
    
    @Autowired
    private PersonDao dao;
    
    public PersonService(){
        System.out.println("create personService");
    }

    public Person getPersonById(int id){
        try {
            return dao.getPerson(id);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public PersonDao getDao() {
        return dao;
    }

    public void setDao(PersonDao dao) {
        this.dao = dao;
    }
    
    
}

4、App(main函数所在的类)

package spring.hibernate;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.PersonService;
import spring.hibernate.entity.Person;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        PersonService service = (PersonService) ctx.getBean("personService");
        Person p = service.getPersonById(16);
        System.out.println(p.getAge()+" "+p.getName());
        
        System.out.println("end");
    }
}

5、PersonDao

package spring.hibernate.dao;

import java.sql.SQLException;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import spring.hibernate.dao.PersonDao;
import spring.hibernate.entity.Person;

public class PersonDao{

    private HibernateTemplate hibernateTemplate;
    
    public PersonDao(){
        System.out.println("create PersonDao");
    }
    
    public void setSessionFactory(SessionFactory sessionFactory){
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }
    
    public void savePerson(Person person) throws SQLException {
        // TODO Auto-generated method stub
        hibernateTemplate.save(person);
        
    }

    public void delPerson(Person person) throws SQLException {
        // TODO Auto-generated method stub
        hibernateTemplate.delete(person);
    }

    public void editPerson(Person person) throws SQLException {
        // TODO Auto-generated method stub
        hibernateTemplate.update(person);
        
    }

    public Person getPerson(int id) throws SQLException {
        // TODO Auto-generated method stub
        Person person = (Person)hibernateTemplate.get(Person.class, id);
        return person;
    }

}

6、Person(实体类)

package spring.hibernate.entity;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "person")
public class Person {

    private String name;
    @Id
    private int age;
    
    public Person(){
        System.out.println("create person");
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
}

7、hibernate.properties(配置文件)

driver=com.mysql.jdbc.Driver
databaseurl=jdbc:mysql://localhost:3306/test
username=root
password=123456
原文地址:https://www.cnblogs.com/tiramisuyj/p/4774389.html