zl程序教程

您现在的位置是:首页 >  后端

当前栏目

springboot + mybatis +easyUI整合案例

2023-09-11 14:18:03 时间

概述

springboot推荐使用的是JPA,但是因为JPA比较复杂,如果业务场景复杂,例如企业应用中的统计等需求,使用JPA不如mybatis理想,原始sql调优会比较简单方便,所以我们的项目中还是使用的mysql,并未使用JPA,在摸索过程中发现了几个mysql的坑,现在整理出来供大家借鉴

详细

一、准备工作

本例子需要使用mysql,运行之前请确保已经安装mysql,如果未安装请自行下载,或者再百度网盘中下载,下载地址:https://pan.baidu.com/s/1pLDKOm3#list/path=%2F

本例子整合了springboot、mysql、easyUI,并提供了一个简单的easyUI datagrid列表样例

运行本例子前请先执行create.sql创建数据库(下载包里面有)

二、程序实现

1、源码截图

blob.png

blob.png

2、具体设计到哪些代码

① MyBatisConfig类配置了mybatis的事务、sqlSessionFactory、mapper、model;MyBatisMapperScannerConfig配置了mybatis的model,MybatisCustomVFS是自定义的VFS,springboot项目打包之后class文件全都在BOOT-INF/classes/目录下,所以model会因为路径问题扫描不到,在VFS中处理该问题,WebConfig中配置了CharacterEncodingFilter,防止出现乱码

① plugins目录下的jQuery和easyUI插件未做任何改变

3、配置文件说明

spring:
  application:
    name: springtest
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource #使用druid连接池
    url: jdbc:mysql://localhost:3306/role?characterEncoding=UTF-8
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    max-idle: 5
    max-wait: 60000
    maxWait: 60000
    min-idle: 5
    minIdle: 5
    maxActive: 20
    initial-size: 5
    initialSize: 5
    validation-query: SELECT 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    filters: stat,wall,log4j
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    useGlobalDataSourceStat: true
    jdbc-interceptors: ConnectionState;SlowQueryReport(threshold=50)
  

thymeleaf: 
    cache: false
    cache-period: 0
template: 
    cache: false

server:
  port: 8080
  tomcat:
    uri-encoding: UTF-8

三、运行效果

1、直接执行com.springtest.Application即可运行(含有main方法,直接运行即可),访问地址是http://localhost:8080/

2、运行时的截图

blob.png

四、其他补充

例子中只是简单的对springboot 、mybatis 、 easyUI做了整合,mybatis未加入分页插件,如果需要请自行加入,和标准的Java项目使用方式无任何区别,easyUI和在标准的Java项目中使用方式也没区别

mybatis文档:http://www.mybatis.org/mybatis-3/zh/index.html

分页插件:https://github.com/pagehelper/Mybatis-PageHelper

 

 

注:本文著作权归作者,由demo大师发表,拒绝转载,转载需要作者授权