Mybatis-Generator的具体使用方法
Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。
转自zorro的菜鸟笔记
1.相关文件
关于Mybatis-Generator的下载可以到这个地址https://github.com/mybatis/generator/releases进行下载
因为我使用的是mysql数据库,所以在这里,先准备一下mysql连接驱动的jar包
这里还需要一个配置文件
generatorConfig.xml
<?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="/xxx/xxx/Mybatis/mysql-connector-java-8.0.11.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:3306/mybatis" userId="root" password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="com.project.model" targetProject="src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="com.project.mapping" targetProject="src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.project.dao" targetProject="src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<table tableName="stationInfo" domainObjectName="Station" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
以上配置文件中的
<table tableName="stationInfo" domainObjectName="Station" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
tableName和domainObjectName为必选项,分别代表数据库表名和生成的实力类名,其余的可以自定义去选择(一般情况下均为false)
切换到项目目录下执行以下语句:
java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
执行命令完后,我们可以看到src多了几个目录,如图下所示:
打开文件夹查看生成的代码
Station.java
package com.cugb.model;
public class Station {
private Integer stationid;
private String trannum;
private String station;
private String outtime;
private Integer daytime;
private Float siteprice;
public Integer getStationid() {
return stationid;
}
public void setStationid(Integer stationid) {
this.stationid = stationid;
}
public String getTrannum() {
return trannum;
}
public void setTrannum(String trannum) {
this.trannum = trannum == null ? null : trannum.trim();
}
public String getStation() {
return station;
}
public void setStation(String station) {
this.station = station == null ? null : station.trim();
}
public String getOuttime() {
return outtime;
}
public void setOuttime(String outtime) {
this.outtime = outtime == null ? null : outtime.trim();
}
public Integer getDaytime() {
return daytime;
}
public void setDaytime(Integer daytime) {
this.daytime = daytime;
}
public Float getSiteprice() {
return siteprice;
}
public void setSiteprice(Float siteprice) {
this.siteprice = siteprice;
}
}
StationMapper.xml
<?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="com.cugb.dao.StationMapper">
<resultMap id="BaseResultMap" type="com.cugb.model.Station">
<id column="stationId" jdbcType="INTEGER" property="stationid" />
<result column="tranNum" jdbcType="VARCHAR" property="trannum" />
<result column="station" jdbcType="VARCHAR" property="station" />
<result column="outTime" jdbcType="VARCHAR" property="outtime" />
<result column="dayTime" jdbcType="INTEGER" property="daytime" />
<result column="sitePrice" jdbcType="REAL" property="siteprice" />
</resultMap>
<sql id="Base_Column_List">
stationId, tranNum, station, outTime, dayTime, sitePrice
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from stationInfo
where stationId = #{stationid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from stationInfo
where stationId = #{stationid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.cugb.model.Station">
insert into stationInfo (stationId, tranNum, station,
outTime, dayTime, sitePrice
)
values (#{stationid,jdbcType=INTEGER}, #{trannum,jdbcType=VARCHAR}, #{station,jdbcType=VARCHAR},
#{outtime,jdbcType=VARCHAR}, #{daytime,jdbcType=INTEGER}, #{siteprice,jdbcType=REAL}
)
</insert>
<insert id="insertSelective" parameterType="com.cugb.model.Station">
insert into stationInfo
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="stationid != null">
stationId,
</if>
<if test="trannum != null">
tranNum,
</if>
<if test="station != null">
station,
</if>
<if test="outtime != null">
outTime,
</if>
<if test="daytime != null">
dayTime,
</if>
<if test="siteprice != null">
sitePrice,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="stationid != null">
#{stationid,jdbcType=INTEGER},
</if>
<if test="trannum != null">
#{trannum,jdbcType=VARCHAR},
</if>
<if test="station != null">
#{station,jdbcType=VARCHAR},
</if>
<if test="outtime != null">
#{outtime,jdbcType=VARCHAR},
</if>
<if test="daytime != null">
#{daytime,jdbcType=INTEGER},
</if>
<if test="siteprice != null">
#{siteprice,jdbcType=REAL},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.cugb.model.Station">
update stationInfo
<set>
<if test="trannum != null">
tranNum = #{trannum,jdbcType=VARCHAR},
</if>
<if test="station != null">
station = #{station,jdbcType=VARCHAR},
</if>
<if test="outtime != null">
outTime = #{outtime,jdbcType=VARCHAR},
</if>
<if test="daytime != null">
dayTime = #{daytime,jdbcType=INTEGER},
</if>
<if test="siteprice != null">
sitePrice = #{siteprice,jdbcType=REAL},
</if>
</set>
where stationId = #{stationid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.cugb.model.Station">
update stationInfo
set tranNum = #{trannum,jdbcType=VARCHAR},
station = #{station,jdbcType=VARCHAR},
outTime = #{outtime,jdbcType=VARCHAR},
dayTime = #{daytime,jdbcType=INTEGER},
sitePrice = #{siteprice,jdbcType=REAL}
where stationId = #{stationid,jdbcType=INTEGER}
</update>
</mapper>
StationMapper.java
package com.cugb.dao;
import com.cugb.model.Station;
public interface StationMapper {
int deleteByPrimaryKey(Integer stationid);
int insert(Station record);
int insertSelective(Station record);
Station selectByPrimaryKey(Integer stationid);
int updateByPrimaryKeySelective(Station record);
int updateByPrimaryKey(Station record);
}