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>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.28.0</version>
</dependency>
<!-- druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.14</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
</plugin>
</plugins>
</build>
</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”/>
<!-- <classPathEntry-->
<!-- location="/Users/jason/.m2/repository/com/h2database/h2/1.4.200/h2-1.4.200.jar"/>-->
<context id="default" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="org.sqlite.JDBC"-->
<!--connectionURL="jdbc:sqlite:./sqlite_db.db"-->
<!--userId=""-->
<!--password=""/>
<!--<jdbcConnection driverClass="org.h2.Driver"
connectionURL="jdbc:h2:file:/Users/jason/Desktop/DemoWorkspace/sqlitetest/zdl"
userId="sa"
password=""/>-->
<!--Java Entity生成器 -->
<javaModelGenerator targetPackage="com.zdltech.test.sqlitetest.entity"
targetProject="./src/main/java">
<!-- TODO enableSubPackages:是否让schema作为包的后缀-->
<property name="enableSubPackages" value="false"/>
<!-- 从数据库返回的值被清理前后的空格-->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--map xml生成器 -->
<sqlMapGenerator targetPackage="mapper"
targetProject="./src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- dao生成器-->
<javaClientGenerator targetPackage="com.zdltech.test.sqlitetest.dao"
targetProject="./src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 数据表与Bean的映射 -->
<table tableName="tb_user" domainObjectName="UserEntityMap" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" >
<!-- 如果设置为true,生成的model类会直接使用column本身的名字,而不会再使用驼峰命名方法,比如BORN_DATE,生成的属性名字就是BORN_DATE,而不会是bornDate -->
<property name="useActualColumnNames" value="true"/>
</table>
</context>
</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";
}
}
`
💬 评论