前言:
springboot整合jpa
代码:
application.yml
spring:
datasource:
username: xuhaixing
password: xuhaixing
url: jdbc:mysql://192.168.94.151:3306/mytest?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
driver-class-name: com.mysql.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
Dao层:
package com.xhx.springboot.dao;
import com.xhx.springboot.entity.Account;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @author xuhaixing
* @date 2018/5/2 11:19
*/
@Repository
public interface AccountDao extends JpaRepository<Account, Integer> {
}
Service层
package com.xhx.springboot.service.impl;
import com.xhx.springboot.dao.AccountDao;
import com.xhx.springboot.entity.Account;
import com.xhx.springboot.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author xuhaixing
* @date 2018/5/2 9:53
*/
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Override
public int add(Account account) {
accountDao.save(account);
return 1;
}
@Override
public int update(Account account) {
accountDao.save(account); //插入和更新同一个方法,内部原理先根据id查一下,没有insert,有update
return 1;
}
@Override
public int delete(int id) {
accountDao.deleteById(id);
return 1;
}
@Override
public Account findById(int id) {
return accountDao.findById(id).get();
}
@Override
public List<Account> findList() {
return accountDao.findAll();
}
}
Controller层:
package com.xhx.springboot.controller;
import com.xhx.springboot.entity.Account;
import com.xhx.springboot.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author xuhaixing
* @date 2018/5/2 9:55
*/
@RestController
@RequestMapping("account")
public class AccountController {
@Autowired
private AccountService accountService;
@RequestMapping(value = "add", method = RequestMethod.POST)
public int add(@RequestBody Account account) {
return accountService.add(account);
}
@RequestMapping(value = "update", method = RequestMethod.POST)
public int update0(@RequestBody Account account) {
return accountService.update(account);
}
@RequestMapping(value = "delete", method = RequestMethod.POST)
public int delete(@RequestParam int id) {
return accountService.delete(id);
}
@RequestMapping(value = "findById", method = RequestMethod.POST)
public Account findById(@RequestParam int id) {
return accountService.findById(id);
}
@RequestMapping(value = "findList", method = RequestMethod.POST)
public List<Account> findList() {
return accountService.findList();
}
}
entity实体类:
package com.xhx.springboot.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* @author xuhaixing
* @date 2018/4/28 10:29
*/
@Entity
public class Account {
@Id
private int id;
private String name;
private Double money;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
}
启动类:
package com.xhx.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Springboot5Application {
public static void main(String[] args) {
SpringApplication.run(Springboot5Application.class, args);
}
}
运行结果:
我的github代码 路径springboot/SpringBoot基础/springboot5/