标签:response app ica source 发送请求 show val sql数据库 factory
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot?serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=123456 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.show-sql= true
@Entity代表这是一个实体类,@Table(name=”user”)用来对应数据库中的use表,@Id用来表达主键,@Column(name=”id”)表明一个id属性。
@GeneratedValue使主键自增,如果还有疑问,可参考@GeneratedValue源码解析。
package com.example.demo.domain; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "user") public class User implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue private Long id; @Column(name = "username") private String userName; @Column(name = "password") private String passWord; public User() { super(); } public User(String userName, String passWord) { super(); this.userName = userName; this.passWord = passWord; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } }
package com.example.demo.dao; import org.springframework.data.jpa.repository.JpaRepository; import com.example.demo.domain.User; public interface UserRepository extends JpaRepository<User, Long> { User findByUserName(String userName); }
package com.example.demo.controller; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.example.demo.dao.UserRepository; import com.example.demo.domain.User; @RestController @RequestMapping("user") public class UserController { @Autowired private UserRepository userRepository; @RequestMapping("/getAllUser") @ResponseBody public List<User> findAll() { List<User> list = new ArrayList<User>(); list = userRepository.findAll(); return list; } @RequestMapping("/getByUserName") @ResponseBody public User getByUserName(String userName) { User user = userRepository.findByUserName(userName); return user; } }
工程添加文件后工程结构图:
在user表中,插入两条测试数据:
http://localhost:8080//user/getByUserName?userName=Turing :
标签:response app ica source 发送请求 show val sql数据库 factory
原文地址:https://www.cnblogs.com/xing-nb/p/12146197.html