java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Springboot整合Mybatis

Springboot整合Mybatis和SQLite的详细过程

作者:orzdh

这篇文章主要介绍了Springboot整合Mybatis和SQLite的详细过程,本文通过图文示例相结合给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧

项目目录

SQLite中的数据

maven的pom.xml导入所需要的依赖

 <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>

创建SQLite需要的文件

在resources目录下创建databases.db,如下图

配置yml文件

server:
  port: 8888
spring:
  mvc:
    static-path-pattern: classpath:/**
  # 配置 Oracle
  datasource:
    driver-class-name: org.sqlite.JDBC  #数据库链接驱动
    url: jdbc:sqlite::resource:databases/data.db #数据库链接地址
    username:
    password:
    type: com.alibaba.druid.pool.DruidDataSource
# mybatis配置
mybatis:
  mapper-locations: classpath:mapper/*.xml    # mapper映射文件位置
  type-aliases-package: cn.xdedm.entity    # 实体类所在的位置
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl   #用于控制台打印sql语句
    map-underscore-to-camel-case: true

Msg类

package cn.xdedm.entity;
import java.util.HashMap;
import java.util.Map;
public class Msg {
    //状态码 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主运行程序加上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上测试

到此这篇关于Springboot整合Mybatis和SQLite的文章就介绍到这了,更多相关Springboot整合Mybatis内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文