SpringBoot(十六)-----Springboot整合JPA

今天用Springboot配合JPA写一个增删改查的小例子

程序结构

首先,加入JPA依赖

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>myspringboot002</groupId>
  <artifactId>myspringboot002</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <parent>
  	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-parent</artifactId>
  	<version>1.3.3.RELEASE</version>
  </parent>
  <dependencies>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-web</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-freemarker</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-jdbc</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>mysql</groupId>
  		<artifactId>mysql-connector-java</artifactId>
  		<version>8.0.17</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-data-jpa</artifactId>
  	</dependency>
  </dependencies>
</project>

  写配置文件application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&useSSL=false&serverTimezone = GMT
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

  Controller层

StudentController.java

package com.zk.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.zk.Dao.StudentDao;
import com.zk.Service.StudentService;
import com.zk.entity.Student;

@RestController
public class StudentController {
	@Autowired
	private StudentService userService;
	
	@Autowired
	private StudentDao userDao;
	
	@RequestMapping("/createUser")
	private String createUser(String Sname,String Sno,Integer Sgrade) {
		userService.createUser(Sname,Sno,Sgrade);
		return "success";
	}
	
	@RequestMapping("/updateUser")
	public String updateStudent(Integer Sid) {
		List<Student> Studentlist=userDao.findAll();
		for(Student stu:Studentlist) {
			System.out.println(stu.toString());
		}
		return "success";
	}
	
	@RequestMapping("/getUser")
	public Student getStudent(Integer Sid) {
		return userDao.findOne(Sid);
	}
	
	@RequestMapping("/deleteUser")
	public String deleteStudent(Integer Sid) {
		userDao.delete(Sid);
		return "success";
	}
}

Dao层

StudentDao.java

package com.zk.Dao;

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

import com.zk.entity.Student;

public interface StudentDao extends JpaRepository<Student,Integer>{


}

 Student.java student实体类

package com.zk.entity;

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

@Entity(name="Student")
public class Student {
	@Id 
	private Integer Sid;
	@Column
	private String Sname;
	@Column
	private String Sno;
	@Column
	private Integer Sgrade;
	
	
	
	public Integer getSid() {
		return Sid;
	}
	public void setSid(Integer sid) {
		Sid = sid;
	}
	public String getSname() {
		return Sname;
	}
	public void setSname(String sname) {
		Sname = sname;
	}
	public String getSno() {
		return Sno;
	}
	public void setSno(String sno) {
		Sno = sno;
	}
	public Integer getSgrade() {
		return Sgrade;
	}
	public void setSgrade(Integer sgrade) {
		Sgrade = sgrade;
	}
	@Override
	public String toString() {
		return "Student [Sid=" + Sid + ", Sname=" + Sname + ", Sno=" + Sno + ", Sgrade=" + Sgrade + "]";
	}
	
	
}

  Service层 StudentService.java

package com.zk.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.zk.Dao.StudentDao;

@Service
public class StudentService {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	private StudentDao userDao;
	
	public void createUser(String sname, String sno, Integer sgrade) {
		System.out.println("createUsers");
		jdbcTemplate.update("insert into student(Sname,Sno,Sgrade) value(?,?,?)",sname,sno,sgrade);
		//jdbcTemplate.execute("Select * from Student");
		System.out.println("创建用户成功");
	}
	
	public void createJPAUser(String name,Integer no,Integer grade) {
		System.out.println("createUsers");
		//jdbcTemplate.execute("Select * from Student");
		System.out.println("创建用户成功");
	}
}

  启动类

package com.zk.myspringboot007;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@ComponentScan(basePackages={"com.zk.Controller","com.zk.Service","com.zk.Dao"})
@EnableJpaRepositories("com.zk.Dao")
@EnableAutoConfiguration
@EntityScan("com.zk.entity")
public class SpringBootApplicationSixth {
	public static void main(String[]args){
        SpringApplication.run(SpringBootApplicationSixth.class, args);
    }
    public static void run(String...arg0) {
        System.out.println("Hello world from Command Line Runner");
    }
}

  测试结果如下:

增加用户

删除用户

 

获取用户

更新有点问题,还没理解具体什么原因。。。。。。

原文地址:https://www.cnblogs.com/longlyseul/p/14165953.html