007-SpringBoot+Mybatis+Sqlite框架搭建

1,配置文件(application.yaml)

server:
  port: 6695

spring:
  datasource:
    url: jdbc:sqlite:D:/examtolearn.db
    username:
    password:
    driver-class-name: org.sqlite.JDBC


mybatis:
  mapper-locations: classpath:mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true

2,依赖(pom.xml)

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <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.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.30.1</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>org.tzl</groupId>
            <artifactId>tzl-framework-web</artifactId>
            <version>0.0.1</version>
        </dependency>
    </dependencies>

注意:sqlite-jdbc的版本不对的话,会报 org.sqlite.Conn.isValid(I)Z 异常;

3,Mapper接口

@Mapper
public interface SubjectMapper {
    List<Subject> list();
}

注意:@Mapper注解一定要加上

4,Mapper.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.examtolearn.dao.SubjectMapper">
    <select id="list" resultType="org.examtolearn.entity.po.Subject">
        select * from subject
    </select>
</mapper>

注意:配置文件放到resources/mapper目录下

5,其它

其它就是控制器层、服务层的代码编写,然后直接通过如下代码,把Mapper接口注入,运行即可获取数据。

@Service
public class SubjectServiceImpl implements SubjectService {

    @Resource
    private SubjectMapper subjectMapper;

    @Override
    public List<Subject> list() {
        return subjectMapper.list();
    }
}

6,注意事项

6.1,mybatis在springboot和非springboot下使用的依赖不一样,写法也不一样,mybatis-spring-boot-starter下已经写好了配置类,简化了很多步骤,只需要把配置写好就可以了

 6.2,就像上面提到的sqlite版本一定要选对,具体怎么对应的没找到规律,但不对的情况下,会报 org.sqlite.Conn.isValid(I)Z 异常,很是无从下手;

posted on 2023-09-05 22:46  走调的钢琴  阅读(522)  评论(0编辑  收藏  举报