SpringBot+SpringDataJpa+Oracle聚合项目踩坑

Posted iclyj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBot+SpringDataJpa+Oracle聚合项目踩坑相关的知识,希望对你有一定的参考价值。

一、整合来源

本来想着今天对接公司项目接口然而其他的接口来没有写完,于是想着能不能吧以前的SSM框架的项目整合成现在比较前沿的简易的开发技术,于是就想到把项目整合成springbot+springdatajpa+mybatis+oracle的项目,想着项目整合刚开始就先把springbot+springdatajpa先整合,想想上次整合springbot框架写项目的时候还是去年想想已经几个月了,但是自己之前整过单个的,现在整起来应该不难,但谁到真是一言难尽啊。。。

二、开始操作

我用的编译器是IDEA

2.1、首先创建一个父项目
技术图片
2.2、选择maven项目不用勾选点next即可
技术图片
2.3、填写相应的GroupId和ArtifactId点next
技术图片
2.4、选好对应的存放路径点击finish
技术图片
这样父项目就创建爱好了,等待一会删除掉src文件夹

三、创建子项目

3.1、选中mttapp->new->Module
技术图片
3.2、后面就跟上面的父项目创建类似
技术图片
技术图片
3.3、这里要注意路径现在是在mttapp下面
技术图片

四、创建好子项目

4.1、按照如上的方法依次的创建好子项目,创建好后的子项目如下
技术图片

五、添加需要的依赖

5.1、圈住的是创建好父项目自项目自动生成的
技术图片
5.2、父项目依赖这里我加的依赖比较多如果不需要的可以自行删除

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lyj</groupId>
    <artifactId>mttapp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>mttapp_common</module>
        <module>mttapp_service</module>
        <module>mttapp_base</module>
    </modules>

    <packaging>pom</packaging>

    <name>mttapp</name>
    <description>医调APP</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.35</version>
        </dependency>

        <!--使用 @ConfigurationProperties 注解需要导入它的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <!--swaggerUI-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>

        <!--thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!--aop-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>


        <!--redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

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

        <!-- Spring Boot JDBC -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- oracle -->
        <dependency>
            <groupId>com.oracle.jdbc.driver</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12c</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

5.3、mttapp_common依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mttapp</artifactId>
        <groupId>com.lyj</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mttapp_common</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.lyj</groupId>
            <artifactId>mttapp</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

5.4、mttapp_service依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mttapp</artifactId>
        <groupId>com.lyj</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mttapp_service</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.lyj</groupId>
            <artifactId>mttapp_common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>


</project>

5.5、mttapp_base依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mttapp</artifactId>
        <groupId>com.lyj</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mttapp_base</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.lyj</groupId>
            <artifactId>mttapp_service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

    </dependencies>

</project>

六、开发

6.1、项目已经构建完成接下来就是开发了首先在mttapp_common下创建result.java
技术图片

package com.lyj.util;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lyj on 2019/3/11.
 * 返回数据
 */
public class Result extends HashMap<String,Object>{

    private static final long serialVersionUID = 9109254706785913366L;

    public Result(){
        put("code",0);
    }

    public static Result error(int code,String msg){
        Result result = new Result();
        result.put("code",code);
        result.put("msg",msg);
        return result;
    }

    public static Result error(String code,String msg){
        Result result = new Result();
        result.put("code",code);
        result.put("msg",msg);
        return result;
    }

    public static Result error(){
        return error(500,"发生异常请联系管理员");
    }

    public Result error(String msg){
        return error(500,msg);
    }

    public Result put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public static Result ok(String msg) {
        Result result = new Result();
        result.put("msg", msg);
        return result;
    }

    public static Result ok(String code,String msg){
        Result result = new Result();
        result.put("code",code);
        result.put("msg",msg);
        return result;
    }

    public static Result ok(){
        return  new Result();
    }

    public static Result ok(Map<String,Object>map){
        Result result = new Result();
        result.putAll(map);
        return result;
    }

}

6.2、接下来在mttapp_service下依次创建下面的包和类
技术图片
6.3、依次下来的代码

package com.lyj.dao;

import com.lyj.entity.YdInsUserRq;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

/**
 * Created by lyj on 2019/3/11.
 */
@Repository
public interface YdInsUserDao extends JpaRepository<YdInsUserRq,String>,JpaSpecificationExecutor<YdInsUserRq> {
}
package com.lyj.entity;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;

/**
 * Created by lyj on 2019/3/11.
 */
@Data
@Entity
@Table(name = "yd_ins_user")
public class YdInsUserRq implements Serializable{

    private static final long serialVersionUID = 5437896266299288020L;

    @Id
    private Long id; // 主键

    private String userId; // 编号

    private String userName; // 姓名

    private String userPhone; // 手机

    private String userMail; // 邮箱

    private String userBankName; // 户名

    private String userDepositBank; // 开户行

    private String userBankAccount; // 银行账户

    private Long userStatus; // 用户状态(0可用,1禁用,9删除)

    private String insBrhId; // 机构表关联字段

    private String updateDate; // 最后一次操作时间

    private String operator; // 操作员

    private String userPass; // 用户密码
}
package com.lyj.service;

import com.lyj.dao.YdInsUserDao;
import com.lyj.entity.YdInsUserRq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.util.List;

/**
 * Created by lyj on 2019/3/11.
 */
@Service
public class YdInsUserService {

    @Autowired
    private YdInsUserDao ydInsUserDao;

    /**
     * 查询出所有用户信息
     * @return
     */
    public List<YdInsUserRq> findAll(){
        return ydInsUserDao.findAll();
    }

}

七、一切准备就绪接下来重点来了

7.1、依次创建如下的包和类名
技术图片
7.2、BaseApplication代码

package com.lyj;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

/**
 * Created by lyj on 2019/3/11.
 */
 //扫面聚合项目下所对应有包下类
@SpringBootApplication(scanBasePackages = {"com.lyj.**"})
public class BaseApplication {
    public static void main(String[] args) {
        SpringApplication.run(BaseApplication.class);
    }
}

其中下面这段代码非常的重要关系到你的项目能不能正常的跑起来并访问

//扫面聚合项目下所对应有包下类
@SpringBootApplication(scanBasePackages = {"com.lyj.**"})

7.3、application.yml代码
技术图片

server:
  port: 8002
spring:
  application:
    name: tensquare-base
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test? characterEncoding=utf‐8
    username: root
    password: 123456
  jpa:
    database: mysql
    show-sql: true
    hibernate:
      ddl-auto: update

连接自己的对应的数据库这里先用mysql做实例
7.4、controller层代码
技术图片

package com.lyj.controller;

import com.lyj.service.YdInsUserService;
import com.lyj.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by lyj on 2019/3/11.
 */
@RestController
@CrossOrigin
@RequestMapping(value = "user")
public class UserController {

    @Autowired
    private YdInsUserService ydInsUserService;

    @RequestMapping(value = "/usersList")
    public Result findAll(){
        return Result.ok().put("userList",ydInsUserService.findAll());
    }
}

八、运行

8.1、接下来我们就可以运行了点击下图绿色三角形出现下图即运行成功
技术图片
8.2、其中springdatajpa会自动的创建在对应数据库创建好表
技术图片
8.3、浏览器输入访问成功这里我没有输入数据出现下图即项目成功运行连接了

http://localhost:8002/user/usersList

技术图片

关注公众号了解更多学习笔记和领取学习资源

技术图片

以上是关于SpringBot+SpringDataJpa+Oracle聚合项目踩坑的主要内容,如果未能解决你的问题,请参考以下文章

SpringBot项目的创建方式

springbot单机秒杀,锁与事务之间的大坑

SpringBot添加多数据源mysql的验证方法

Spring Boot2 系列教程 | 使用 Spring Data JPA 访问 Mysql

Spring Boot教程35——Spring Data JPA

SpringDataJPA