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

相关文章

java之Objects.nonNull用法代码解读

《java之Objects.nonNull用法代码解读》:本文主要介绍java之Objects.nonNull用法代码,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录Java之Objects.nonwww.chinasem.cnNull用法代码Objects.nonN

java中使用POI生成Excel并导出过程

《java中使用POI生成Excel并导出过程》:本文主要介绍java中使用POI生成Excel并导出过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求说明及实现方式需求完成通用代码版本1版本2结果展示type参数为atype参数为b总结注:本文章中代码均为

SpringBoot中封装Cors自动配置方式

《SpringBoot中封装Cors自动配置方式》:本文主要介绍SpringBoot中封装Cors自动配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot封装Cors自动配置背景实现步骤1. 创建 GlobalCorsProperties

在java中如何将inputStream对象转换为File对象(不生成本地文件)

《在java中如何将inputStream对象转换为File对象(不生成本地文件)》:本文主要介绍在java中如何将inputStream对象转换为File对象(不生成本地文件),具有很好的参考价... 目录需求说明问题解决总结需求说明在后端中通过POI生成Excel文件流,将输出流(outputStre

idea中创建新类时自动添加注释的实现

《idea中创建新类时自动添加注释的实现》在每次使用idea创建一个新类时,过了一段时间发现看不懂这个类是用来干嘛的,为了解决这个问题,我们可以设置在创建一个新类时自动添加注释,帮助我们理解这个类的用... 目录前言:详细操作:步骤一:点击上方的 文件(File),点击&nbmyHIgsp;设置(Setti

Spring Boot结成MyBatis-Plus最全配置指南

《SpringBoot结成MyBatis-Plus最全配置指南》本文主要介绍了SpringBoot结成MyBatis-Plus最全配置指南,包括依赖引入、配置数据源、Mapper扫描、基本CRUD操... 目录前言详细操作一.创建项目并引入相关依赖二.配置数据源信息三.编写相关代码查zsRArly询数据库数

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

python+opencv处理颜色之将目标颜色转换实例代码

《python+opencv处理颜色之将目标颜色转换实例代码》OpenCV是一个的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上,:本文主要介绍python+ope... 目录下面是代码+ 效果 + 解释转HSV: 关于颜色总是要转HSV的掩膜再标注总结 目标:将红色的部分滤

在C#中调用Python代码的两种实现方式

《在C#中调用Python代码的两种实现方式》:本文主要介绍在C#中调用Python代码的两种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#调用python代码的方式1. 使用 Python.NET2. 使用外部进程调用 Python 脚本总结C#调

Spring Boot 整合 MyBatis 连接数据库及常见问题

《SpringBoot整合MyBatis连接数据库及常见问题》MyBatis是一个优秀的持久层框架,支持定制化SQL、存储过程以及高级映射,下面详细介绍如何在SpringBoot项目中整合My... 目录一、基本配置1. 添加依赖2. 配置数据库连接二、项目结构三、核心组件实现(示例)1. 实体类2. Ma