聚合项目踩坑SpringBoot+SpringDataJpa+Oracle

84人浏览 / 0人评论

原文链接

一、整合来源 本来想着今天对接公司项目接口然而其他的接口来没有写完,于是想着能不能吧以前的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、父项目依赖这里我加的依赖比较多如果不需要的可以自行删除

4.0.0

<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>
5.3、mttapp_common依赖

mttapp com.lyj 1.0-SNAPSHOT 4.0.0

<artifactId>mttapp_common</artifactId>

<dependencies>
    <dependency>
        <groupId>com.lyj</groupId>
        <artifactId>mttapp</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>
5.4、mttapp_service依赖

mttapp com.lyj 1.0-SNAPSHOT 4.0.0

<artifactId>mttapp_service</artifactId>

<dependencies>
    <dependency>
        <groupId>com.lyj</groupId>
        <artifactId>mttapp_common</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>
5.5、mttapp_base依赖

mttapp com.lyj 1.0-SNAPSHOT 4.0.0

<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>
六、开发 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 { } 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 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 在这里插入图片描述

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

全部评论