SpringData,Spring与Hibernate整合小记

学习过程中国内对整合的代码文章少之又少,无奈每日对着英文博客和英文API翻译,在Spring+Hibernate+SpringData整合中其中一个异常曾经解决4天,希望和我一样的小鲜肉不要再碰头~文章中有不足的地方希望大家多多指正~谢谢~

请大家忽略package name

首先第一步需要准备相对应的jar包,jar包很关键,其中不少Exception都是源自jar缺少或者冲突,需要jar包的可以留言邮箱

第二部构建Domain实体

package priv.wxz.dao;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

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

/**
 * Created by wangxizhong on 16/11/1.
 */

@Entity(name = "user")
@DynamicInsert
@DynamicUpdate
public class User {
    @Id
    @Column
    private int id;
    @Column
    private String name;
    @Column
    private String password;
    @Column
    private String phone;
    @Column
    private String address;

    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;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

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

    }

    public User() {
    }
}


第三步构建BaseDao,所有的Dao接口都需要实现这个此接口,此接口又继承了Repository相关接口以实现相对应的CRUD以及分页排序方法

package priv.wxz.data.domain;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.io.Serializable;
/**
 * Created by wangxizhong on 2016/11/4.
 * This is a framework of the underlying query method
 *Including the crud
 *This method must be inherited to achieve the following functions
 *Must be passed to ID, object
 *Database interaction Middleware
 */
@NoRepositoryBean
public interface BaseDao extends PagingAndSortingRepository {
    T save(T entity);

    Iterable save(Iterable iterableEntity);

    T findOne(ID primaryKey);

    Iterable findAll();

    long count();

    boolean exists(ID primaryKey);

    void delete(T entity);

    void delete(ID primaryKey);

    void delete(Iterable iterableEntity);

   // void deleteAll();

    Iterable findAll(Sort sortInfo);

    Page findAll(Pageable pageInfo);


}

第四步创建相对应的Dao接口以实现数据库操作

如果要使用update delete insert 需要添加事务注解  如果是复杂的例如多表连接需要使用@Query注解来实现自定义查询,并且在方法命名上尽量使用类似于findAll,并且有区别于spring规定命名

package priv.wxz.test;

import priv.wxz.dao.User;
import priv.wxz.data.domain.BaseDao;

/**
 * Created by wangxizhong on 2016/11/7.
 */
public interface UserDao extends BaseDao {

    User findById(int id);
}

第五步  测试类

package priv.wxz.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import priv.wxz.dao.User;

/**
 * Created by wangxizhong on 2016/11/9.
 */
public class TestMain {
    private ApplicationContext context=null;
    private ServiceTest serviceTest=null;

    @Before
    public  void before(){
        context=new ClassPathXmlApplicationContext("applicationContext.xml");
        serviceTest=(ServiceTest)context.getBean(ServiceTest.class);
    }
    @Test
    public void testUser(){
        User user=serviceTest.showUser();
        System.out.println(user.getName()+"..."+user.getAddress()+"..."+user.getPhone());
    }
}

贴一下application配置文件



    
    

    
        
        
        
        
    

    
        
        
            
        
        
        
            
                org.hibernate.dialect.MySQL5Dialect
                20
                true
                false
                false
                true
                UTF-8
                ehcache.xml
                org.hibernate.cache.ehcache.EhCacheRegionFactory
                
                false
                your db username
                your db password
                jdbc:mysql://localhost:3306/db name?characterEncoding=utf-8"
            
        
    

    
        
        
    

    

    
    



数据库配置文件db.properties
jdbc.username=yourdbname
jdbc.password=yourpassword
jdbc.url=jdbc:mysql://localhost:3306/dev
jdbc.driverClassName=com.mysql.jdbc.Driver


hibernate 缓存文件ehcache.xml




    
    


    
    

    

    
    

    
     -->

    





你可能感兴趣的:(Java随记)