先说点什么
mybatis-plus是一款增强版的mybatis,功能强大,可以很大程度的简化开发。
然而达梦数据库比较小众,虽然官方说mybatis-plus支持达梦数据库,但是使用起来遇到了很多问题。
这篇文章主要讲如何使用mybatis-plus访问达梦数据库,并使用逆向工程自动生成代码。
=。=对了 这是个使用spring boot的项目。
(配置)pom文件,引入所需要的依赖
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-starter-jdbc
org.projectlombok
lombok
true
io.springfox
springfox-swagger2
2.7.0
io.springfox
springfox-swagger-ui
2.7.0
com.baomidou
mybatis-plus-boot-starter
3.2.0
com.baomidou
mybatis-plus-generator
3.2.0
org.freemarker
freemarker
2.3.29
junit
junit
org.springframework
spring-test
5.2.0.release
compile
org.springframework.boot
spring-boot-test
(配置)达梦的驱动包,配置数据源
在达梦数据库的安装目录下有驱动包,我们先把jar包丢进来,放到lib这个文件夹下:
然后配置pom文件:
com.dm
dm7jdbcdriver
1.7
system
${project.basedir}/src/lib/dm7jdbcdriver18.jar
到现在,所有需要的依赖就都已经导入了。
(配置)application.properties文件
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=dm.jdbc.driver.dmdriver
mybatis-plus.configuration.cache-enabled=true
mybatis-plus.mapper-locations=classpath*:mappers/*.xml
mybatis-plus.type-aliases-package=com.example.demo.extity.*
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.auto-mapping-behavior=full
mybatis-plus.global-config.banner=false
创建mybatis-plus自动生成代码的配置类
在配置数据源的时候要注意,如果不设置数据源的类型是达梦数据库,会无法识别。
经历了开心的看源码环节,我们发现mybatis中有个枚举类dbtype来标识数据库的类型,其中有达梦数据库的类型。(=。=竟然有)
所以我们在配置类里传个参数就好了。(=。= 不然可能就凉了)
dsc.setdbtype(dbtype.dm);
配置类代码:
public class mysqlgenerator {
public static string scanner(string tip) {
scanner scanner = new scanner(system.in);
stringbuilder help = new stringbuilder();
help.append("请输入" + tip + ":");
system.out.println(help.tostring());
if (scanner.hasnext()) {
string ipt = scanner.next();
if (stringutils.isnotempty(ipt)) {
return ipt;
}
}
throw new mybatisplusexception("请输入正确的" + tip + "!");
}
public static void main(string[] args) {
// 代码生成器
autogenerator mpg = new autogenerator();
// 全局配置
globalconfig gc = new globalconfig();
string projectpath = system.getproperty("user.dir");
gc.setoutputdir(projectpath + "/src/main/java");
gc.setauthor("kning");
gc.setopen(false);
gc.setswagger2(true);
gc.setidtype(idtype.auto);
gc.setbaseresultmap(true);
mpg.setglobalconfig(gc);
//达梦数据库的配置
datasourceconfig dsc = new datasourceconfig();
dsc.setdbtype(dbtype.dm);
dsc.setschemaname("sysdba");
dsc.seturl("");
dsc.setdrivername("dm.jdbc.driver.dmdriver");
dsc.setusername("");
dsc.setpassword("");
mpg.setdatasource(dsc);
// 包配置
packageconfig pc = new packageconfig();
pc.setmodulename(scanner("模块名"));
pc.setparent("com.example");
mpg.setpackageinfo(pc);
// 自定义配置
injectionconfig cfg = new injectionconfig() {
@override
public void initmap() {
// to do nothing
}
};
list foclist = new arraylist<>();
foclist.add(new fileoutconfig("/templates/mapper.xml.ftl") {
@override
public string outputfile(tableinfo tableinfo) {
// 自定义输入文件名称
return projectpath + "/src/main/resources/mapper/" + pc.getmodulename()
+ "/" + tableinfo.getentityname() + "mapper" + stringpool.dot_xml;
}
});
cfg.setfileoutconfiglist(foclist);
mpg.setcfg(cfg);
mpg.settemplate(new templateconfig().setxml(null));
// 策略配置
strategyconfig strategy = new strategyconfig();
strategy.setnaming(namingstrategy.underline_to_camel);
strategy.setcolumnnaming(namingstrategy.underline_to_camel);
strategy.setentitylombokmodel(true);
strategy.setinclude(scanner("表名"));
strategy.setsuperentitycolumns("id");
strategy.setcontrollermappinghyphenstyle(true);
strategy.settableprefix(pc.getmodulename() + "_");
strategy.setentitylombokmodel(true);
mpg.setstrategy(strategy);
// 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有!
mpg.settemplateengine(new freemarkertemplateengine());
mpg.execute();
}
}
mybatis-plus生成代码
首先我们在数据库里创建一张表,就叫教师(teacher)表好了。达梦数据库似乎要求表名尽量是大写的。
然后插入一点数据方便测试。
运行测试类,输入模块名和表名,达梦数据库要求表名是大写的。
不出意外,我们就生成成功了。=。=不要问为什么有个students,因为自己测试用的这个。
测试一下
我们来简单写个测试类,其功能是查出教师表的所有数据,然后插入一条教师信息,然后在查一次:
@runwith(springjunit4classrunner.class)
@springboottest
public class teachertest {
@autowired
private teachermapper teachermapper;
@test
public void teacher(){
list teachers = teachermapper.selectlist(null);
teachers.foreach(system.out::println);
system.out.println("==================================");
teacher teacher = new teacher();
teacher.setid(6);
teacher.setname("zhou");
teacher.setage(58);
teachermapper.insert(teacher);
teachers = teachermapper.selectlist(null);
teachers.foreach(system.out::println);
}
}
=。=看样子我们成功了。
到这里,我们就成功的使用mybatis-plus成功的生成了代码。
mybatis-plus主键生成可能出现的问题
让我们来看看错误信息:
org.springframework.dao.dataintegrityviolationexception:
### error updating database. cause: java.sql.sqlexception: 违反列[id]非空约束
### the error may exist in com/example/demo/mapper/teachermapper.java (best guess)
### the error may involve com.example.demo.mapper.teachermapper.insert-inline
### the error occurred while setting parameters
### sql: insert into teacher ( name, age ) values ( ?, ? )
### cause: java.sql.sqlexception: 违反列[id]非空约束
很显然,是主键插入时的问题。
我们看一下mybatis-plus生成的实体类。
@tableid(value = "id", type = idtype.auto)
private integer id;
其中id的属性设置为自动,然而如果达梦数据库建表的时候如果没有设置主键为自增。=。= 那没准就凉了。
我们看一下mybatis-plus支持哪些属性:
@getter
public enum idtype {
/**
* 数据库id自增
*/
auto(0),
/**
* 该类型为未设置主键类型(将跟随全局)
*/
none(1),
/**
* 用户输入id
*
该类型可以通过自己注册自动填充插件进行填充
*/
input(2),
/* 以下3种类型、只有当插入对象id 为空,才自动填充。 */
/**
* 全局唯一id (idworker)
*/
id_worker(3),
/**
* 全局唯一id (uuid)
*/
uuid(4),
/**
* 字符串全局唯一id (idworker 的字符串表示)
*/
id_worker_str(5);
private final int key;
idtype(int key) {
this.key = key;
}
}
可以看出,解决这个问题最简单的方法就是,修改idtype,使用none,自己传入id(主键)。
同样的,在自动生成代码的阶段,我们曾经设置过
gc.setidtype(idtype.auto);
在这里更改可以直接更改自动生成的代码,甚至也可以选择uuid等方式。
当然,这样并不好,所以也可以修改数据库表,设置id为自增。
在刚刚的teacher表中执行下面这条语句,就可以修改主键id的属性为自增了。
alter table teacher add id identity (1,1);
然后在运行代码,多半就成了。
到此这篇关于mybatis-plus+达梦数据库实现自动生成代码的示例的文章就介绍到这了,更多相关mybatis-plus 自动生成代码内容请搜索萬仟网以前的文章或继续浏览下面的相关文章希望大家以后多多支持萬仟网!
希望与广大网友互动??
点此进行留言吧!