|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "springmvc整合mybatis之mybatis generator自动生成" |
| 4 | +date: 2020-11-08 08:41:26 +0800 |
| 5 | +tags: java |
| 6 | +description: |
| 7 | +--- |
| 8 | + |
| 9 | +当我们使用hibernate作为数据持久层的时候,对于实体类的生成在Idea中可以使用__Persistence__菜单进行实体类的自动生成(Persistence->Generate Persistence Mapping->By Database Schema) |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +当使用mybatis的时候,hibernate的Persistence菜单不能使用,但是mybatis提供了另一个非常好用的插件__mybatis-generator__,可以通过maven插件的方式,一键生成实体类、接口类、Xml映射等 |
| 14 | + |
| 15 | +## maven配置 |
| 16 | + |
| 17 | +jar包引入 |
| 18 | +{% highlight xml %} |
| 19 | +<dependency> |
| 20 | + <groupId>org.mybatis.generator</groupId> |
| 21 | + <artifactId>mybatis-generator-core</artifactId> |
| 22 | + <version>1.4.0</version> |
| 23 | +</dependency> |
| 24 | +<!-- mybatis通用Mapper类 --> |
| 25 | +<dependency> |
| 26 | + <groupId>tk.mybatis</groupId> |
| 27 | + <artifactId>mapper</artifactId> |
| 28 | + <version>4.1.5</version> |
| 29 | +</dependency> |
| 30 | +<!-- mybatis类 --> |
| 31 | +<dependency> |
| 32 | + <groupId>org.mybatis</groupId> |
| 33 | + <artifactId>mybatis</artifactId> |
| 34 | + <version>3.5.3</version> |
| 35 | +</dependency> |
| 36 | +<!-- mybatis+spring整合包 --> |
| 37 | +<dependency> |
| 38 | + <groupId>org.mybatis</groupId> |
| 39 | + <artifactId>mybatis-spring</artifactId> |
| 40 | + <version>2.0.5</version> |
| 41 | +</dependency> |
| 42 | +{% endhighlight %} |
| 43 | + |
| 44 | +配置maven插件 |
| 45 | +{% highlight xml %} |
| 46 | +<plugin> |
| 47 | + <groupId>org.mybatis.generator</groupId> |
| 48 | + <artifactId>mybatis-generator-maven-plugin</artifactId> |
| 49 | + <version>1.3.6</version> |
| 50 | + <configuration> |
| 51 | + <configurationFile> |
| 52 | + ${basedir}/src/main/resources/generator/generatorConfig.xml |
| 53 | + </configurationFile> |
| 54 | + <overwrite>true</overwrite> |
| 55 | + <verbose>true</verbose> |
| 56 | + </configuration> |
| 57 | + <dependencies> |
| 58 | + <dependency> |
| 59 | + <groupId>mysql</groupId> |
| 60 | + <artifactId>mysql-connector-java</artifactId> |
| 61 | + <version>${mysql.version}</version> |
| 62 | + <scope>runtime</scope> |
| 63 | + </dependency> |
| 64 | + <dependency> |
| 65 | + <groupId>tk.mybatis</groupId> |
| 66 | + <artifactId>mapper</artifactId> |
| 67 | + <version>4.1.5</version> |
| 68 | + </dependency> |
| 69 | + </dependencies> |
| 70 | +</plugin> |
| 71 | +{% endhighlight %} |
| 72 | + |
| 73 | +## 配置generator文件 |
| 74 | + |
| 75 | +在配置文件中,我们可以配置数据库链接、需要生成的类以及生成目录等信息,直接上配置吧,对应的配置说明都在文件注释中 |
| 76 | +{% highlight xml %} |
| 77 | +<?xml version="1.0" encoding="UTF-8"?> |
| 78 | +<!DOCTYPE generatorConfiguration |
| 79 | + PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" |
| 80 | + "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> |
| 81 | +<generatorConfiguration> |
| 82 | + <!-- 引入数据库配置文件 --> |
| 83 | + <properties resource="conf/jdbc.properties"></properties> |
| 84 | + <context id="mySqlContext" defaultModelType="flat"> |
| 85 | + <!-- 自动处理mysql中的关键字 --> |
| 86 | + <property name="autoDelimitKeywords" value="true"/> |
| 87 | + <!-- sql关键字中的分隔符 --> |
| 88 | + <property name="beginningDelimiter" value="`"/> |
| 89 | + <property name="endingDelimiter" value="`"/> |
| 90 | + <!-- 编码 --> |
| 91 | + <property name="javaFileEncoding" value="UTF-8"/> |
| 92 | + <!-- 继承mybatis通用类 --> |
| 93 | + <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> |
| 94 | + <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/> |
| 95 | + <property name="caseSensitive" value="true"/> |
| 96 | + <property name="forceAnnotation" value="true"/> |
| 97 | + <property name="beginningDelimiter" value="`"/> |
| 98 | + <property name="endingDelimiter" value="`"/> |
| 99 | + </plugin> |
| 100 | + <!-- 阻止自动生成时间戳和MBG注释,会影响版本控制 --> |
| 101 | + <commentGenerator> |
| 102 | + <property name="suppressDate" value="true"/> |
| 103 | + </commentGenerator> |
| 104 | + <!-- 数据库信息 --> |
| 105 | + <jdbcConnection |
| 106 | + driverClass="${jdbc.driver}" |
| 107 | + connectionURL="${jdbc.url}" |
| 108 | + userId="${jdbc.username}" |
| 109 | + password="${jdbc.password}" |
| 110 | + > |
| 111 | + <property name="nullCatalogMeansCurrent" value="true"/> |
| 112 | + </jdbcConnection> |
| 113 | + <!-- 生成实体类的位置 --> |
| 114 | + <javaModelGenerator targetPackage="com.bc.entity" targetProject="src/main/java"> |
| 115 | + <!-- 如果设置为true会在targetPackage的基础上,根据数据库的schema再生成一层package --> |
| 116 | + <property name="enableSubPackages" value="false"/> |
| 117 | + <!-- 在getter方法中,对String类型字段调用trim()方法 --> |
| 118 | + <property name="trimStrings" value="true"/> |
| 119 | + </javaModelGenerator> |
| 120 | + <!-- 如果用XML格式,该配置需要配置 --> |
| 121 | + <sqlMapGenerator targetPackage="mapper" targetProject="src\main\resources"> |
| 122 | + <property name="enableSubPackages" value="false"/> |
| 123 | + </sqlMapGenerator> |
| 124 | + <!-- |
| 125 | + mapper的生成形式 |
| 126 | + XMLMAPPER:XML形式,不需要mapper注解 |
| 127 | + --> |
| 128 | + <javaClientGenerator type="XMLMAPPER" targetPackage="com.bc.dao" targetProject="src/main/java"></javaClientGenerator> |
| 129 | + <!-- 配置要生成的表 --> |
| 130 | + <table tableName="bc_one" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> |
| 131 | + <!-- 设置mysql主键 --> |
| 132 | + <generatedKey column="id" sqlStatement="MySql"></generatedKey> |
| 133 | + <!-- 匹配生成的类型名称,替换掉数据中的前缀 --> |
| 134 | + <domainObjectRenamingRule searchString="^Bc" replaceString=""></domainObjectRenamingRule> |
| 135 | + </table> |
| 136 | + </context> |
| 137 | +</generatorConfiguration> |
| 138 | +{% endhighlight %} |
| 139 | + |
| 140 | +## 使用方式 |
| 141 | + |
| 142 | +在项目根目录中执行: |
| 143 | +{% highlight cli %} |
| 144 | +mvn mybatis-generator:generate |
| 145 | +{% endhighlight %} |
| 146 | + |
| 147 | +执行成功后,会显示如下界面 |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +如果执行失败的话,可以在上述命令执行时候在结尾增加__-X__参数。会显示详细的执行日志 |
| 152 | + |
| 153 | +完成,就会发生已经生成了实体、接口类和xml映射文件 |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | +每个文件内容如下: |
| 158 | + |
| 159 | +### OneMapper.java |
| 160 | + |
| 161 | +{% highlight java %} |
| 162 | +package com.bc.dao; |
| 163 | + |
| 164 | +import com.bc.entity.One; |
| 165 | +import tk.mybatis.mapper.common.Mapper; |
| 166 | + |
| 167 | +public interface OneMapper extends Mapper<One> { |
| 168 | + |
| 169 | + //继承了通用类,可以实现一些通用的函数,这里可以编写自定义查询函数... |
| 170 | + |
| 171 | +} |
| 172 | +{% endhighlight %} |
| 173 | + |
| 174 | + |
| 175 | +### One.java |
| 176 | + |
| 177 | +{% highlight java %} |
| 178 | +package com.bc.entity; |
| 179 | + |
| 180 | +import javax.persistence.*; |
| 181 | + |
| 182 | +@Table(name = "`bc_one`") |
| 183 | +public class One { |
| 184 | + @Id |
| 185 | + @Column(name = "`id`") |
| 186 | + @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "SELECT LAST_INSERT_ID()") |
| 187 | + private Integer id; |
| 188 | + |
| 189 | + @Column(name = "`name`") |
| 190 | + private String name; |
| 191 | + |
| 192 | + @Column(name = "`age`") |
| 193 | + private Integer age; |
| 194 | + |
| 195 | + /** |
| 196 | + * @return id |
| 197 | + */ |
| 198 | + public Integer getId() { |
| 199 | + return id; |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * @param id |
| 204 | + */ |
| 205 | + public void setId(Integer id) { |
| 206 | + this.id = id; |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * @return name |
| 211 | + */ |
| 212 | + public String getName() { |
| 213 | + return name; |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * @param name |
| 218 | + */ |
| 219 | + public void setName(String name) { |
| 220 | + this.name = name == null ? null : name.trim(); |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * @return age |
| 225 | + */ |
| 226 | + public Integer getAge() { |
| 227 | + return age; |
| 228 | + } |
| 229 | + |
| 230 | + /** |
| 231 | + * @param age |
| 232 | + */ |
| 233 | + public void setAge(Integer age) { |
| 234 | + this.age = age; |
| 235 | + } |
| 236 | +} |
| 237 | +{% endhighlight %} |
| 238 | + |
| 239 | +### OneMapper.xml |
| 240 | + |
| 241 | +{% highlight xml %} |
| 242 | +<?xml version="1.0" encoding="UTF-8"?> |
| 243 | +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| 244 | +<mapper namespace="com.bc.dao.OneMapper"> |
| 245 | + <resultMap id="BaseResultMap" type="com.bc.entity.One"> |
| 246 | + <!-- |
| 247 | + WARNING - @mbg.generated |
| 248 | + --> |
| 249 | + <id column="id" jdbcType="INTEGER" property="id" /> |
| 250 | + <result column="name" jdbcType="VARCHAR" property="name" /> |
| 251 | + <result column="age" jdbcType="INTEGER" property="age" /> |
| 252 | + </resultMap> |
| 253 | + <sql id="Base_Column_List"> |
| 254 | + <!-- |
| 255 | + WARNING - @mbg.generated |
| 256 | + --> |
| 257 | + id, `name`, age |
| 258 | + </sql> |
| 259 | +</mapper> |
| 260 | +{% endhighlight %} |
| 261 | + |
| 262 | +至此mybatis-generator的配置和用法就记录完成了,感觉上确实方便了很多,对于Dao类生成来说,不需要重复的去建立文件了,直接通过命令行直接生成就好了 |
| 263 | + |
| 264 | + |
| 265 | +下一篇文章介绍mybatis与springmvc的整合和使用 |
0 commit comments