最近开发环境的不打印执行sql,但是我又不知道是哪个地方配置错误,因为不是自己搭的,所以打算自己重新搭建一下springBoot整合mybatis。顺便记录一下。
总共分为三个部分
idea 中选择 mybatis jdbc 2个 (下图的 mysql 可以不选)
注意:我这里的项目 都是在test中运行的,所以没选web模块,因为没必要
或者直接pom.xml中引入如下配置
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
.....
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-maven-pluginartifactId>
<configuration>
<verbose>trueverbose>
<overwrite>trueoverwrite>
configuration>
plugin>
plugins>
build>
新建application.yml,配置如下
spring:
messages:
basename: i18n/Messages,i18n/Pages
datasource:
type: com.alibaba.druid.pool.DruidDataSource #durid数据源
url: jdbc:mysql://localhost:3306/chat?serverTimezone=UTC # 数据库连接地址
username: root # 数据库用户名
password: # 数据库连接密码
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
type-aliases-package: com.demo.myspringbootmybatis.entity #模型所在的包名
mapper-locations: classpath*:/mybatis/mapper/*Mapper.xml #映射文件路径
configuration:
call-setters-on-nulls: true # 返回Map集合时,针对空值字段强行返回
新建generator.xml
<generatorConfiguration>
<classPathEntry
location="绝对路径/mysql-connector-java-8.0.12.jar"/>
<context id="DB2Tables" targetRuntime="Mybatis3">
<property name="autoDelimitKeywords" value="true"/>
<property name="javaFileEncoding" value="utf-8"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
<property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="addRemarkComments" value="true"/>
commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/chat?serverTimezone=UTC&nullCatalogMeansCurrent=true"
userId="root"
password="123456"/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
javaTypeResolver>
<javaModelGenerator
targetPackage="com.demo.myspringbootmybatis.entity"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
javaModelGenerator>
<sqlMapGenerator
targetPackage="mybatis/mapper"
targetProject=".\src\main\resources">
<property name="enableSubPackages" value="false"/>
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.demo.myspringbootmybatis.mapper"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="false"/>
javaClientGenerator>
<table tableName="MyUser" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="true"
enableSelectByPrimaryKey="true"
selectByExampleQueryId="false">
table>
context>
generatorConfiguration>
注意 以下配置自己修改:
1.JDBC驱动 自己替换
2.jdbc连接 数据库地址 密码
3.实体类生成的位置 *Mapper.xml 文件的位置 Mapper 接口文件的位置 自己替换
4.table标签改为自己的表名
这个是使用过程遇到的问题,他会导致生成的mapper文件,没有selectByPrimaryKey方法。
解决方法:generator.xml中修改链接mysql的url,添加nullCatalogMeansCurrent=true,如下
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/chat?serverTimezone=UTC&nullCatalogMeansCurrent=true"
userId="root"
password=""/>
@SpringBootTest(classes = MyspringbootmybatisApplication.class)
@WebAppConfiguration
class MyspringbootmybatisApplicationTests {
@Resource
private DataSource dataSource;
@Test
public void testConnection() throws Exception {
System.out.println(this.dataSource.getClass() );
System.out.println(this.dataSource );
System.out.println(this.dataSource.getConnection() );
}
@Resource
private MyuserMapper myuserMapper;
@Resource
private UserDao userDao;
@Test
public void testMapper() throws Exception {
//testMapper 运行成功说明 mybatis 和mybatis generator插件集成成功
System.out.println(myuserMapper.selectByPrimaryKey(1));
}
@Test
public void testDao() throws Exception {
//testDao 运行成功说明 mybatis 配置没问题
System.out.println(userDao.selectByPrimaryKey(1));
}
}
运行 testConnection 就成功就说明 连接是通的,如下:
class com.zaxxer.hikari.HikariDataSource
HikariDataSource (null)
2020-11-28 20:25:15.903 INFO 31412 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-11-28 20:25:16.206 INFO 31412 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
HikariProxyConnection@2098830440 wrapping com.mysql.cj.jdbc.ConnectionImpl@63411512
探究了一下打印可执行sql ,发现打印sql ,mybatis 的只能做到 打印时 带?,参数另行打印,要打印执行sql,只能通过
如下两种方式:
druid 是阿里的开源 连接池,他有很多其他功能,这里主要用他来打印可执行sql
首先引入包
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.10version>
dependency>
修改application.xml
spring:
messages:
basename: i18n/Messages,i18n/Pages
datasource:
#1. durid数据源
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/chat?serverTimezone=UTC # 数据库连接地址
username: root # 数据库用户名
password: 密码 # 数据库连接密码
driver-class-name: com.mysql.cj.jdbc.Driver
# 配置打印sql配置
druid:
filters: slf4j
filter:
slf4j:
enabled: true #开启日志
statement-log-enabled: false #这里不配置的话 sql会被重复打印
statement-executable-sql-log-enable: true #打印执行sql的
mybatis:
type-aliases-package: com.demo.myspringbootmybatis.entity #模型所在的包名
mapper-locations: classpath*:/mybatis/mapper/*Mapper.xml #映射文件路径
logging:
level:
druid:
sql:
Statement: debug # 这里也很重要 这里就是将sql的打印级别调整为debug
1.引入druid之后千万别自己写config类,自己在new 一个 druid数据源,这样会导致 yml中关于druid的配置完全不起作用,毕竟springBoot的配置加载优先级 代码里的是比配置文件的要高。
2.SpringBoot的默认日志框架是 logback,但是slf4j是日志统一接口,logback是slf4j的实现,所以druid的官方文档中没有logback的配置。只有slf4j的配置。 下面是官方文档中关于配置可执行sql的文档链接:
https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_LogFilter
测试:运行上面的testDao或testMapper输出:
2020-11-28 20:57:06.867 DEBUG 5944 --- [ main] druid.sql.Statement : {conn-10001, pstmt-20000} executed. select id, aid, tel, `name`, f
, createTime
from myuser
where id = 1
Myuser [Hash = 1594981181, id=1, aid=2, tel=null, name=233, f=null, createtime=null, serialVersionUID=1]
可以看到打印出来,可直接执行的sql
这个信息是来自mybatis-plus,在浏览其官方网站时发现的
与druid不同 只需要
1.引入jar包
<dependency>
<groupId>p6spygroupId>
<artifactId>p6spyartifactId>
<version>3.7.0version>
dependency>
2.修改application.yml
url 中间要加p6spy
driver要改成 com.p6spy.engine.spy.P6SpyDriver
spring:
messages:
basename: i18n/Messages,i18n/Pages
datasource:
# 1.url 中间要加p6spy
url: jdbc:p6spy:mysql://localhost:3306/chat?serverTimezone=UTC # 数据库连接地址
username: root # 数据库用户名
password: # 数据库连接密码
#2.driver要改成 com.p6spy.engine.spy.P6SpyDriver
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
mybatis:
type-aliases-package: com.demo.myspringbootmybatis.entity #模型所在的包名
mapper-locations: classpath*:/mybatis/mapper/*Mapper.xml #映射文件路径
3.resource下添加spy.properties
appender=com.p6spy.engine.spy.appender.StdoutLogger
4.测试
运行testDao可以看到也同样输出了可执行sql
1606570278590|13|statement|connection 0|select id, user_name, password, age from user_t where id = ?|select id, user_name, password, age from user_t where id = 1
User{id=1, userName='b', password='b,b', age=101}
项目地址:
https://gitee.com/LylYorick/myspringbootmybatis
参考资料:
https://www.cnblogs.com/hankuikui/p/10150205.html
https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_LogFilter