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

相关文章

MyBatis中$与#的区别解析

《MyBatis中$与#的区别解析》文章浏览阅读314次,点赞4次,收藏6次。MyBatis使用#{}作为参数占位符时,会创建预处理语句(PreparedStatement),并将参数值作为预处理语句... 目录一、介绍二、sql注入风险实例一、介绍#(井号):MyBATis使用#{}作为参数占位符时,会

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)