MyBatis自动生成实体类、DAO接口和Mapping映射文件的代码(逆向工程)

本文主要是介绍MyBatis自动生成实体类、DAO接口和Mapping映射文件的代码(逆向工程),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

MyBatis属于一种半自动的ORM框架,它需要程序员自己编写sql语句和映射文件,但是编写映射文件和sql语句很容易出错,所以mybatis官方提供了Generator生成器,自动生成DAO接口。实体类和Mapping。这个生成器是根据单表自动生成mybatis执行所需要的代码,因此,首先得先创建数据库表,然后再自动生成代码。

1.创建user数据表

create table `user`(id int(11) not null AUTO_INCREMENT,userName varchar(40) not null,password varchar(255) not null,age int(4) not null,primary key(`id`)
)DEFAULT CHARSET=utf8;  

2.通过代码生成器自动生成代码

代码生成器的下载地址为:http://download.csdn.net/download/u013216156/10134751)
解压代码生成器,打开它的lib目录,如下所示:

lib目录下包含了代码生成器生成代码所需要的mybatis和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="mysql-connector-java-5.1.25-bin.jar"/>    <context id="DB2Tables"  targetRuntime="MyBatis3">    <commentGenerator>    <property name="suppressDate" value="true"/>     <property name="suppressAllComments" value="true"/>    </commentGenerator>    <!--数据库链接URL,用户名、密码 -->    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/ssm" userId="root" password="hello">    </jdbcConnection>    <javaTypeResolver>    <property name="forceBigDecimals" value="false"/>    </javaTypeResolver>    <!-- 生成模型的包名和位置-->    <javaModelGenerator targetPackage="com.spenglu.pojo" targetProject="src">    <property name="enableSubPackages" value="true"/>    <property name="trimStrings" value="true"/>    </javaModelGenerator>    <!-- 生成映射文件的包名和位置-->    <sqlMapGenerator targetPackage="com.spenglu.mapping" targetProject="src">    <property name="enableSubPackages" value="true"/>    </sqlMapGenerator>    <!-- 生成DAO的包名和位置-->    <javaClientGenerator type="XMLMAPPER" targetPackage="com.spenglu.IDao" targetProject="src">    <property name="enableSubPackages" value="true"/>    </javaClientGenerator>    <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->    <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>  </context>    
</generatorConfiguration>

只需要把上面文件注释下面的内容修改为自己工程相关的内容就可以了.
文件修改完以后,需要通过控制台来执行脚本生成代码。

  • 打开控制台,进入lib目录下,如下图所示:

    先通过”E:”指令进入E盘,然后通过”cd +lib文件路径”进入lib目录下
  • 执行脚本java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
    自动生成代码如下图所示:

    执行成功以后会在你自己定义的文件目录下生成相关的代码,如下图所示:

    package com.spenglu.IDao;
    import com.spenglu.pojo.User;
    public interface UserMapper {int deleteByPrimaryKey(Integer id);int insert(User record);int insertSelective(User record);User selectByPrimaryKey(Integer id);int updateByPrimaryKeySelective(User record);int updateByPrimaryKey(User record);
    }
    

<?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.spenglu.IDao.UserMapper" ><resultMap id="BaseResultMap" type="com.spenglu.pojo.User" ><id column="id" property="id" jdbcType="INTEGER" /><result column="userName" property="username" jdbcType="VARCHAR" /><result column="password" property="password" jdbcType="VARCHAR" /><result column="age" property="age" jdbcType="INTEGER" /></resultMap><sql id="Base_Column_List" >id, userName, password, age</sql><select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >select<include refid="Base_Column_List" />from userwhere id = #{id,jdbcType=INTEGER}</select><delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >delete from userwhere id = #{id,jdbcType=INTEGER}</delete><insert id="insert" parameterType="com.spenglu.pojo.User" >insert into user (id, userName, password,age)values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},#{age,jdbcType=INTEGER})</insert><insert id="insertSelective" parameterType="com.spenglu.pojo.User" >insert into user<trim prefix="(" suffix=")" suffixOverrides="," ><if test="id != null" >id,</if><if test="username != null" >userName,</if><if test="password != null" >password,</if><if test="age != null" >age,</if></trim><trim prefix="values (" suffix=")" suffixOverrides="," ><if test="id != null" >#{id,jdbcType=INTEGER},</if><if test="username != null" >#{username,jdbcType=VARCHAR},</if><if test="password != null" >#{password,jdbcType=VARCHAR},</if><if test="age != null" >#{age,jdbcType=INTEGER},</if></trim></insert><update id="updateByPrimaryKeySelective" parameterType="com.spenglu.pojo.User" >update user<set ><if test="username != null" >userName = #{username,jdbcType=VARCHAR},</if><if test="password != null" >password = #{password,jdbcType=VARCHAR},</if><if test="age != null" >age = #{age,jdbcType=INTEGER},</if></set>where id = #{id,jdbcType=INTEGER}</update><update id="updateByPrimaryKey" parameterType="com.spenglu.pojo.User" >update userset userName = #{username,jdbcType=VARCHAR},password = #{password,jdbcType=VARCHAR},age = #{age,jdbcType=INTEGER}where id = #{id,jdbcType=INTEGER}</update>
</mapper>

package com.spenglu.pojo;
public class User {private Integer id;private String username;private String password;private Integer age;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username == null ? null : username.trim();}public String getPassword() {return password;}public void setPassword(String password) {this.password = password == null ? null : password.trim();}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}
}

这篇关于MyBatis自动生成实体类、DAO接口和Mapping映射文件的代码(逆向工程)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1125693

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

MybatisPlus service接口功能介绍

《MybatisPlusservice接口功能介绍》:本文主要介绍MybatisPlusservice接口功能介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录Service接口基本用法进阶用法总结:Lambda方法Service接口基本用法MyBATisP

Mybatis的分页实现方式

《Mybatis的分页实现方式》MyBatis的分页实现方式主要有以下几种,每种方式适用于不同的场景,且在性能、灵活性和代码侵入性上有所差异,对Mybatis的分页实现方式感兴趣的朋友一起看看吧... 目录​1. 原生 SQL 分页(物理分页)​​2. RowBounds 分页(逻辑分页)​​3. Page

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Mybatis Plus Join使用方法示例详解

《MybatisPlusJoin使用方法示例详解》:本文主要介绍MybatisPlusJoin使用方法示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录1、pom文件2、yaml配置文件3、分页插件4、示例代码:5、测试代码6、和PageHelper结合6

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

MyBatis设计SQL返回布尔值(Boolean)的常见方法

《MyBatis设计SQL返回布尔值(Boolean)的常见方法》这篇文章主要为大家详细介绍了MyBatis设计SQL返回布尔值(Boolean)的几种常见方法,文中的示例代码讲解详细,感兴趣的小伙伴... 目录方案一:使用COUNT查询存在性(推荐)方案二:条件表达式直接返回布尔方案三:存在性检查(EXI

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三