使用Mybatis的开发者,大多数都会遇到一个问题,就是要写大量的SQL在xml文件中,除了特殊的业务逻辑SQL之外,还有大量结构类似的增删改查SQL。而且,当数据库表结构改动时,对应的所有SQL以及实体类都需要更改。这工作量和效率的影响或许就是区别增删改查程序员和真正程序员的屏障。这时,通用Mapper便应运而生……
View DetailsUnsigned Fields MySql supports both signed, and unsigned, numeric fields. These are not JDBC types, so MyBatis generator cannot automatically account for these types of fields. The Java data types are always signed. This can lead to a loss of precision when using unsigned fields. The solution is to provide a <columnOverride> for any unsigned numeric field in MySql. Here is an example of how to deal with an unsigned bigint field in MySql:
1 2 3 |
<table tableName="ALLTYPES" > <columnOverride column="UNSIGNED_BIGINT_FIELD" javaType="java.lang.Object" jdbcType="LONG" /> </table> |
You will have to cast the returned value to the appropriate type yourself (in this […]
View Details错误日志:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Exception in thread "main" java.lang.NoClassDefFoundError: org/mybatis/generator/api/dom/xml/Element at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:348) at org.mybatis.generator.internal.ObjectFactory.internalClassForName(ObjectFactory.java:144) at org.mybatis.generator.internal.ObjectFactory.createInternalObject(ObjectFactory.java:180) at org.mybatis.generator.internal.ObjectFactory.createCommentGenerator(ObjectFactory.java:241) at org.mybatis.generator.config.Context.getCommentGenerator(Context.java:266) at org.mybatis.generator.codegen.mybatis3.model.SimpleModelGenerator.getCompilationUnits(SimpleModelGenerator.java:51) at org.mybatis.generator.codegen.mybatis3.IntrospectedTableMyBatis3Impl.getGeneratedJavaFiles(IntrospectedTableMyBatis3Impl.java:181) at org.mybatis.generator.config.Context.generateFiles(Context.java:460) at org.mybatis.generator.api.MyBatisGenerator.generate(MyBatisGenerator.java:262) at org.mybatis.generator.api.MyBatisGenerator.generate(MyBatisGenerator.java:132) at com.weChat.MybatisStartup.GeneratorDisplay.generator(GeneratorDisplay.java:33) at com.weChat.MybatisStartup.GeneratorDisplay.main(GeneratorDisplay.java:17) Caused by: java.lang.ClassNotFoundException: org.mybatis.generator.api.dom.xml.Element at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 13 more |
原因及其解决办法: 错误日志中很明显的说是 mybatis 的代码自动生成依赖 generator 的 api 存在问题,查看 pom.xml 文件的依赖,发现引用是正常的,也就是说引用和使用都不可能有问题,因为我并没有直接操作 generator 的 api ,那么只可能是版本的问题了,而我的这个依赖的版本是 1.4.0 ,是目前最高的版本: 所以只有可能是我的版本太高了,下降一个版本试试,换成1.3.7的果然就好了!看来做这个不能写的太满,不然很容易给自己埋雷啊! from:https://www.cnblogs.com/90s-ITBoy/p/13388452.html
View Details在 Intellij IDEA 中结合 Gradle 使用 MyBatis Generator 逆向生成代码 Info: JDK 1.8 Gradle 2.14 Intellij IDEA 2016.3 前言 由于 IDEA 的教程较少,且 MyBatis Generator 不支持 Gradle 直接运行,因此这次是在自己折腾项目过程中,根据一些参考资料加上自己的实践得出的结论,并附上相应的 Demo 可供自己未来参考,也与大家分享。 本文的 Demo 也可以当作工具直接导入 IDEA,加上自己的数据库信息即可生成代码。 创建项目 详细的创建步骤可以参考使用 Gradle 构建 Struts 2 Web 应用中「新建 Gradle Web 项目」一节即可。当创建完毕,需要等待 Gradle 联网构建,由于国内网络因素,可能需要稍作等待。当构建完成,目录结构应如下图一致: Gradle 构建完成 配置依赖 这里需要使用 MyBatis Generator,MySQL 驱动,以及 MyBatis Mapper。由于代码生成单独运行即可,不需要参与到整个项目的编译,因此在 build.gradle 中添加配置:
1 2 3 |
configurations { mybatisGenerator } |
在 Maven Repository (http://mvnrepository.com/) 分别搜索依赖: org.mybatis.generator mysql-connector-java tk.mybatis:mapper 依赖的版本并不是局限于某个特定版本,可以选择适时相应的更新版本。添加到 build.gradle 的 dependencies 中(注意前面需将「compile group:」改为 「mybatisGenerator」):
1 2 3 4 5 6 7 |
dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.5' mybatisGenerator 'mysql:mysql-connector-java:5.1.40' mybatisGenerator 'tk.mybatis:mapper:3.3.9' } |
设置数据库信息 在 resources 下,新建 mybatis 文件夹,并新建 config.properties 和 generatorConfig.xml,文件结构如下: 配置文件目录 在 config.properties 中配置数据库和要生成的 […]
View Details今天写东西测试的时候发现一个问题,如下: application.yml中数据源是这样配置的: 第一反应就是记忆中连接mysql的驱动不都是com.mysql.jdbc.Driver吗?com.mysql.cj.jdbc.Driver是什么鬼? 后来查看了一下才知道 这个跟驱动的依赖版本有关
1 2 3 4 5 |
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> |
com.mysql.cj.jdbc.Driver是mysql-connector-java 6 中的特性,相比mysql-connector-java 5 多了一个时区:serverTimezone,把数据源配置的驱动改一下就好了 这样启动就不会再报:Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. 使用UTC会有8小时的时差(中国快全球标准8小时,比如:全球标准当前是上午1点,中国时间则为上午9点),可设置为北京时间东八区GMT%2B8 或者上海时间Asia/Shanghai。 总结: com.mysql.jdbc.Driver和mysql-connector-java 5一起用。 com.mysql.cj.jdbc.Driver和mysql-connector-java 6 一起用。 from:https://blog.csdn.net/weixin_43770545/article/details/90486809
View Details1、在win10桌面编写test.gradle脚本,里面内容如下
1 2 3 |
task helloword << { println 'Hello gradle qick start' } |
2、在doc命令窗口执行脚本
1 |
gradle -q helloword |
结果如下: 3、解决办法 查看为gradle 5.0中 << 已经过时了,即(<<),是推荐使用doLast 来替代的,可以通过降低gradle版本(比如4.10.1)或者使用doLast解决 重新修改脚本如下,执行后正常输出’Hello gradle qick start
1 2 3 |
task helloword { println 'Hello gradle qick start' } |
或者
1 2 3 4 5 |
task helloword { doLast{ println 'Hello gradle qick start' } } |
人要耐得住寂寞,才能守得住繁华。人生最痛苦的就是拿不起放不下,不属于自己的快乐,及时放手也许是一种解脱,生活中没有谁对谁错,只有适不适合。当发现很多已经改变,更要面对的是事实。 from:https://www.cnblogs.com/yuanchaoyong/p/11421595.html
View DetailsMybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。 1、相关文件 关于Mybatis-Generator的下载可以到这个地址:https://github.com/mybatis/generator/releases 由于我使用的是Mysql数据库,这里需要在准备一个连接mysql数据库的驱动jar包 以下是相关文件截图: 和Hibernate逆向生成一样,这里也需要一个配置文件: generatorConfig.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?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"> <generatorConfiguration> <!--数据库驱动--> <classPathEntry location="mysql-connector-java-5.0.8-bin.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库链接地址账号密码--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mymessages" userId="root" password="root"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成Model类存放位置--> <javaModelGenerator targetPackage="lcw.model" targetProject="src"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成映射文件存放位置--> <sqlMapGenerator targetPackage="lcw.mapping" targetProject="src"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--生成Dao类存放位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="src"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--生成对应表及类名--> <table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration> |
需要修改文件配置的地方我都已经把注释标注出来了,这里的相关路径(如数据库驱动包,生成对应的相关文件位置可以自定义)不能带有中文。 上面配置文件中的:
1 |
<table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> |
tableName和domainObjectName为必选项,分别代表数据库表名和生成的实力类名,其余的可以自定义去选择(一般情况下均为false)。 生成语句文件:
1 |
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite |
2、使用方法 在该目录按住Shift键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成语句的文件代码即可。 看下效果图: 生成相关代码: Message.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package lcw.model; public class Messgae { private Integer id; private String title; private String describe; private String content; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title == null ? null : title.trim(); } public String getDescribe() { return describe; } public void setDescribe(String describe) { this.describe = describe == null ? null : describe.trim(); } public String getContent() { return content; } public void setContent(String content) { this.content = content == null ? null : content.trim(); } } |
MessgaeMapper.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="lcw.dao.MessgaeMapper" > <resultMap id="BaseResultMap" type="lcw.model.Messgae" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="title" property="title" jdbcType="VARCHAR" /> <result column="describe" property="describe" jdbcType="VARCHAR" /> <result column="content" property="content" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > id, title, describe, content </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from message where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from message where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="lcw.model.Messgae" > insert into message (id, title, describe, content) values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{describe,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR}) </insert> <insert id="insertSelective" parameterType="lcw.model.Messgae" > insert into message <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="title != null" > title, </if> <if test="describe != null" > describe, </if> <if test="content != null" > content, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=INTEGER}, </if> <if test="title != null" > #{title,jdbcType=VARCHAR}, </if> <if test="describe != null" > #{describe,jdbcType=VARCHAR}, </if> <if test="content != null" > #{content,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="lcw.model.Messgae" > update message <set > <if test="title != null" > title = #{title,jdbcType=VARCHAR}, </if> <if test="describe != null" > describe = #{describe,jdbcType=VARCHAR}, </if> <if test="content != null" > content = #{content,jdbcType=VARCHAR}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="lcw.model.Messgae" > update message set title = #{title,jdbcType=VARCHAR}, describe = #{describe,jdbcType=VARCHAR}, content = #{content,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update> </mapper> |
MessgaeMapper.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package lcw.dao; import lcw.model.Messgae; public interface MessgaeMapper { int deleteByPrimaryKey(Integer id); int insert(Messgae record); int insertSelective(Messgae record); Messgae selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(Messgae record); int updateByPrimaryKey(Messgae record); } |
from:https://www.cnblogs.com/zhujiabin/p/4990762.html
View Details这两天启动了一个新项目因为项目组成员一直都使用的是 Mybatis,虽然个人比较喜欢 Jpa 这种极简的模式,但是为了项目保持统一性技术选型还是定了 Mybatis 。到网上找了一下关于 Spring Boot 和 Mybatis 组合的相关资料,各种各样的形式都有,看的人心累,结合了 Mybatis 的官方 Demo 和文档终于找到了最简的两种模式,花了一天时间总结后分享出来。 Orm 框架的本质是简化编程中操作数据库的编码,发展到现在基本上就剩两家了,一个是宣称可以不用写一句 Sql 的 Hibernate,一个是可以灵活调试动态 Sql 的 Mybatis ,两者各有特点,在企业级系统开发中可以根据需求灵活使用。发现一个有趣的现象:传统企业大都喜欢使用 Hibernate ,互联网行业通常使用 Mybatis 。 Hibernate 特点就是所有的 Sql 都用 Java 代码来生成,不用跳出程序去写(看) Sql ,有着编程的完整性,发展到最顶端就是 Spring Data Jpa 这种模式了,基本上根据方法名就可以生成对应的 Sql 了,有不太了解的可以看我的上篇文章Spring Boot(五): Spring Data Jpa 的使用。 Mybatis 初期使用比较麻烦,需要各种配置文件、实体类、Dao 层映射关联、还有一大推其它配置。当然 Mybatis 也发现了这种弊端,初期开发了generator可以根据表结果自动生产实体类、配置文件和 Dao 层代码,可以减轻一部分开发量;后期也进行了大量的优化可以使用注解了,自动管理 Dao 层和配置文件等,发展到最顶端就是今天要讲的这种模式了,mybatis-spring-boot-starter 就是 Spring Boot+ Mybatis 可以完全注解不用配置文件,也可以简单配置轻松上手。 现在想想 Spring Boot 就是牛逼呀,任何东西只要关联到 Spring Boot 都是化繁为简。 mybatis-spring-boot-starter 官方说明:MyBatis Spring-Boot-Starter will help you use MyBatis with Spring Boot 其实就是 Mybatis 看 Spring Boot 这么火热也开发出一套解决方案来凑凑热闹,但这一凑确实解决了很多问题,使用起来确实顺畅了许多。mybatis-spring-boot-starter主要有两种解决方案,一种是使用注解解决一切问题,一种是简化后的老传统。 当然任何模式都需要首先引入mybatis-spring-boot-starter的 Pom 文件,现在最新版本是 2.0.0 […]
View Details配置gradle镜像 在 %USERPROFILE%\.gradle下面创建新文件init.gradle,输入下面的内容并保存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
allprojects{ repositories { def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public/' all { ArtifactRepository repo -> if(repo instanceof MavenArtifactRepository){ def url = repo.url.toString() if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')) { project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL." remove repo } } } maven { url REPOSITORY_URL } } } |
配置maven镜像 复制E:\maven\apache-maven-3.3.9\conf中的settings.xml到%USERPROFILE%\.m2中,修改mirrors即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </mirror> <mirror> <id>jcenter</id> <mirrorOf>central</mirrorOf> <name>jcenter.bintray.com</name> <url>http://jcenter.bintray.com/</url> </mirror> <mirror> <id>repo1</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://repo1.maven.org/maven2/</url> </mirror> <mirror> <id>repo2</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://repo2.maven.org/maven2/</url> </mirror> </mirrors> |
build.gradle 配置
1 2 3 4 5 |
repositories { mavenLocal() mavenCentral() maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} } |
作者:liurongming 链接:https://www.jianshu.com/p/67b547b4e6ae 来源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
View DetailsMariaDB 10.4.12 Stable# Row size too large (> 8126). Changing some columns to TEXT or BLOB may help.# 以下两步解决问题 1. 修改my.ini文件在[mysqld]下面加入下面三行#
1 2 3 |
innodb_file_per_table innodb_file_format = Barracuda innodb_strict_mode = 0 |
2. 新建一个查询进行如下操作将nombre_tabla改成你的表名#
1 2 3 |
ALTER TABLE nombre_tabla ENGINE=InnoDB ROW_FORMAT=COMPRESSED; |
from:https://www.cnblogs.com/fanlinglong/p/12425116.html
View Details