实体类?Dao接口?Mapper映射文件?都别写了!!!用这种方法就可以
编辑
大家好,我是雄雄,前两天一直在解决使用idea整合SSM的报错问题,今天,给大家带来的是如何使用插件快速生成实体类、接口以及映射文件,相信你看过本文之后对你有很大的帮助。
前言
每次我们在写代码的时候,都会对应数据表来写实体类,以及接口中常用的增删改查方法,包括Mapper映射文件,一个两个表还可以,如果数据表多的情况下,可见工作量也会很大,对此有没有一种很好的解决办法呢?答案当然是有的,下面我们就来看看如何让程序自动生成这些代码呢?
01
下载所需的工具
需要下载“mybatis反向生成工具”(可在本公众号【雄雄的小课堂】后台回复:mybatis反向生成工具,即可免费下载)。
02
修改配置文件generatorConfig.xml
打开下载的工具,进入lib文件夹,右击用记事本(其他编辑器也可以)打开generatorConfig.xml文件,如下所示:
编辑
数据库表设计如下:
编辑
所在数据库为:schooldb。
1.加载所对应数据库的jar,我在这里用的是mysql数据库,所以就得需要mysql的驱动jar文件(jar文件已经放至lib目录中,oracle的也有),将名字写在下面的地方。
<classPathEntry location="mysql-connector-java-5.1.25-bin.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
mysql-connector-java-5.1.25-bin.jar就是数据库的jar包。
2.连接配置(此步骤的作用就是连接数据库),将对应的driverClass,connectionURL,userId和password写上,如下是我的,你就把用户名和密码改成你的就行。
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/schooldb" userId="root" password="root">
</jdbcConnection>
3.配置生成实体类、Dao接口以及映射文件的路径,我在这放在了org.dao里面了,最后在放在src下面,如下代码:
<javaModelGenerator targetPackage="org.entity" targetProject="src">
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.shkj.pojo, true:com.shkj.pojo.[schemaName] -->
<property name="enableSubPackages" value="true"/>
<!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
<!-- <property name="trimStrings" value="false"/> -->
</javaModelGenerator>
<!-- 生成xml文件的路径,这个路径可以自动生成,但是必须有src这个路径-->
<sqlMapGenerator targetPackage="org.dao" targetProject="src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成Dao类的路径,这个路径可以自动生成,但是必须有src这个路径-->
<javaClientGenerator type="XMLMAPPER" targetPackage="org.dao" targetProject="src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
4.根据表生成对应的类名:
<table tableName="BookManage" domainObjectName="BookManage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
注意:这是一个表,如果有多个表的话,需要写多个
03
开始生成
打开文件中的cmd.bat批处理,双击进入之后自己会定位当前所在的目录,然后输入:java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite 回车即可。
如下所示:
编辑
最后打开文件夹中的src目录,就会发现想要的都在里面。
实体类:
package org.entity;
import java.util.Date;
public class BookManage {
private Integer bid;
private String bname;
private String bauthor;
private Date btime;
private Integer btype;
public Integer getBid() {
return bid;
}
public void setBid(Integer bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public String getBauthor() {
return bauthor;
}
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
public Date getBtime() {
return btime;
}
public void setBtime(Date btime) {
this.btime = btime;
}
public Integer getBtype() {
return btype;
}
public void setBtype(Integer btype) {
this.btype = btype;
}
}
Dao接口:
package org.dao;
import org.entity.BookManage;
public interface BookManageMapper {
int deleteByPrimaryKey(Integer bid);
int insert(BookManage record);
int insertSelective(BookManage record);
BookManage selectByPrimaryKey(Integer bid);
int updateByPrimaryKeySelective(BookManage record);
int updateByPrimaryKey(BookManage record);
}
Dao接口所对应的Mapper文件:
<?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="org.dao.BookManageMapper" >
<resultMap id="BaseResultMap" type="org.entity.BookManage" >
<id column="bid" property="bid" jdbcType="INTEGER" />
<result column="bname" property="bname" jdbcType="VARCHAR" />
<result column="bauthor" property="bauthor" jdbcType="VARCHAR" />
<result column="btime" property="btime" jdbcType="TIMESTAMP" />
<result column="btype" property="btype" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
bid, bname, bauthor, btime, btype
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from bookmanage
where bid = #{bid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from bookmanage
where bid = #{bid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="org.entity.BookManage" >
insert into bookmanage (bid, bname, bauthor,
btime, btype)
values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{bauthor,jdbcType=VARCHAR},
#{btime,jdbcType=TIMESTAMP}, #{btype,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="org.entity.BookManage" >
insert into bookmanage
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="bid != null" >
bid,
</if>
<if test="bname != null" >
bname,
</if>
<if test="bauthor != null" >
bauthor,
</if>
<if test="btime != null" >
btime,
</if>
<if test="btype != null" >
btype,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="bid != null" >
#{bid,jdbcType=INTEGER},
</if>
<if test="bname != null" >
#{bname,jdbcType=VARCHAR},
</if>
<if test="bauthor != null" >
#{bauthor,jdbcType=VARCHAR},
</if>
<if test="btime != null" >
#{btime,jdbcType=TIMESTAMP},
</if>
<if test="btype != null" >
#{btype,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="org.entity.BookManage" >
update bookmanage
<set >
<if test="bname != null" >
bname = #{bname,jdbcType=VARCHAR},
</if>
<if test="bauthor != null" >
bauthor = #{bauthor,jdbcType=VARCHAR},
</if>
<if test="btime != null" >
btime = #{btime,jdbcType=TIMESTAMP},
</if>
<if test="btype != null" >
btype = #{btype,jdbcType=INTEGER},
</if>
</set>
where bid = #{bid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="org.entity.BookManage" >
update bookmanage
set bname = #{bname,jdbcType=VARCHAR},
bauthor = #{bauthor,jdbcType=VARCHAR},
btime = #{btime,jdbcType=TIMESTAMP},
btype = #{btype,jdbcType=INTEGER}
where bid = #{bid,jdbcType=INTEGER}
</update>
</mapper>
- 点赞
- 收藏
- 关注作者
评论(0)