Spring boot+mybatis+Sqlite+mybatis-generator环境配置

有的时候 我们开发不需要一定要用到mysql、oracle等数据库,Sqlite也是我们的一种选择。

Sqlite使用场景

小型网站

SQLite适用于中小规模流量的网站.

日访问在10万以下的网站可以很好的支持,适用于读多写少的操作,如管理员在后台添加数据,其他访客多为浏览.

10万/天是一个临界值,事实上在100万的数据量之下,SQLite的表现还是可以的,在往上就不适合了.

使用它无需单独购买数据库服务,无需服务器进程,配置成本几乎为零,加上数据的导入导出都是复制文件,维护难度也几乎为零,迁移到别的服务器无需任何配置即可支持,加上其读取的速度非常快,省去了远程数据库的链接,能够极大提升网站访问速度.

嵌入式设备

SQLite适用于手机, PDA, 机顶盒, 以及其他嵌入式设备. 作为一个嵌入式数据库它也能够很好的应用于客户端程序.

因为其轻量,小巧,不怎么占用内存,数据的读写性能好,加上嵌入式设备数据量并不大,不需要频繁的维护,所以比较适合.

数据库教学

SQLite 支持 SQL92(SQL2)标准的大多数查询语言的功能。

其无配置,无依赖,小巧,单一文件的特性让它的安装和使用非常简单,非常适合用来讲解SQL语句.

学生可以在很短的时候使用并操作SQLite,不受系统和商业限制等影响,学习的结果可以通过邮件或者云文件等形式发送给老师进行评分.

可以通过它快速实现一个最小化应用,适合学生快速了解SQLite,以及SQL语法,从而实现数据库的触类旁通,了解其他数据库系统的设计实现原则.

本地应用程序

其单一磁盘文件的特性,并且不支持远程连接,使其适用于本地的应用程序,如PC客户端软件.

常用的应用类型为金融分析工具、CAD 包、档案管理程序等等. (手机上的通讯录也是用此开发的)

没有远程,意味着适用于内部或者临时的数据库,用来处理一些数据,让程序更加灵活.

不适用场景

很明显其适合小型网站,相对的就不适合高流量网站.,也不适合超大的数据集,在其缺点也提到,不适合高并发访问.

POM.xm配置

`<?xml version=“1.0” encoding=“UTF-8”?> <project xmlns=“http://maven.apache.org/POM/4.0.0" xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!– lookup parent from repository –> </parent> <groupId>com.zdltech.test</groupId> <artifactId>sqlitetest</artifactId> <version>0.0.1-SNAPSHOT</version> <name>sqlitetest</name> <description>Demo project for Spring Boot</description>

&lt;properties&gt;
    &lt;java.version&gt;1.8&lt;/java.version&gt;
&lt;/properties&gt;

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.mybatis.spring.boot&lt;/groupId&gt;
        &lt;artifactId&gt;mybatis-spring-boot-starter&lt;/artifactId&gt;
        &lt;version&gt;2.1.1&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
        &lt;scope&gt;test&lt;/scope&gt;
        &lt;exclusions&gt;
            &lt;exclusion&gt;
                &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
                &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
            &lt;/exclusion&gt;
        &lt;/exclusions&gt;
    &lt;/dependency&gt;
    &lt;!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.xerial&lt;/groupId&gt;
        &lt;artifactId&gt;sqlite-jdbc&lt;/artifactId&gt;
        &lt;version&gt;3.28.0&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;!-- druid--&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.alibaba&lt;/groupId&gt;
        &lt;artifactId&gt;druid&lt;/artifactId&gt;
        &lt;version&gt;1.1.14&lt;/version&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;

&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
        &lt;/plugin&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.mybatis.generator&lt;/groupId&gt;
            &lt;artifactId&gt;mybatis-generator-maven-plugin&lt;/artifactId&gt;
            &lt;version&gt;1.3.7&lt;/version&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;

</project> `


### application.properties配置 {#toc_7}

> ```
`server.port=19999
server.servlet.context-path=/sqlite

# thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5

# Sqlite数据库配置
spring.datasource.driver-class-name=org.sqlite.JDBC
spring.datasource.url=jdbc:sqlite:./sqlite_db.db
spring.datasource.username=
spring.datasource.password=

# H2数据库配置
# spring.datasource.driver-class-name=org.h2.Driver
#spring.datasource.url=jdbc:h2:file:/Users/jason/Desktop/DemoWorkspace/sqlitetest/zdl
#spring.datasource.username=sa
#spring.datasource.password=

spring.h2.console.path=/h2-consle
spring.h2.console.enabled=true
spring.h2.console.settings.web-allow-others=true


spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.tomcat.validation-query=select 1

spring.datasource.tomcat.initial-size=1
spring.datasource.tomcat.min-idle=3
spring.datasource.tomcat.max-active=20


# 配置mapper文件路径
mybatis.mapper-locations=classpath:mapper/**/*.xml
`

generatorConfig.xml配置

`<?xml version=“1.0” encoding=“UTF-8”?> <!DOCTYPE generatorConfiguration PUBLIC “-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN” “http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<!– classPath:数据库的JDBC驱动–> <generatorConfiguration> <classPathEntry location="/Users/jason/.m2/repository/org/xerial/sqlite-jdbc/3.28.0/sqlite-jdbc-3.28.0.jar”/>

&lt;!-- &lt;classPathEntry--&gt;
        &lt;!-- location="/Users/jason/.m2/repository/com/h2database/h2/1.4.200/h2-1.4.200.jar"/&gt;--&gt;

&lt;context id="default" targetRuntime="MyBatis3"&gt;

    &lt;commentGenerator&gt;

        &lt;property name="suppressDate" value="false"/&gt;
        &lt;property name="suppressAllComments" value="true"/&gt;
    &lt;/commentGenerator&gt;

   &lt;jdbcConnection driverClass="org.sqlite.JDBC"--&gt;
                    &lt;!--connectionURL="jdbc:sqlite:./sqlite_db.db"--&gt;
                    &lt;!--userId=""--&gt;
                    &lt;!--password=""/&gt;

     &lt;!--&lt;jdbcConnection driverClass="org.h2.Driver"
                    connectionURL="jdbc:h2:file:/Users/jason/Desktop/DemoWorkspace/sqlitetest/zdl"
                    userId="sa"
                    password=""/&gt;--&gt;
    &lt;!--Java Entity生成器 --&gt;
    &lt;javaModelGenerator targetPackage="com.zdltech.test.sqlitetest.entity"
                        targetProject="./src/main/java"&gt;
        &lt;!-- TODO enableSubPackages:是否让schema作为包的后缀--&gt;
        &lt;property name="enableSubPackages" value="false"/&gt;
        &lt;!-- 从数据库返回的值被清理前后的空格--&gt;
        &lt;property name="trimStrings" value="true"/&gt;
    &lt;/javaModelGenerator&gt;
    &lt;!--map xml生成器 --&gt;
    &lt;sqlMapGenerator targetPackage="mapper"
                     targetProject="./src/main/resources"&gt;
        &lt;property name="enableSubPackages" value="false"/&gt;
    &lt;/sqlMapGenerator&gt;
    &lt;!-- dao生成器--&gt;
    &lt;javaClientGenerator targetPackage="com.zdltech.test.sqlitetest.dao"
                         targetProject="./src/main/java" type="XMLMAPPER"&gt;
        &lt;property name="enableSubPackages" value="false"/&gt;
    &lt;/javaClientGenerator&gt;


    &lt;!-- 数据表与Bean的映射 --&gt;
    &lt;table tableName="tb_user"  domainObjectName="UserEntityMap" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
           enableSelectByExample="false" selectByExampleQueryId="false" &gt;
        &lt;!-- 如果设置为true,生成的model类会直接使用column本身的名字,而不会再使用驼峰命名方法,比如BORN_DATE,生成的属性名字就是BORN_DATE,而不会是bornDate --&gt;
        &lt;property name="useActualColumnNames" value="true"/&gt;
    &lt;/table&gt;

&lt;/context&gt;

</generatorConfiguration> `


### 测试Controller 是否正常连接数据库 {#toc_9}

> ```
`import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    DataSource dataSource;
    /**
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("index")
    public String index(){
        try {
            Connection conn = dataSource.getConnection();
            System.out.println("*************************");
            System.out.println(conn);
            DruidDataSource dss = (DruidDataSource) dataSource;
            System.out.println(dss.getName());
            System.out.println(dss.getValidationQuery());
            System.out.println(dss.getTimeBetweenEvictionRunsMillis());
            System.out.println(dss.getMinEvictableIdleTimeMillis());
            System.out.println(dataSource.getClass().getName());
            System.out.println(dss);
            System.out.println("**************************");
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return "index is run";
    }
}
`