SpringDataJPA正向工程

》#千锋逆战#

如果数据库中有表,我们可以逆向工程,如果数据库中没有表,但是我们有实体类,我们可以使用正向工程

pom.xml


        
            junit
            junit
            4.12
        
        
            mysql
            mysql-connector-java
            5.1.44
        
        
            com.alibaba
            druid
            1.0.28
        
        
        
            org.springframework.data
            spring-data-jpa
            1.11.0.RELEASE
        
        
        
            org.hibernate
            hibernate-entitymanager
            5.2.10.Final
        
        
            org.springframework
            spring-test
            4.3.6.RELEASE
        
        
            org.projectlombok
            lombok
            1.18.6
        
    

db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE
user=root
pass=123456

spring-jpa.xml




    

    
    

    
        
        
        
        
    

    
    
        
        
        
    

    
    
        
        
        
        
            
                true
                
                validate
            
        
    

    
    
        
    
    
    
    


entity

package com.qfedu.entity;

import lombok.Data;

import javax.persistence.*;

@Data
@Entity
@Table(name = "tbl_emp")
public class Emp {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "eid")
    private int eid;
    /**
     * Column注解的各个属性值说明:
     * name用来指定该属性所对应的列名,默认与属性名一致
     * unique为true代表该属性生成的字段唯一,默认不唯一
     * nullable为false不允许为空,默认运行为空
     * length可以给字段指定长度,默认为255
     */
    @Column(name = "first_name",nullable = false,unique = true,length = 20)
    private String firstName;
    @Column(name = "last_name")
    private String lastName;
    private double Salary;
}

dao

package com.qfedu.dao;

import com.qfedu.entity.Emp;
import org.hibernate.boot.model.source.spi.JpaCallbackSource;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
@Repository
public interface IEmpDao extends JpaRepository {
}

service

package com.qfedu.service;

import com.qfedu.entity.Emp;

public interface IEmpService {
    void saveEmp(Emp emp);
}

service.impl

package com.qfedu.service.impl;

import com.qfedu.dao.IEmpDao;
import com.qfedu.entity.Emp;
import com.qfedu.service.IEmpService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class EmpServiceImpl implements IEmpService {
    @Resource
    private IEmpDao empDao;

    @Override
    public void saveEmp(Emp emp) {
        empDao.saveAndFlush(emp);
    }
}

test

package com.qfedu.test;

import com.qfedu.entity.Emp;
import com.qfedu.service.IEmpService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-jpa.xml")
public class TestEmp {
    @Resource
    private IEmpService empService;
    @Test
    public void saveEmp() {
        Emp emp = new Emp();
        emp.setFirstName("猪");
        emp.setLastName("八戒");
        emp.setSalary(200000);
        empService.saveEmp(emp);
    }
}

你可能感兴趣的:(SpringDataJPA正向工程)