Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件,它能够帮我们自动生成相应的实体类、Dao接口以及映射文件。
1.Eclipse中安装mybatis generator插件
mybatis generator下载地址:https://github.com/mybatis/generator/releases
第一步:下载Mybatis Generator插件将features和plugins这2个文件夹放到eclipse的安装目录下面,重启后eclipse即可。
第二步:验证是否配置成功,在线配置也是这么验证的,菜单File=>new=>others,输入Mybatis出现如下图标就代表配置成功了。
2.配置使用 mybatis generator
选中项目=>右键=》New=》other=》选择如上图所示=》Next=>Finish
然后会生成一个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="E:\eclipse-workspace\FrameStudy\SSM_MyBatisGenerator\WebContent\WEB-INF\lib\mysql-connector-java-5.1.6-bin.jar"/>
<context id="mysql">
<!--设置去除自动生成的注释-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/emps" userId="root" password="">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="com.ssmdemo.model" targetProject="SSM_MyBatisGenerator\src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="ssmdemo.mapping" targetProject="SSM_MyBatisGenerator\src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成mapper类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.ssmdemo.mapper" targetProject="SSM_MyBatisGenerator\src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<table tableName="employee" domainObjectName="Employee" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
选择generatorConfig.xml文件=>右键=》Run As =>Run mybatis Generator,然后它就会自动帮我们生成相应的实体类、Dao接口、实体类映射文件。
EmployeeMapper.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.ssmdemo.mapper.EmployeeMapper">
<resultMap id="BaseResultMap" type="com.ssmdemo.model.Employee">
<id column="eid" jdbcType="INTEGER" property="eid" />
<result column="cid" jdbcType="INTEGER" property="cid" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="height" jdbcType="REAL" property="height" />
<result column="weight" jdbcType="REAL" property="weight" />
</resultMap>
<sql id="Base_Column_List">
eid, cid, name, height, weight
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from employee
where eid = #{eid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from employee
where eid = #{eid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.ssmdemo.model.Employee">
insert into employee (eid, cid, name,
height, weight)
values (#{eid,jdbcType=INTEGER}, #{cid,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
#{height,jdbcType=REAL}, #{weight,jdbcType=REAL})
</insert>
<insert id="insertSelective" parameterType="com.ssmdemo.model.Employee">
insert into employee
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="eid != null">
eid,
</if>
<if test="cid != null">
cid,
</if>
<if test="name != null">
name,
</if>
<if test="height != null">
height,
</if>
<if test="weight != null">
weight,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="eid != null">
#{eid,jdbcType=INTEGER},
</if>
<if test="cid != null">
#{cid,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="height != null">
#{height,jdbcType=REAL},
</if>
<if test="weight != null">
#{weight,jdbcType=REAL},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.ssmdemo.model.Employee">
update employee
<set>
<if test="cid != null">
cid = #{cid,jdbcType=INTEGER},
</if>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="height != null">
height = #{height,jdbcType=REAL},
</if>
<if test="weight != null">
weight = #{weight,jdbcType=REAL},
</if>
</set>
where eid = #{eid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.ssmdemo.model.Employee">
update employee
set cid = #{cid,jdbcType=INTEGER},
name = #{name,jdbcType=VARCHAR},
height = #{height,jdbcType=REAL},
weight = #{weight,jdbcType=REAL}
where eid = #{eid,jdbcType=INTEGER}
</update>
</mapper>
EmployeeMapper.java
package com.ssmdemo.mapper;
import com.ssmdemo.model.Employee;
public interface EmployeeMapper {
int deleteByPrimaryKey(Integer eid);
int insert(Employee record);
int insertSelective(Employee record);
Employee selectByPrimaryKey(Integer eid);
int updateByPrimaryKeySelective(Employee record);
int updateByPrimaryKey(Employee record);
}
Employee.java
package com.ssmdemo.model;
public class Employee {
private Integer eid;
private Integer cid;
private String name;
private Float height;
private Float weight;
public Integer getEid() {
return eid;
}
public void setEid(Integer eid) {
this.eid = eid;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public Float getHeight() {
return height;
}
public void setHeight(Float height) {
this.height = height;
}
public Float getWeight() {
return weight;
}
public void setWeight(Float weight) {
this.weight = weight;
}
}