1,myslq配置
server.port=8099
spring.datasource.url=jdbc:mysql://localhost:3306/share
spring.datasource.username=root
spring.datasource.password=650249Msm
spring.datasource.tomcat.max-active=100
spring.datasource.tomcat.max-idle=200
spring.datasource.tomcat.initialSize=20
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
2,目录结构
3,meaven配置
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4,controller
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
@RestController
public class HelloWorldController {
@Autowired
AccountServiceaccountService;
@RequestMapping(path ="/regist", method = RequestMethod.PUT,consumes ="application/json", produces ="application/json")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Account addAccountInfo (@RequestBody Account account) {
System.out.println(account);
accountService.save(account);
return account;
}
@RequestMapping(path="/find/{username}", method = RequestMethod.GET,consumes ="application/json", produces ="application/json" )
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Account searchAcountInfo(HttpServletRequest request,@PathVariable String username) {
Account account =new Account();
account =accountService.find(username);
return account;
}
}
5,service
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AccountService {
@Autowired
private AccountRepositoryaccountrepository;
public Account find(String username) {
return accountrepository.findFirstByUsername(username);
}
public Account save (Account account) {
accountrepository.save(account);
return account;
}
}
6,entity
package com.example.demo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name="accout")
@Entity
public class Account {
private Stringusername;
private Stringpassword;
@Id
@Column(name="USERNAME")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name="PASSWORD")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
6,repo
package com.example.demo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface AccountRepositoryextends JpaRepository {
Account findFirstByUsername(String username);
}