springboot+jpa 简单实例创建

参考:https://blog.csdn.net/wazz753/article/details/72472411#commentsedit

1.展示效果



2.开始

2.1创建maven工程


2.2修改pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.lynash.springboot</groupId>
  <artifactId>jpa-springboot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
    <parent>  
   <groupId>org.springframework.boot</groupId>  
   <artifactId>spring-boot-starter-parent</artifactId>  
   <version>1.5.2.RELEASE</version>  
</parent>  
  <dependencies>  
    <dependency>  
      <groupId>junit</groupId>  
      <artifactId>junit</artifactId>  
      <version>3.8.1</version>  
      <scope>test</scope>  
    </dependency>  
    <dependency>  
       <groupId>org.springframework.boot</groupId>  
       <artifactId>spring-boot-starter-web</artifactId>  
    </dependency>  
    <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
    <dependency>  
   <groupId>org.springframework.boot</groupId>  
   <artifactId>spring-boot-starter-data-jpa</artifactId>  
    </dependency>  
     
<dependency>  
           <groupId>org.springframework.boot</groupId>  
           <artifactId>spring-boot-starter-thymeleaf</artifactId>  
</dependency>  
    
  </dependencies>  

</project>

2.3添加配置文件

---

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/jpa_test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
    username: root
    password: 111111
    driver-class-name: com.mysql.jdbc.Driver
    time-between-eviction-runs-millis: 300000
    num-tests-per-eviction-run: 3
    min-evictable-idle-time-millis: 3600000
    test-on-borrow: true
    validation-query: select 1
  jpa:

    generate-ddl: true

2.5建立相应的文件

Application.java

package com.lynash.jpa;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication   
/** 
 * 要将Application放在最外层,也就是要包含所有子包。 
比如你的groupId是com.google,子包就是所谓的com.google.xxx,所以要将Application放在com.google包下。 
请参考以下结论:spring-boot会自动加载启动类所在包下及其子包下的所有组件. 
 * @author LHC 
 * 
 */  
public class Application {   
   
    public static void main(String[] args) {   
        SpringApplication.run(Application.class,args);   
    }   
}   

Emp.java

package com.lynash.jpa.bean;

import java.util.Date;

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

@Entity  
@Table(name="emp2")  
public class Emp {  
   
	private int eid;  
	private String ename;  
	private String sex;  
	private Date hire;  
	private float sar;  
	private int did;  
	   
	@Id  
	@GeneratedValue  
	public int getEid() {  
	           return eid;  
	}  
	   
	public void setEid(int eid) {  
	           this.eid = eid;  
	}  
	   
	public String getEname() {  
	           return ename;  
	}  
	   
	public void setEname(String ename) {  
	           this.ename = ename;  
	}  
	   
	public String getSex() {  
	           return sex;  
	}  
	   
	public void setSex(String sex) {  
	           this.sex = sex;  
	}  
	   
	public Date getHire() {  
	           return hire;  
	}  
	   
	public void setHire(Date hire) {  
	           this.hire = hire;  
	}  
	   
	public float getSar() {  
	           return sar;  
	}  
	   
	public void setSar(float sar) {  
	           this.sar = sar;  
	}  
	   
	public int getDid() {  
	           return did;  
	}  
	   
	public void setDid(int did) {  
	           this.did = did;  
	}  
}  

IEmpService.java

package com.lynash.jpa.service;

import java.util.List;

public interface IEmpService {
	public List queryEmpAll();  
}

EmpServiceImpl.java

package com.lynash.jpa.service.impl;

import java.util.List;

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

import com.lynash.jpa.dao.IEmpDao;
import com.lynash.jpa.service.IEmpService;

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

        @Override  
        public List queryEmpAll() {  
             return empDao.findAll(); 
	}

}

IEmpDao.java

package com.lynash.jpa.dao;

import org.springframework.data.jpa.repository.JpaRepository;

import com.lynash.jpa.bean.Emp;  
  
   
public interface IEmpDao extends JpaRepository<Emp, Integer> {  
   
}  

HelloController.java

package com.lynash.jpa.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lynash.jpa.bean.Emp;
import com.lynash.jpa.service.IEmpService;

@RestController
public class HelloController {
    @Autowired  
    private IEmpService empService;  

    @RequestMapping("/queryEmpAll")  
    public List queryEmpAll() {  
              List<Emp>empList=empService.queryEmpAll();  
              for(Emp emp : empList) {  
                   System.out.println(emp.getEname());  
              }  
              return empList;  
    }  
}

3.运行

http://localhost:8080/queryEmpAll

代码https://download.csdn.net/download/liyang_nash/10478746

阅读更多
个人分类: java_web
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭