数据库
首页 > 数据库> > SpringBoot:Sqlite3+SpringBoot2.1.3+Mybatis-Puls整合项目

SpringBoot:Sqlite3+SpringBoot2.1.3+Mybatis-Puls整合项目

作者:互联网

应公司要求完成sqlite3数据库的增改查小功能,特此记录一下。

1.建造项目

结构如下

 

因为是提供给前端调用所以做了接口。

2.Pom依赖文件

下面是这个项目所依赖的jar包。

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!-- 统一管理jar包版本 -->
    <properties>
        <java.version>12</java.version>
        <mybatis.spring.boot.version>2.0.0</mybatis.spring.boot.version>
        <sqlite.jdbc.version>3.27.2.1</sqlite.jdbc.version>
    </properties>

    <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>
        <!-- sqlite3驱动包 -->
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>${sqlite.jdbc.version}</version>
        </dependency>
        <!-- Springboot兼容Mybatis版本 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.spring.boot.version}</version>
        </dependency>
        <!-- mybatis-Plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.6</version>
        </dependency>
        <!-- 阿里FastJson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.73</version>
        </dependency>
    </dependencies>

    <!-- SpringBoot兼容maven打包工具 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

3.application.yml配置文件

//当数据库目录位于 resources/sqlite/dome.db 时 DriverManager.getConnection("jdbc:sqlite::resource:sqlite/dome.db");即配置文件应该写jdbc:sqlite::resource:sqlite/dome.db

#端口
server: port: 8190 #spring spring: datasource:
#sqlite3驱动 driver-class-name: org.sqlite.JDBC # 方式一: 引用外部文件 # url: jdbc:sqlite:D:/eclipse/dome.db #方式二: 引用项目中的文件 url: jdbc:sqlite::resource:sqlite/dome.db username: password: # sql打印 logging: level: debug level.com.xuanyin: debug path: logs/ # file: admin.log

4.java实体类

接下来是java各个实体类。

DomeController.java

package com.java.common.controller;

import com.alibaba.fastjson.JSONObject;
import com.java.common.entity.Dome;
import com.java.common.service.DomeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class DomeController {
    private static final Logger logger = LoggerFactory.getLogger(DomeController.class);

    @Autowired
    DomeService domeService;

    @RequestMapping("/select")
    @ResponseBody
    public String getSelect(@RequestParam("title")String title){
        Dome dome = new Dome();
        logger.info(" = = = title : {} = = = ",title);
        if(title != null){
            dome = domeService.selectData(title);
            if(dome != null){
                String json = JSONObject.toJSONString(dome);
                logger.info(" = = = json : {} = = = ",json);
                return json;
            }
        }
        return "主题字段为空!";
    }

    @RequestMapping("/insert")
    @ResponseBody
    public String getInsert(@RequestBody Dome dome){
        int result = 0;
        logger.info(" = = = dome : {} = = = ", JSONObject.toJSONString(dome));
        if(dome != null) {
            result = domeService.insertData(dome);
            logger.info(" = = = result : {} = = = ", result);
        }
        if(result > 0){
            return "添加成功!!";
        }
        return "添加失败!!";
    }

    @RequestMapping("/update")
    @ResponseBody
    public String getUpdate(@RequestBody Dome dome){
        int result = 0;
        logger.info(" = = = dome : {} = = = ", JSONObject.toJSONString(dome));
        if(dome != null){
            result = domeService.updateData(dome);
            logger.info(" = = = result : {} = = = ", result);
        }
        if(result > 0){
            return "修改成功!!";
        }
        return "修改失败!!";
    }
}

Dome.java

package com.java.common.entity;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

@TableName("dome")
public class Dome {

    //主键
    @TableId("domeId")
    private Integer domeId;

    //主题
    private String title;

    //内容
    private String content;

    //分类
    private String type;

    public Integer getDomeId() {
        return domeId;
    }

    public void setDomeId(Integer domeId) {
        this.domeId = domeId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

DomeMapper.java

package com.java.common.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.java.common.entity.Dome;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface DomeMapper extends BaseMapper<Dome> {

}

DomeService.java

package com.java.common.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.java.common.entity.Dome;

public interface DomeService extends IService<Dome> {

    //查询
    Dome selectData(String title);

    //添加
    int insertData(Dome dome);

    //修改
    int updateData(Dome dome);

}

DomeServiceImpl.java

package com.java.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.EmptyWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.common.entity.Dome;
import com.java.common.mapper.DomeMapper;
import com.java.common.service.DomeService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class DomeServiceImpl extends ServiceImpl<DomeMapper, Dome> implements DomeService {


    @Override
    public Dome selectData(String title) {
        QueryWrapper<Dome> domeWrapper = new QueryWrapper<>();
        domeWrapper.eq("title", title);
        Dome dome = baseMapper.selectOne(domeWrapper);
        return dome;
    }

    @Override
    public int insertData(Dome dome) {
        String str = String.valueOf(new Date().getTime());
        int domeId = Integer.valueOf(str.substring(5,str.length()));
        dome.setDomeId(domeId);
        int result = baseMapper.insert(dome);
        return result;
    }

    @Override
    public int updateData(Dome dome) {
        UpdateWrapper<Dome> domeWrapper = new UpdateWrapper<>();
        domeWrapper.eq("title", dome.getTitle());
        int result = baseMapper.update(dome,domeWrapper);
        return result;
    }
}

DomeApplication.java

package com.java.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DomeApplication {
    private final static Logger logger = LoggerFactory.getLogger(DomeApplication.class);

    public static void main(String[] args) {
        logger.info(" = = = 服务开始启动 = = = ");
        SpringApplication.run(DomeApplication.class,args);
        logger.info(" = = = 服务启动完成 = = = ");
    }

}

5.sqlite3数据库文件

参照  文章自行创建。

标签:java,SpringBoot,dome,Sqlite3,Mybatis,import,com,public,String
来源: https://www.cnblogs.com/nhdlb/p/14292167.html