SpringBoot系列教材 (十八)- CRUD+分页 - SPRINGBOOT 基于SQLITE 和JPA 方式

 

步骤1:SQLite 介绍
步骤2:先运行,看到效果,再学习
步骤3:模仿和排错
步骤4:基于前面的知识点
步骤5:SQLite 方言一堆
步骤6:application.properties
步骤7:pom.xml
步骤8:重启运行

步骤 1 : SQLite 介绍

SQLite 是一种数据库,它是跑在 JVM里面的,所以不需要像 mysql 那样得独立安装配置,而是直接拿来就用。。。
本知识点就会把对mysql 的依赖,建立在 sqlite 上,这样大家跑起来就不用费神地安装配置 mysql 数据了啦

步骤 2 : 先运行,看到效果,再学习

老规矩,先下载下载区(点击进入)的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。 
如图所示,效果和使用 mysql 一样, CRUD和分页都有~
测试地址:

http://127.0.0.1:8080/listCategory


注: 启动方式是Springboot特有的,直接运行类:com.how2java.springboot.Application 的主方法。

先运行,看到效果,再学习

步骤 3 : 模仿和排错

在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。 
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。 
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。 

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。 
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来 
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程

步骤 4 : 基于前面的知识点

本知识点是建立在上一个知识点 Springboot使用JPA实现完整的增删改查 CRUD和分页 可运行项目的基础上进行的改进,所以最好把上个知识点理解和消化了.

步骤 5 : SQLite 方言一堆

因为是使用 JPA 来链接 SQlite, 而 JPA 默认用的是 Hibernate,所以要为 Hibernate 配置专门的方言。
方言是什么意思呢? 
为了更好地和四川人打交道,最好说四川方言。
为了更好地和浙江人打交道,最好说浙江方言。
为了更好地和 sqlite 打交道,最好说 sqlite 方言。
方言就是这么个意思啦,方便 Hibernate和 Sqlite打交道。 方言的英文是 Dialect, 所以就有了SQLiteDialect 这个类了。
除此之外,还需要其他两个类配合。

至于他们是怎么工作的。。。就不用关心啦,反正能用就行啦。。。

package com.how2java.sqlite;

  

import java.sql.SQLException;

import java.sql.Types;

 

import org.hibernate.JDBCException;

import org.hibernate.ScrollMode;

import org.hibernate.dialect.Dialect;

import org.hibernate.dialect.function.AbstractAnsiTrimEmulationFunction;

import org.hibernate.dialect.function.NoArgSQLFunction;

import org.hibernate.dialect.function.SQLFunction;

import org.hibernate.dialect.function.SQLFunctionTemplate;

import org.hibernate.dialect.function.StandardSQLFunction;

import org.hibernate.dialect.function.VarArgsSQLFunction;

import org.hibernate.dialect.identity.IdentityColumnSupport;

import org.hibernate.dialect.pagination.AbstractLimitHandler;

import org.hibernate.dialect.pagination.LimitHandler;

import org.hibernate.dialect.pagination.LimitHelper;

import org.hibernate.dialect.unique.DefaultUniqueDelegate;

import org.hibernate.dialect.unique.UniqueDelegate;

import org.hibernate.engine.spi.RowSelection;

import org.hibernate.exception.DataException;

import org.hibernate.exception.JDBCConnectionException;

import org.hibernate.exception.LockAcquisitionException;

import org.hibernate.exception.spi.SQLExceptionConversionDelegate;

import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtracter;

import org.hibernate.exception.spi.ViolatedConstraintNameExtracter;

import org.hibernate.internal.util.JdbcExceptionHelper;

import org.hibernate.mapping.Column;

import org.hibernate.type.StandardBasicTypes;

 

public class SQLiteDialect extends Dialect {

    private final UniqueDelegate uniqueDelegate;

  

    public SQLiteDialect() {

        registerColumnType(Types.BIT, "boolean");

        registerColumnType(Types.DECIMAL, "decimal");

        registerColumnType(Types.CHAR, "char");

        registerColumnType(Types.LONGVARCHAR, "longvarchar");

        registerColumnType(Types.TIMESTAMP, "datetime");

        registerColumnType(Types.BINARY, "blob");

        registerColumnType(Types.VARBINARY, "blob");

        registerColumnType(Types.LONGVARBINARY, "blob");

  

        registerFunction("concat"new VarArgsSQLFunction(StandardBasicTypes.STRING, """||"""));

        registerFunction("mod"new SQLFunctionTemplate(StandardBasicTypes.INTEGER, "?1 % ?2"));

        registerFunction("quote"new StandardSQLFunction("quote", StandardBasicTypes.STRING));

        registerFunction("random"new NoArgSQLFunction("random", StandardBasicTypes.INTEGER));

        registerFunction("round"new StandardSQLFunction("round"));

        registerFunction("substr"new StandardSQLFunction("substr", StandardBasicTypes.STRING));

        registerFunction("trim"new AbstractAnsiTrimEmulationFunction() {

            @Override

            protected SQLFunction resolveBothSpaceTrimFunction() {

                return new SQLFunctionTemplate(StandardBasicTypes.STRING, "trim(?1)");

            }

  

            @Override

            protected SQLFunction resolveBothSpaceTrimFromFunction() {

                return new SQLFunctionTemplate(StandardBasicTypes.STRING, "trim(?2)");

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值