SpringDataJpa+Hibernate+Spring搭建环境

先贴上pom.xml



    4.0.0

    Spring+SpringData+mybatis+Maven
    30
    1.0-SNAPSHOT
    
        
        
            com.alibaba
            druid
            1.0.29
        
        
            mysql
            mysql-connector-java
            8.0.13
        
        
        
            org.aspectj
            com.springsource.org.aspectj.weaver
            1.6.8.RELEASE
        
        
        
            org.springframework.data
            spring-data-commons
            2.1.3.RELEASE
        
        
            org.springframework.data
            spring-data-jpa
            2.1.3.RELEASE
        
        
            org.hibernate
            hibernate-core
            5.3.7.Final
        
        
        
            org.springframework
            spring-core
            5.1.3.RELEASE
        
        
            org.springframework
            spring-beans
            5.1.3.RELEASE
        
        
            org.springframework
            spring-context
            5.1.3.RELEASE
        
        
            org.springframework
            spring-jdbc
            5.1.3.RELEASE
        
        
            org.springframework
            spring-web
            5.1.3.RELEASE
        
        
            org.springframework
            spring-webmvc
            5.1.3.RELEASE
        
        
            org.springframework
            spring-test
            5.1.3.RELEASE
        
        
        
            junit
            junit
            4.12
        
        
            org.apache.logging.log4j
            log4j-core
            2.6.2
        
        
            org.apache.logging.log4j
            log4j-api
            2.6.2
        
             
            org.apache.logging.log4j
            log4j-slf4j-impl
            2.6.2
        
        
            org.slf4j 
            log4j-over-slf4j
            1.7.12
        
        
        
            com.lmax
            disruptor
            3.3.6
        
        
            org.slf4j
            slf4j-simple
            1.7.25
            test
        
        
            commons-logging
            commons-logging
            1.2
        
    
    
        
            
            
                src/main/java
                
                    **/*.xml
                
            
            
            
                src/main/resources
            
        
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    8
                    8
                
            
        
    

Spring.XML



    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    
    
        
        
        
        
        
        
            
        
        
        
            
                org.hibernate.dialect.MySQL5Dialect
                true
                true
                update
            
        
    
    
    
        
    
    
        
            
            
            
        
    
    
        
        
    
    

实体类

package pojo;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Objects;

@Entity
public class Student {
    private int id;
    private String name;
    private Integer age;
    private String password;

    @Id
    @Column(name = "id", nullable = false)
    public int getId() {
        return id;
    }

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

    @Basic
    @Column(name = "name", nullable = true, length = 20)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Basic
    @Column(name = "age", nullable = true)
    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Basic
    @Column(name = "password", nullable = true, length = 20)
    public String getPassword() {
        return password;
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return id == student.id &&
                Objects.equals(name, student.name) &&
                Objects.equals(age, student.age) &&
                Objects.equals(password, student.password);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, age, password);
    }
}

DAO接口要使用注解或者继承
domainClass是你的实体类。就是指你这个接口会用到那个实体类你就使用那个
然后Idclass就是你的id类型。如果你数据库的id是int那就直接Integer

package dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.repository.RepositoryDefinition;
import pojo.Student;

import java.util.List;
@RepositoryDefinition(domainClass =Student.class,idClass = Integer.class)
public interface Studao {
    List findByNameIs(String name);
    Student findByIdIs(int id);
}

测试类

import dao.Studao;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import pojo.Student;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:applicationContext.xml")
public class Test {
    @Autowired
    private Studao studao;
    @org.junit.Test
    public void testDataSouce() {
        Student aa = studao.findByIdIs(1);
        System.out.println(aa);
    }
}

测试结果

Hibernate: 
    
    create table Student (
       id integer not null,
        age integer,
        name varchar(20),
        password varchar(20),
        primary key (id)
    ) engine=MyISAM
Hibernate: 
    select
        student0_.id as id1_0_,
        student0_.age as age2_0_,
        student0_.name as name3_0_,
        student0_.password as password4_0_ 
    from
        Student student0_ 
    where
        student0_.id=?
pojo.Student@3c1d56

Process finished with exit code 0

你可能感兴趣的:(SpringDataJpa+Hibernate+Spring搭建环境)