CrudRepository接口访问数据

CrudRepository提供了最基本的对实体类的增、删、改、查操作。

示例:CrudRepository接口访问数据

程序清单:/jpa/pom.xml



	4.0.0

	com.dwx
	jpa
	0.0.1-SNAPSHOT
	jar

	jpa
	

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.5.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		
		
		 
			mysql
			mysql-connector-java
		
		
		
			org.springframework.boot
			spring-boot-starter-data-jpa
		
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

程序清单:/jpa/src/main/resources/application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.database=MySQL
spring.jpa.show-sql=true
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

程序清单:/jpa/src/main/java/com/dwx/bean/User.java

package com.dwx.bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="t_user")
public class User {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer userId;
	private String userName;
	private String sex;
	private Integer age;
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}

程序清单:/jpa/src/main/java/com/dwx/repository/UserRepository.java

package com.dwx.repository;
import org.springframework.data.repository.CrudRepository;
import com.dwx.bean.User;
public interface UserRepository extends CrudRepository {

}

程序清单:/jpa/src/main/java/com/dwx/service/UserService.java

package com.dwx.service;
import java.util.Optional;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import org.springframework.stereotype.Service;
import com.dwx.bean.User;
import com.dwx.repository.UserRepository;
@Service
public class UserService {
	@Resource
	private UserRepository userRepository;
	@Transactional
	public User save(User user) {
		return userRepository.save(user);
	}
	
	@Transactional
	public void delete(Integer id) {
		userRepository.deleteById(id);
	}
	
	@Transactional
	public void update(User user) {
		user.setUserName("露丝");
		user.setAge(23);
	}
	
	public User getById(Integer id) {
		Optional op=userRepository.findById(id);
		return op.get();
	}
	
	public Iterable getAll(){
		return userRepository.findAll();
	}
}

程序清单:/jpa/src/main/java/com/dwx/controller/UserController.java

package com.dwx.controller;
import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dwx.bean.User;
import com.dwx.service.UserService;
@RestController
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserService userService;
	@RequestMapping("/save")
	public String save() {
		User user=new User();
		user.setUserName("jack");
		user.setSex("男");
		user.setAge(22);
		userService.save(user);
		return "数据添加成功!";
	}
	
	@RequestMapping("/delete")
	public String delete() {
		userService.delete(2);
		return "删除成功!";
	}
		
	@RequestMapping("/update")
	public String update() {
		User user=userService.getById(2);
		userService.update(user);
		return "更新成功!";
	}
	
	@RequestMapping("/getAll")
	public Iterable getAll(){
		return userService.getAll();
	}
}

启动Spring Boot项目,输入以下地址:http://localhost:8080/user/save

CrudRepository接口访问数据_第1张图片

测试删除用户,输入以下地址:http://localhost:8080/user/delete

CrudRepository接口访问数据_第2张图片

测试更改用户,输入以下地址:http://localhost:8080/user/update

测试查询用户,输入以下地址:http://localhost:8080/user/getAll

CrudRepository接口访问数据_第3张图片

 

你可能感兴趣的:(spring,boot)