Mybatis逆向工程

1、MyBatis的逆向工程

1.1.安装插件:

安装逆向工程插件 按照第2或者第3种方法安装

8.2 创建generator.xml.
选中generator.xml文件,右键-------> Run As ------->Run Mybatis Renerator 生成逆向工程
<?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>
  <!--版本信息   targetRuntime=“MyBatis3Simple” 会生成简单的增删改查 -->
 <context id="DB2Tables" targetRuntime="MyBatis3">
  <commentGenerator>
   <!--
    suppressAllComments属性值:
        true:自动生成实体类、SQL映射文件时没有注释
        false:自动生成实体类、SQL映射文件,并附有注释
     -->
   <property name="suppressAllComments" value="true" />
  </commentGenerator>
   
  <!-- 数据库连接信息 -->
  <jdbcConnection driverClass="com.mysql.jdbc.Driver"
   connectionURL="jdbc:mysql://localhost:3306/MybatisTest?characterEncoding=UTF-8" 
   userId="root"  password="123456">
  </jdbcConnection>
  
   <!-- 
            forceBigDecimals属性值: 
                true:把数据表中的DECIMAL和NUMERIC类型,
解析为JAVA代码中的java.math.BigDecimal类型 
                false(默认):把数据表中的DECIMAL和NUMERIC类型,
解析为解析为JAVA代码中的Integer类型 
        -->
  <javaTypeResolver>
   <property name="forceBigDecimals" value="false" />
  </javaTypeResolver>
​
      <!-- 
            targetProject属性值:实体类javaBean的生成位置  项目名/src
            targetPackage属性值:实体类所在包的路径   包路径名
        --> 
  <javaModelGenerator targetPackage="com.pojo" 
                             targetProject="Mybatis02/src/mybatis">
   <!-- trimStrings属性值:
                true:对数据库的查询结果进行trim操作
                false(默认):不进行trim操作
    -->
   <property name="trimStrings" value="true" />
  </javaModelGenerator>
   
  <!-- 
            targetProject属性值:SQL映射文件的生成位置(接口以及xml)  
            targetPackage属性值:SQL映射文件所在包的路径(xml)
        -->
  <sqlMapGenerator targetPackage="com.mapper" targetProject="Mybatis02/src/mybatis">
  </sqlMapGenerator>
  <!-- 生成动态代理的接口  -->
  <javaClientGenerator type="XMLMAPPER" targetPackage="com.mapper" targetProject="Mybatis02/src/mybatis">
  </javaClientGenerator>
  
<!-- 指定要逆向的数据库表  tableName表名   useActualColumnNames true:实体类属性使用原数据库属性名  false:使用驼峰规则-->
  <table tableName="category">
  <property name="useActualColumnNames" value="false"/>
   </table>
      
 </context>
</generatorConfiguration>

2、映射关系

  <resultMap id="BaseResultMap" type="com.mybatis.pojo.TAdmin">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="admin_name" jdbcType="VARCHAR" property="adminName" />
    <result column="admin_password" jdbcType="VARCHAR" property="adminPassword" />
    <result column="admin_email" jdbcType="VARCHAR" property="adminEmail" />
    <result column="admin_address" jdbcType="VARCHAR" property="adminAddress" />
  </resultMap>

参数以实体类的属性名为准,即property的值

注意: 在写select时,必须加上resultMap="BaseResultMap",才能保持数据的一致性

  <select id="selectUserList" resultType="com.ssmTest.system.admin.user.pojo.User" resultMap="BaseResultMap">
     select <include refid="Base_Column_List" /> from `user`
  </select>

3、逆向工程中各个方法的使用

逆向工程提供了数据的增删改查的基本操作

方法功能说明
long countByExample(EmployeeExample example) ;按条件计数
int deleteByExample(EmployeeExample example);按条件删除
int deleteByPrimaryKey(Integer id);按主键删除
int insert(Employee record);插入数据
int insertSelective(Employee record);按条件插入数据
List selectByExample(EmployeeExample example);按条件查询
Employee selectByPrimaryKey(Integer id);按主键查询
int updateByExampleSelective(@Param(“record”) Employee record, @Param(“example”) EmployeeExample example);按条件更新值不为null的字段
int updateByExample(@Param(“record”) Employee record, @Param(“example”) EmployeeExample example);按条件更新
int updateByPrimaryKeySelective(Employee record);按主键更新值不为null的字段
int updateByPrimaryKey(Employee record);按主键更新
9.1.1 Example用法讲解

Example相当于where后面的条件

1. deleteByExample

        TAdminExample adminExample = new TAdminExample();
        TAdminExample.Criteria criteria = adminExample.createCriteria();
        
        criteria.andAdminNameLike("%李%");
        adminMapper.deleteByExample(adminExample);

2. selectByExample    

  TAdminExample adminExample = new TAdminExample();
        TAdminExample.Criteria criteria = adminExample.createCriteria();
        
        criteria.andAdminNameLike("李%"); 
        //adminMapper.deleteByExample(adminExample);
        criteria.andAdminPasswordEqualTo("123");
        List<TAdmin> admins =  adminMapper.selectByExample(adminExample);
        System.out.println(admins.toString());
//相当于数据库语句  select id, admin_name, admin_password, admin_email, admin_address from t_admin WHERE ( admin_name like ‘李%’ and admin_password = ‘123’ ) 

3. countByExample

TAdminExample adminExample = new TAdminExample();
        TAdminExample.Criteria criteria = adminExample.createCriteria();
        
        criteria.andAdminNameLike("李%");
        //adminMapper.deleteByExample(adminExample);
        criteria.andAdminPasswordEqualTo("123");
        List<TAdmin> admins =  adminMapper.selectByExample(adminExample);
        System.out.println(admins.toString());
        
        long count = adminMapper.countByExample(adminExample);
        System.out.println("姓李的且密码为123的人数有:"+count);

4. int updateByExampleSelective(@Param("row") TAdmin row, @Param("example") TAdminExample example):

有条件的修改部分数据。参数 前面是要修改的数据,后面是条件

   TAdminExample adminExample = new TAdminExample();
        TAdminExample.Criteria criteria = adminExample.createCriteria();
        
        criteria.andAdminNameLike("李%");
        criteria.andAdminPasswordEqualTo("123");
​
    TAdmin admin = new TAdmin();
        admin.setAdminAddress("中国");
        admin.setAdminEmail("123456@qq.com");
        
        adminMapper.updateByExampleSelective(admin, adminExample);

​ //相当于

update t_admin SET admin_email = ?, admin_address = ? WHERE ( admin_name like ? and admin_password = ? ) Parameters: 123456@qq.com(String), 中国(String), 李%(String), 123(String)

  • 25
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小陈编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值