欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot整合Mybatis和SQLite的詳細(xì)過程

 更新時(shí)間:2024年07月16日 10:46:30   作者:orzdh  
這篇文章主要介紹了Springboot整合Mybatis和SQLite的詳細(xì)過程,本文通過圖文示例相結(jié)合給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧

項(xiàng)目目錄

SQLite中的數(shù)據(jù)

maven的pom.xml導(dǎo)入所需要的依賴

 <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.8.11.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

創(chuàng)建SQLite需要的文件

在resources目錄下創(chuàng)建databases.db,如下圖

配置yml文件

server:
  port: 8888
spring:
  mvc:
    static-path-pattern: classpath:/**
  # 配置 Oracle
  datasource:
    driver-class-name: org.sqlite.JDBC  #數(shù)據(jù)庫鏈接驅(qū)動(dòng)
    url: jdbc:sqlite::resource:databases/data.db #數(shù)據(jù)庫鏈接地址
    username:
    password:
    type: com.alibaba.druid.pool.DruidDataSource
# mybatis配置
mybatis:
  mapper-locations: classpath:mapper/*.xml    # mapper映射文件位置
  type-aliases-package: cn.xdedm.entity    # 實(shí)體類所在的位置
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl   #用于控制臺(tái)打印sql語句
    map-underscore-to-camel-case: true

Msg類

package cn.xdedm.entity;
import java.util.HashMap;
import java.util.Map;
public class Msg {
    //狀態(tài)碼 10001 成功 10002失敗
    private int code;
    private String msg;
    private Map<String,Object> extend=new HashMap<String,Object>();
    /**
     * 返回成功
     * @return
     */
    public static  Msg Success (){
        Msg result = new Msg();
        result.setCode(10001);
        result.setMsg("成功");
        return result;
    }
    /***
     * 返回失敗
     * @return
     */
    public static  Msg fail (){
        Msg result = new Msg();
        result.setCode(10002);
        result.setMsg("失敗");
        return result;
    }
    public Msg Add(String key,Object value){
        this.getExtend().put(key,value);
        return this;
    }
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public Map<String, Object> getExtend() {
        return extend;
    }
    public void setExtend(Map<String, Object> extend) {
        this.extend = extend;
    }
    @Override
    public String toString() {
        return "Msg{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", extend=" + extend +
                '}';
    }
}

User類

package cn.xdedm.entity;
import lombok.Data;
@Data
public class User {
    private Integer id ;
    private String  nickName ;
    private String  account ;
    private String  password ;
    private String  createTime;
    private Integer vaildFlag ;
}

controller類

   package cn.xdedm.controller;
import cn.xdedm.entity.Msg;
import cn.xdedm.entity.User;
import cn.xdedm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/query")
    public Msg QueryUser(@RequestBody User user){
        Msg msg=userService.QueryUser(user);
        return msg;
    }
}

service類

package cn.xdedm.service;
import cn.xdedm.dao.UserMapper;
import cn.xdedm.entity.Msg;
import cn.xdedm.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public Msg QueryUser(User user) {
        User user_db=userMapper.QueryUser(user);
        return Msg.Success().Add("user",user_db);
    }
}

dao類

package cn.xdedm.dao;
import cn.xdedm.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
@Component
@Mapper
public interface UserMapper {
    User QueryUser(User user);
}

maper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.xdedm.dao.UserMapper">
    <select id="QueryUser" parameterType="cn.xdedm.entity.User">
        select * from passwordsystem_user where 1=1
        <if test="id !=null and id !='' or id == 0">
            and id=#{id}
        </if>
        <if test="nickName !=null and nickName !='' or nickName == 0">
            and nick_name=#{nickName}
        </if>
        <if test="vaildFlag !=null and vaildFlag !='' or vaildFlag == 0">
            and vaild_flag=#{vaildFlag}
        </if>
        <if test="account !=null and account !='' or account== 0">
            and account=#{account}
        </if>
    </select>
</mapper>

在springboot主運(yùn)行程序加上MapperScan注解

package cn.xdedm;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("cn.xdedm.dao")
public class PasswordsystemApplication {
    public static void main(String[] args) {
        SpringApplication.run(PasswordsystemApplication.class, args);
    }
}

配置完成在postman上測試

到此這篇關(guān)于Springboot整合Mybatis和SQLite的文章就介紹到這了,更多相關(guān)Springboot整合Mybatis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 深入理解Java抽象類

    深入理解Java抽象類

    這篇文章主要介紹了Java抽象類的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下
    2020-08-08
  • 詳解如何在Elasticsearch中搜索空值

    詳解如何在Elasticsearch中搜索空值

    這篇文章主要為大家介紹了如何在Elasticsearch中搜索空值的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • java中Map遍歷的四種方式總結(jié)

    java中Map遍歷的四種方式總結(jié)

    Map集合遍歷日常開發(fā)最常使用,下面這篇文章主要給大家總結(jié)介紹了關(guān)于java中Map遍歷的四種方式,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-03-03
  • Java?輪詢鎖使用時(shí)遇到問題解決方案

    Java?輪詢鎖使用時(shí)遇到問題解決方案

    這篇文章主要介紹了Java?輪詢鎖使用時(shí)遇到問題解決方案,當(dāng)我們遇到死鎖之后,除了可以手動(dòng)重啟程序解決之外,還可以考慮使用順序鎖和輪詢鎖,但是過程也會(huì)遇到一些問題,接下來我們一起進(jìn)入下面文章了解解決方案,需要的小伙伴可以參考一下
    2022-05-05
  • Java中Jackson的序列化與反序列化詳解

    Java中Jackson的序列化與反序列化詳解

    這篇文章主要介紹了Java中Jackson的序列化與反序列化詳解,Jackson被認(rèn)為是"Java JSON庫"或"Java最好的JSON解析器",Jackson 還是一套用于 Java(和 JVM 平臺(tái))的數(shù)據(jù)處理工具,需要的朋友可以參考下
    2024-01-01
  • Java并發(fā)之BlockingQueue的使用

    Java并發(fā)之BlockingQueue的使用

    這篇文章主要介紹了Java并發(fā)之BlockingQueue的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-06-06
  • Java日期相關(guān)API的基本操作總結(jié)

    Java日期相關(guān)API的基本操作總結(jié)

    大概總結(jié)一下日期相關(guān)API操作原因是對(duì)于日期的操作我們開發(fā)中還是很常見的,包括在數(shù)據(jù)庫中保存日期,以及之前String類中對(duì)字符串的一些處理開發(fā)中都很常見,希望對(duì)大家有所幫助
    2022-11-11
  • springboot中使用groovy的示例代碼

    springboot中使用groovy的示例代碼

    Groovy就是一種繼承了動(dòng)態(tài)語言的優(yōu)良特性并運(yùn)行在JVM上的編程語言,Groovy支持動(dòng)態(tài)輸入,閉包,元編程,運(yùn)算符重載等等語法,這篇文章主要介紹了springboot中使用groovy的相關(guān)知識(shí),需要的朋友可以參考下
    2022-09-09
  • java構(gòu)造函數(shù)的三種類型總結(jié)

    java構(gòu)造函數(shù)的三種類型總結(jié)

    在本篇文章里小編給大家整理了一篇關(guān)于java構(gòu)造函數(shù)的三種類型總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。
    2021-01-01
  • java算法題解??虰M99順時(shí)針旋轉(zhuǎn)矩陣示例

    java算法題解??虰M99順時(shí)針旋轉(zhuǎn)矩陣示例

    這篇文章主要為大家介紹了java算法題解??虰M99順時(shí)針旋轉(zhuǎn)矩陣示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01

最新評(píng)論