使用mybatis-generator插件结合tk.mybatis自动生成mapper
1.在pom.xml中引入mybatis-generator插件。注意,这里我使用的是tk.mybatis.所以需要引入相应的依赖。并添加mapper接口
tk.mybatis mapper-spring-boot-starter 2.0.2
package com.tk; import tk.mybatis.mapper.common.Mapper; import tk.mybatis.mapper.common.MySqlMapper; public interface MyMapperextends Mapper , MySqlMapper { }
configurationFile节点配置了生成mapper、entity所需的配置文件,所以我们需要在相应的位置添加gengeatorConfig.xml配置文件。
org.mybatis.generator mybatis-generator-maven-plugin 1.3.5 ${basedir}/src/main/resources/generatorConfig.xml true true mysql mysql-connector-java ${mysql.version} tk.mybatis mapper 3.4.4
2.添加generatorConfig.xml文件。文件位置要与pom.xml中配置的路径一致。关于该配置文件各个配置节点的含义和用途,文章末尾有详细解释。
配置文件一开始引入了jdbc.properties数据库连接配置文件,所以需要在resources文件夹下添加该配置文件。
<?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">
<jdbcConnection
driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.connectionURL}"
userId="${jdbc.username}"
password="${jdbc.password}">
<javaClientGenerator
targetPackage="com.senda.hello.spring.boot.mapper"
targetProject="src/main/java"
type="XMLMAPPER"/>
3.添加jdbc.properties数据库连接配置文件。需要注意的是mysql8.0之后,driverClass的包名有变化。
jdbc.driverClass=com.mysql.jdbc.Driver #8.0之后:jdbc.driverClass=com.mysql.cj.jdbc.Driver jdbc.connectionURL=jdbc:mysql://127.0.0.1:3306/dbtest?useUnicode=true&characterEncoding=utf-8&useSSL=false jdbc.username=root jdbc.password=root
4.经过上述步骤后,工程结构如下图所示。
5.双击mybatis-generator插件下的mybatis-generator:generate指令,开始自动构建。构建完成后,项目中就多了entity、dao、mapper。
6.现在简单查一查数据库,看这玩意儿好不好使。这里我使用的是阿里巴巴的的druid,所以需要引入相关依赖。
com.alibaba druid-spring-boot-starter 1.1.10 mysql mysql-connector-java
并在application.yml配置文件中添加相关的配置,包括druid和mybatis。
spring: datasource: druid: url: jdbc:mysql://127.0.0.1:3306/dbtest?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root initial-size: 1 min-idle: 1 max-active: 20 test-on-borrow: true driver-class-name: com.mysql.jdbc.Driver #MySQL 8.x: driver-class-name: com.mysql.cj.jdbc.Driver mybatis: type-aliases-package: com.senda.hello.spring.boot.mybatis.entity mapper-locations: classpath:mapper/*.xml
还需要在启动类加上MapperScan注解,指明Dao的位置。注意这里引入的是tk包下的MapperScan。
package com.senda.hello.spring.boot.mybatis; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import tk.mybatis.spring.annotation.MapperScan; @SpringBootApplication @MapperScan(basePackages = "com.senda.hello.spring.boot.mapper") public class HelloSpringBootMybatisApplication { public static void main(String[] args) { SpringApplication.run(HelloSpringBootMybatisApplication.class, args); } }
写个测试方法,玩一玩吧!注意要在SpringBootTest注解里注明测试类,为了加载配置文件。
package com.senda.hello.spring.boot.mybatis; import com.senda.hello.spring.boot.mapper.TbUserMapper; import com.senda.hello.spring.boot.entity.TbUser; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest(classes = HelloSpringBootMybatisApplication.class) public class HelloSpringBootMybatisApplicationTests { @Autowired private TbUserMapper userMapper; @Test public void testSelect() { ListuserList = userMapper.selectAll(); for (TbUser user : userList) System.out.println(user.getLoginname()); } }
发现SQL语句错了,数据库和表名字中间多了一个点。
org.springframework.jdbc.BadSqlGrammarException: ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.tb_user' at line 1 ### The error may exist in com/senda/hello/spring/boot/mapper/TbUserMapper.java (best guess) ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### SQL: SELECT uid,loginname,loginpass,email,status,activationCode FROM dbtest..tb_user ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.tb_user' at line 1 ; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.tb_user' at line 1
打开生成的TbUser实体类,我们惊讶的发现,table的名字竟然多了一个点。赶紧删除,再运行测试方法。
7.问题分析
首先,我们需要知道,database、schema、catalog三者的联系与区别。
按照SQL标准的解释,在SQL环境下Catalog和Schema都属于抽象概念,可以把它们理解为一个容器或者数据库对象命名空间中的一个层次,主要 用来解决命名冲突问题。从概念上说,一个数据库系统包含多个Catalog,每个Catalog又包含多个Schema,而每个Schema又包含多个数 据库对象(表、视图、字段等),反过来讲一个数据库对象必然属于一个Schema,而该Schema又必然属于一个Catalog,这样我们就可以得到该数据库对象的完全限定名称从而解决命名冲突的问题了;例如数据库对象表的完全限定名称就可以表示为:Catalog名称.Schema名称.表名称。这里 还有一点需要注意的是,SQL标准并不要求每个数据库对象的完全限定名称是唯一的,就象域名一样,如果喜欢的话,每个IP地址都可以拥有多个域名。
CREATE DATABASE creates a database with the given name. To use this statement, you need the CREATE privilege for the database. CREATE SCHEMA is a synonym for CREATE DATABASE.[译]CREATE DATABASE根据给定的名称创建数据库。你需要拥有数据库的CREATE权限来使用这个语句。CREATE SCHEMA是CREATE DATABASE的一个代名词。
也就是说,在MySQL中,schema和database是同一个玩意儿。而MySQL又不支持catalog,所以,我们平常看到的MySQL数据库对象表的完全限定名称是:database.table.
下面给出常用数据库对catalog、schema的支持情况:
|
供应商 |
Catalog支持 |
Schema支持 |
|
Oracle |
不支持 |
Oracle User ID |
|
MySQL |
不支持 |
数据库名 |
|
MS SQL Server |
数据库名 |
对象属主名,2005版开始有变 |
|
DB2 |
指定数据库对象时,Catalog部分省略 |
Catalog属主名 |
|
Sybase |
数据库名 |
数据库属主名 |
|
Informix |
不支持 |
不需要 |
|
PointBase |
不支持 |
数据库名 |
好了,回归我们的问题。有了上述背景知识,我们就可以快速定位generatorConfig.xml文件配置错误的地方了。在table节点,我们配置了catalog属性为database名称。现在一眼就知道配置错了,无中生有,删掉该属性。
为了保险起见,我们还要在jdbcConnection节点中添加属性,申明当前数据库不支持catalog。
现在让我们重新运行命令,生成Entity、Dao、mapper,生成成功后,看看TbUser的table注解,已经变成了表名字了。在运行下测试类,完美!!!还说啥,直接开花就完事儿。
8.附录
完整的generatorConfig.xml配置及属性注解。
DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
参考:https://www.cnblogs.com/little-rain/p/11063410.html