搭建第一个SpringDataJPA工程

第一步:创建maven工程,导入maven坐标

使用Spring Data JPA,需要整合Spring与Spring Data JPA,并且需要提供JPA的服务提供者hibernate,所以需要导入spring相关坐标,hibernate坐标,数据库驱动坐标等


        4.2.4.RELEASE
        5.0.7.Final
        1.6.6
        1.2.12
        0.9.1.2
        5.1.6
    

    
        
        
            junit
            junit
            4.9
            test
        
        
        
        
            org.aspectj
            aspectjweaver
            1.6.8
        

        
            org.springframework
            spring-aop
            ${spring.version}
        

        
            org.springframework
            spring-context
            ${spring.version}
        

        
            org.springframework
            spring-context-support
            ${spring.version}
        

        
            org.springframework
            spring-orm
            ${spring.version}
        

        
            org.springframework
            spring-beans
            ${spring.version}
        

        
            org.springframework
            spring-core
            ${spring.version}
        
        
        

        
        
            org.hibernate
            hibernate-core
            ${hibernate.version}
        
        
            org.hibernate
            hibernate-entitymanager
            ${hibernate.version}
        
        
            org.hibernate
            hibernate-validator
            5.2.1.Final
        
        

        
        
            c3p0
            c3p0
            ${c3p0.version}
        
        

        
        
            log4j
            log4j
            ${log4j.version}
        

        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        

        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
        
        

        
        
            mysql
            mysql-connector-java
            ${mysql.version}
        

        
            org.springframework.data
            spring-data-jpa
            1.9.0.RELEASE
        

        
            org.springframework
            spring-test
            4.2.4.RELEASE
        
        
        
          
            javax.el  
            javax.el-api  
            2.2.4  
          
          
          
            org.glassfish.web  
            javax.el  
            2.2.4  
         
        
    

第二步:创建核心配置文件

配置数据源

配置实体管理器工厂

注入数据源

注入实体类扫描

配置Hibernate底层供应商信息

配置JPA事务管理器

注入实体管理器工厂

配置数据仓库dao加强




    
    
        
        
        
        
    

    
    
    
        
    
        
    
        
            
        
        
        
            
                
                
                
                
            
        
        
            
        
        
        
            
                create
            
        
    
    
    
    
        
    

    
    



第三步:创建实体类,标明相关注解

映射注解、表格注解、主键注解、主键生成策略注解

package com.xszx.beans;

import javax.persistence.*;

@Entity
@Table(name = "t_user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String username;
    private String password;
    private int age;

    public User() {
    }

    public User(int id, String username, String password, int age) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.age = age;
    }

    public User(int age, String password, String username) {
        this.age = age;
        this.password = password;
        this.username = username;
    }

    public int getAge() {
        return age;
    }

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

    public String getPassword() {
        return password;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getId() {
        return id;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                '}';
    }
}

第四步:创建符合SpringDataJPA规范的dao接口

继承JpaRepository,JpaSpecificationExecutor

package com.xszx.dao;

import com.xszx.beans.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface UserDao extends JpaRepository,JpaSpecificationExecutor {

}

第五步:测试

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:beans.xml")
public class UserTest {
    @Autowired
    private UserDao userDao;
    @Test
    public void add(){
        User user=new User(18,"123456","1111");
        userDao.save(user);
    }

}

你可能感兴趣的:(JavaWeb,java)