springBoot+mybatisPlus小demo

項目介紹:採用restful api進行接口規範 / 項目框架SpringBoot+mybatis Plus / 採用mysql進行數據存儲 / 採用swaggerUI進行先後端業務分離式開發。html

開發環境:JDK1.8+Mysql8.0.12+IDEALjava

實現功能:springboot搭建總體框架,MybatisPlus動態生成Dao+Services+Entity+Controller結構mysql

項目介紹:無實際的業務操做,都是測試功能。其中爲了區別mybastis和mybatisPlus,特地寫了兩個接口分別採用mapper.xml進行dao層操做和採用Iservice提供的CRUD操做進行數據查詢。git

ps:若是對springboot和mybayisPlus已瞭解,可直接訪問碼雲進行demo下載 https://gitee.com/xc199534/SpringBootMybatisPlus/web

----------------------------------------------------------分割線---------------------------------------------redis

1.項目總體結構spring

其中Config中包括MybatisPlusConfig和SwaggerConfig兩個類,分別用於初始化MybatisPlus和Swagger基礎設置。sql

數據庫結構,採用navicat進行可視化管理。數據庫

 

 

2.配置文件application.propertiesapache

也可採用.yml格式進行配置文件設置,本項目是使用.properties。

spring.application.name=spring-boot-config
server.port=8080
server.context-path=/

#mybatis mapper文件的位置
mybatis.mapper-locations=classpath*:mapper/*.xml
#掃描pojo類的位置,在此處指明掃描實體類的包,在mapper中就能夠不用寫pojo類的全路徑名了
mybatis.type-aliases-package=com.example.demo
#能夠經過mybatis.config-location屬性來指定mybatis的配置文件的位置,
#mybatis.config-location=classpath:mybatis-config.xml

#數據庫參數設置 jdbc.type=mysql spring.datasource.url=jdbc:mysql://localhost:3306/demo spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver

 

 

3.pom.xml內容(全部不上項目完整pom.xml的demo都是耍流氓)

<dependencies>
        <!-- Springboot-->
        <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>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <!-- <version>2.1.3</version>   -->
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <!-- <version>3.0.9</version> -->
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- 阿里巴巴druid數據庫鏈接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

        <!-- mysql驅動 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- mybatisplus與springboot整合 -->
        <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>2.3</version>
        </dependency>


        <!-- 模板引擎 -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>

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

    </dependencies>

 

3.自動生成代碼(Dao/service/Entity)

調用MpGenerator.java的主函數生成定義好MVC層結構(MpGenerator定義了文件位置,文件名稱,依賴關係等屬性)。

Dao層:生成*Dao.java文件和*Dao.xml,後者可理解爲Dao層的數據庫映射操做,mybatis支持在.xml文件中寫數據庫CURD語句,但myatisPlus支持使用內置的CRUD操做,避免了複雜的數據庫語句。

Service層 :生成一個接口文件,一個接口實現類。

Entity層:生成實體類,對應到數據庫的表結構。

SchoolDao.java

package com.example.demo.dao;


import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.example.demo.entity.School;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;


public interface SchoolDao extends BaseMapper<School> {
    School getSchoolById(@Param("id") int id);
}
View Code

SchoolService.java

import com.baomidou.mybatisplus.service.IService;
import com.example.demo.entity.School;
import org.elasticsearch.index.query.QueryStringQueryBuilder;

public interface SchoolService extends IService<School> {
    public School getSchoolById(int id);
    // public Iterable<School> searchByES(QueryStringQueryBuilder builder);

}
View Code

SchoolServiceImpl.java

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.example.demo.dao.SchoolDao;
import com.example.demo.entity.School;
import com.example.demo.services.SchoolService;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolDao, School> implements SchoolService {
    @Override
    public School getSchoolById(int id) {
        return baseMapper.selectById(id);
    }
}
View Code

School.java

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import org.springframework.data.elasticsearch.annotations.Document;

import java.io.Serializable;

@TableName("school")
@Document(indexName = "education", type = "school")
public class School implements Serializable {
    @TableId("ID")
    private int id;

    @TableField("NAME")
    private String name;

    @TableField("RANGE")
    private int range;

    @TableField("AGE")
    private int age;

    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 int getRange() { return range; }
    public void setRange(int range) { this.range = range; }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "School{" +
                "id=" + id +
                ", name='" + name +
                ", range=" + range +
                ", age=" + age +
                '}';
    }
}
View Code

 

四、Controller層

@RestController
@RequestMapping("/")
public class SampleController {
    @Autowired
    private UserService userService;

    @Autowired
    private SchoolService schoolService;

    @Autowired
    private testDao testdao;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @ApiOperation(value = "測試")
    @GetMapping("/home")
    String home() {
        return "Hello World!";
    }

    @RequestMapping(value = "/getUser", method = RequestMethod.GET)
    public User helloUser() {
        User user = userService.getUserById(1);
        System.out.println(user);
        return user;
    }

    @ApiOperation(value = "採起mybatis方式查詢")
    @ApiImplicitParam(name = "id", value = "用戶id", paramType = "path", required = true)
    @GetMapping("/getSchool/maybatis/{id}")
    public School helloSchool1(@PathVariable("id") int id) {
        School school = schoolService.getSchoolById(id);
        System.out.println(school);
        return school;
    }

    @ApiOperation(value = "採起mybatis-plus方式查詢")
    @ApiImplicitParam(name = "id", value = "用戶id", paramType = "path", required = true)
    @GetMapping("/getSchool/maybatis-plus/{id}")
    public School helloSchool2(@PathVariable("id") int id) {
        School school = schoolService.selectById(id);
        System.out.println(school);
        return school;
    }

    @ApiOperation(value = "插入數據")
    @ApiImplicitParam(name = "school", value = "學校信息", paramType = "body", required = true)
    @PostMapping("/insert/school")
    public Boolean insertSchool(@RequestBody School school) {
        Boolean tag = schoolService.insert(school);
        System.out.println(tag);
        return tag;
    }
}
View Code

 

五、調用主程序啓動

@SpringBootApplication
@MapperScan("com.example.demo.dao")
@ComponentScan(basePackages = {
        "com.example.demo.config",
        "com.example.demo.controller",
        "com.example.demo.services"})
public class DemoApplication {

    public static void main(String[] args) {

        //System.setProperty("es.set.netty.runtime.available.processors", "false");
        SpringApplication.run(DemoApplication.class, args);
    }
}

 啓動成功後訪問:http://localhost:8080/swagger-ui.html#/,顯示下圖效果即配置成功。

 

相關文章
相關標籤/搜索