一、添加依赖
数据库框架主要使用了sping-data-jpa
<!--spring-data-jpa框架-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
二、springboot的配置
1、application.yml文件的配置
spring:
profiles:
active: dev
#数据库驱动等一些信息
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springboot
username: root
password: root
#spring-data-jpa的配置
jpa:
hibernate:
#设置自动创建数据库
#create:先删除,再创建表,以前表的数据清除
#update:不删除以前的数据
#create-drop:当应用停下来的时候删除
#none:默认什么也不做
#validate:先验证实体类和表结构是否一致,不一致就会报错
ddl-auto: create
#设置显示执行的sql语句
show-sql: true
2、设置实体类
User.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* 用户
*
* @author MiChong
* @Email 1564666023@qq.com
* @create 2018/2/21 0021
* @Version:V1.0
*/
@Entity
public class User {
/**
* 设置主键的配置
*/
@Id
@GeneratedValue
private Long id;
private String username;
private int userage;
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 int getUserage() {
return userage;
}
public void setUserage(int userage) {
this.userage = userage;
}
}
3、设置dao层接口
UserRespository.java
import cn.buildworld.girl.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
/**
* 数据操作层接口
*
* @author MiChong
* @Email 1564666023@qq.com
* @create 2018/2/21 0021
* @Version:V1.0
*/
public interface UserRespository extends JpaRepository<User,Long> {
/**
* 自定义接口,通过年龄来查询用户
* @param age
* @return
*/
public List<User> findByUserage(Integer age);
}
4、web层的控制器实现我们所需的功能(增删改查)
UserController.java
import cn.buildworld.girl.pojo.User;
import cn.buildworld.girl.web.dao.UserRespository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 用户的控制器
*
* @author MiChong
* @Email 1564666023@qq.com
* @create 2018/2/21 0021
* @Version:V1.0
*/
@RestController
public class PersonController {
@Autowired
private UserRespository userRespository;
/**
* 通过get方式获得数据所有的数据集合
* @return
*/
@GetMapping(value = "getInfo")
public List<User> getInfo(){
List<User> all = userRespository.findAll();
return all;
}
/**
* POST方式
* @return
*/
@PostMapping(value = "getInfo")
public String getInfoByPost(){
return "请使用get方式!";
}
/**
* POST方式添加一个用户
* @param user
* @return
*/
@PostMapping(value = "adduser")
public User addUser(User user){
userRespository.save(user);
return user;
}
/**
* GET方式查询一个用户
* @param id
* @return
*/
@GetMapping(value = "user/{id}")
public User checkUser(@PathVariable("id")Long id){
return userRespository.findOne(id);
}
/**
* PUT方式更新一个用户
* @param id
* @return
*/
@PutMapping(value = "user/{id}")
public User updateUser(@PathVariable("id")Long id,User user){
user.setId(id);
userRespository.save(user);
return userRespository.findOne(id);
}
/**
* DELETE方式删除一个用户
* @param id
* @return
*/
@DeleteMapping(value = "user/{id}")
public List<User> deleteUser(@PathVariable("id")Long id){
userRespository.delete(id);
return userRespository.findAll();
}
/**
* 通过年龄来查询用户信息
* @param age
* @return
*/
@GetMapping(value = "user/age/{age}")
public List<User> findUserByAge(@PathVariable("age")int age){
return userRespository.findByUserage(age);
}
}
5、事务
UserService.java
import cn.buildworld.girl.web.dao.UserRespository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* 用户服务接口
*
* @author MiChong
* @Email 1564666023@qq.com
* @create 2018/2/21 0021
* @Version:V1.0
*/
@Service
public class UserService {
@Autowired
private UserRespository userRespository;
/**
* 测试事务,同时插入两条数据
*/
@Transactional(rollbackFor = Exception.class)
public void insertTwo(){
//doing one
//doing two
}
}