SpringBoot整合JPARepository

SpringBoot与JPARepository的整合

1.SpringBoot与数据库进行操作的方法

     阅读SpringBoot的说明文档,SpringBoot操作数据库的方法有很多

  1. Using Jdbc Template
  2. Jpa and Spring Date Jpa
  3. Spring Data JDBC
  4. Using H2's Web Console
  5. Using JOOQ

     本篇介绍SpringBoot与Jpa的整合

2.配置文件

     本篇是以web项目进行展开介绍,使用MySql数据库。

     在配置文件中设置服务的端口号

server.port=8085

     并进行数据库的相关配置,包括连接的地址,MySql的帐号密码,连接方式

spring.datasource.url=jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Hongkong
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=

     这里由于MySql版本和SpringBoot版本的问题,需要把连接com.mysql.jdbc.Driver修改为com.mysql.cj.jdbc.Driver,并在url处增加时区serverTimezone=Hongkong

     下面的配置文件包括在调用完成接口的调用,在eclipse控制台显示sql语句,以及自动建表

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

3.xml配置文件

     在创建SpringBoot项目的时候,就已经选择了MySql,Jpa,以及web。因此相关的信息会在xml中自动生成

        
			org.springframework.boot
			spring-boot-starter-data-jpa
		
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			mysql
			mysql-connector-java
			runtime
		

4.Java代码部分

4 .1数据库表对应的实体类



import java.io.Serializable;

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

@Entity
//表名
@Table(name="chenxin")
public class UserInfo implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//主键
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Integer id;
	//列名
	@Column(name="name")
	public String name;
	@Override
	public String toString() {
		return "UserInfo [id=" + id + ", name=" + name + ", sex=" + sex + "]";
	}
	@Column(name="sex")
	public String sex;
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

     @Table注解,对应数据库中的一个表,name为表名。不填写name,表名为类名;

     @Id 表的主键

     @Column表的列,name为列名,不填则默认属性名

4.2继承JpaRepository接口,进行数据库交互

import org.springframework.data.jpa.repository.JpaRepository;
/*
 * 继承JpaRepository,第一个为实体类名,第二个为主键类型
 */

import com.example.demo.bean.UserInfo;

public interface UserRespority extends JpaRepository {

}

4.3写一个控制器

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.Dao.UserRespority;
import com.example.demo.bean.UserInfo;
@RestController
public class UserController {
 
    @Autowired
    private UserRespority userRespority;
 
 
    @GetMapping("/insert")
    public UserInfo insertuser(UserInfo user){
    	UserInfo save = userRespority.save(user);
        return save;
    }
 
    @RequestMapping(value="/getId/{id}")
    public UserInfo getUser(@PathVariable("id")Integer id) {
    	UserInfo user=userRespority.findById(id).get();
    	return user;
    }
    @RequestMapping(value="/getAll")
    public List getUser() {
    	List list=userRespority.findAll();
    	return list;
    }
    @RequestMapping(value="/delete/{id}")
    public String delter(@PathVariable("id")Integer id) {
    	userRespority.deleteById(id);
    	return "success";
    }
}

     不要忘记@Autowired注解!!!

5.测试

  SpringBoot整合JPARepository_第1张图片   SpringBoot整合JPARepository_第2张图片SpringBoot整合JPARepository_第3张图片SpringBoot整合JPARepository_第4张图片

 

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