Spring Boot 之 JPA 和 Controller接收参数

在pom.xml中添加依赖包

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>

在application.properties中增加数据库配置

注意spring.jpa.properties.hibernate.hbm2ddl.auto的选择,create每次都会重新建表

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=!QAZ2wsx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

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

增加模型类

@Entity
@Data
public class User implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue
	private Long id;
	
	@Column(nullable = false, unique = true)
	private String userName;
	
	@Column(nullable = false)
	private String passWord;
	
	@Column(nullable = false)
	private String email;
	
	@Column(nullable = true)
	private String nickName;
	
	@Column(nullable = false)
	private String regTime;
}

增加模型接口

public interface UserRepository extends JpaRepository<User, Long> {
}

增加接收参数并操作数据库

@RestController
public class UserController {
	
	@Autowired
    private UserRepository userRepository;
	
    @RequestMapping("/list")
    public List<User> list() {
    	return userRepository.findAll();
    }
    
    @RequestMapping("/add")
    // /add?userName=c&passWord=c&email=c&regTime=c&nickName=c
    public List<User> add(
    		@RequestParam(name = "userName") String userName,
    		@RequestParam(name = "passWord") String passWord,
    		@RequestParam(name = "email") String email,
    		@RequestParam(name = "regTime") String regTime,
    		@RequestParam(name = "nickName") String nickName) {
    
    	User user = new User();
    	user.setUserName(userName);
    	user.setPassWord(passWord);
    	user.setEmail(email);
    	user.setRegTime(regTime);
    	user.setNickName(nickName);
    	userRepository.save(user);
    	return userRepository.findAll();
    }
    
    @RequestMapping("/del/{id}")
    // /del/{1}
    public List<User> del(@PathVariable(name = "id") Long id) {
    
    	userRepository.deleteById(id);
    	return userRepository.findAll();
    }

}

接收参数的几种形式

参考:https://blog.csdn.net/suki_rong/article/details/80445880

  • get: url/{id}
  • get: url?name=
  • post:
  • request header
  • cookie

提示时区错误的应对

  • 修改mysql时区,参考 https://www.cnblogs.com/shiqiangqiang/p/8393662.html
  • 如果需要使用gmt+8时区,在数据库连接url后添加:?serverTimezone=GMT%2B8

参考文档

  • http://www.ityouknow.com/springboot/2016/02/03/spring-boot-web.html
  • http://www.ityouknow.com/springboot/2016/08/20/spring-boot-jpa.html
  • https://www.jianshu.com/p/c23c82a8fcfc
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值