170622、springboot编程之JPA操作数据库

JPA操作数据库

什么事JAP?JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。

1、在pom中引用jpa

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

2、配置application.properties文件

###datasource 数据源配置
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
###Java Persistence Api
#指定的数据库
spring.jpa.database=MYSQL
#显示每个SQL
spring.jpa.show-sql=true
#Hibernate DDL自动(创建,创建-删除,更新)
spring.jpa.hibernate.ddl-auto=update
#命名策略
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
#在将其添加到实体管理剥离
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

3、编写UserRepository.java文件

package com.rick.apps.repository;


import com.rick.apps.entity.User;
import org.springframework.data.repository.CrudRepository;
/**
 * Desc :  CrudRepository是springboot提供的封装方法
 * 里面封装了基本的增删改查的方法,方便调用
 * 方法如下:
 *       <S extends T> S save(S var1);
         <S extends T> Iterable<S> save(Iterable<S> var1);
         T findOne(ID var1);
         boolean exists(ID var1);
         Iterable<T> findAll();
         Iterable<T> findAll(Iterable<ID> var1);
         long count();
         void delete(ID var1);
         void delete(T var1);
         void delete(Iterable<? extends T> var1);
         void deleteAll();
 * User : RICK
 * Time : 2017/8/21 15:45
  */

public interface UserRepository extends CrudRepository<User,Integer> {

}

4、编写UserService.java

import com.rick.apps.entity.User;
import com.rick.apps.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    @Transactional
    public void save(User user){
        userRepository.save(user);
    }
}

注:我只写了一个保存方法,其他的调用方法雷同,再次不一一展示

5、编写HelloController.java

package com.rick.apps.controller;

import com.rick.apps.entity.User;
import com.rick.apps.service.UserService;
import com.rick.common.ResultJson;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
public class HelloController {

    @Resource
    private UserService userService;

    @RequestMapping("/hello")
    public String hello(){
        System.out.println(1/0);
        return "Hello World!";
    }

    @GetMapping(value = "/save")
    public ResultJson saveDemo(){
        User user = new User();
        user.setUserName("rick");
        user.setPassWord("123456");
        userService.save(user);
        ResultJson resultJson = ResultJson.buildSuccessInstance();
        return resultJson;
    }
}

注:ResultJson是我自己封装的一个返回对象类,方便大家测试,我也把这个类的代码给贴出来

package com.rick.common;

import java.io.Serializable;

public class ResultJson implements Serializable{
    private static final long serialVersionUID = 1L;
    //返回码
    private String code;
    //返回提示
    private String msg;
    //返回内容
    private Object data;
    
    public ResultJson(){        
    }
    
    public ResultJson(String msg){
        this.code = "1";
        this.msg = msg;
    }
    
    public ResultJson(Object data){
        this.code = "0";
        this.msg = "成功";
        this.data = data;
    }
    
    public ResultJson(String code, String msg){
        this.code = code;
        this.msg = msg;
    }
    
    public ResultJson(String code, String msg, Object data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
    
    public static ResultJson buildSuccessInstance(){
        return new ResultJson("0", "操作成功!", null);
    }
    
    public static ResultJson buildSuccessInstance(String msg){
        return new ResultJson("0", msg, null);
    }

    public static ResultJson buildSuccessInstance(Object data){
        return new ResultJson("0", "操作成功!", data);
    }
    
    public static ResultJson buildSuccessInstance(String msg, Object data){
        return new ResultJson("0", msg, data);
    }
    
    public static ResultJson buildFailInstance(){
        return new ResultJson("1", "操作异常!", null);
    }
    
    public static ResultJson buildFailInstance(String msg){
        return new ResultJson("1", msg, null);
    }
    
    /**
     * TODO: 自定义返回异常信息
     * @Auhor: RICK
     * @Date : 2017年6月26日
     * @param code 99 表示没有获取到经纬度信息
     * @param msg
     * @return
     */
    public static ResultJson buildFailInstance(String code,String msg){
        return new ResultJson(code, msg, null);
    }
    
}

6、启动项目测试

数据库添加数据成功:

项目截图

 

posted @ 2017-08-21 15:53  目标奔雷手  阅读(281)  评论(0编辑  收藏  举报