mybatis自动生成entity层和dao层中Mapper接口中的各个方法的意义及example实体类的用法

以下是自动生成的Mapper接口中的方法所代表的意义,网上这篇文章有更详细的讲解,推荐给大家MyBatis的Mapper接口以及Example的实例函数及详解

package cn.lichenyang.emall.dao;

import cn.lichenyang.emall.entity.TbContent;
import cn.lichenyang.emall.entity.TbContentExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface TbContentMapper {
	
    //根据条件计算总数,example用来设置当前对象的条件
    int countByExample(TbContentExample example);
    
    //根据条件删除,example用来设置当前对象的条件
    int deleteByExample(TbContentExample example);

    //根据主键删除,example用来设置当前对象的条件
    int deleteByPrimaryKey(Long id);

    //插入数据(如果当前对象没有值,也会被插入到数据库中)
    int insert(TbContent record);
    
    //插入数据,如果当前对象的属性为null,则当前属性不会插入数据库,只向数据库插入有值的属性
    int insertSelective(TbContent record);

    //按条件查询(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。
    List<TbContent> selectByExampleWithBLOBs(TbContentExample example);

    //根据条件查询,example就是TbContent的条件
    List<TbContent> selectByExample(TbContentExample example);

    //根据主键查询
    TbContent selectByPrimaryKey(Long id);
    
    //按条件更新值不为null的字段 			解释:前台传入那个值就修改那个值,example用于添加条件,相当于是where后面的内容			
    int updateByExampleSelective(@Param("record") TbContent record, @Param("example") TbContentExample example);
    
    //按条件进行跟新		 (包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。
    int updateByExampleWithBLOBs(@Param("record") TbContent record, @Param("example") TbContentExample example);

    //根据条件进行更新,所有带有example的修改,左边只用设置从界面传过来的值,右边设置的是传过来的对象的条件
    int updateByExample(@Param("record") TbContent record, @Param("example") TbContentExample example);

    //根据主键更新当前对象的属性的值不为null的属性,如果为null,就不进行修改
    int updateByPrimaryKeySelective(TbContent record);

    //根据主键进行更新,(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。
    int updateByPrimaryKeyWithBLOBs(TbContent record);

    //根据主键进行修改
    int updateByPrimaryKey(TbContent record);
}

xxxexample的用法:

xxxExample example = new xxxExample();  
Criteria criteria = example.createCriteria();  
方法说明:  
// 1.添加升序排列条件,DESC为降序  
example.setOrderByClause("字段名ASC")  
// 2.去除重复,boolean类型,true为选择不重复的记录  
example.setDistinct(false)  
// 3.添加字段xxx为null的条件  
criteria.andXxxIsNull  
// 4.添加字段xxx不为null的条件  
criteria.andXxxIsNotNull  
// 5.添加xxx字段等于value条件  
criteria.andXxxEqualTo(value)  
// 6.添加xxx字段不等于value条件  
criteria.andXxxNotEqualTo(value)  
// 7.添加xxx字段大于value条件  
criteria.andXxxGreaterThan(value)  
// 8.添加xxx字段大于等于value条件  
criteria.andXxxGreaterThanOrEqualTo(value)  
// 9.添加xxx字段小于value条件  
criteria.andXxxLessThan(value)  
// 10.添加xxx字段小于等于value条件  
criteria.andXxxLessThanOrEqualTo(value)  
// 11.添加xxx字段值在List  
criteria.andXxxIn(List)  
// 12.不添加xxx字段值在List  
criteria.andXxxNotIn(List)  
// 13.添加xxx字段值在之间  
criteria.andXxxBetween(value1,value2)  
// 14.添加xxx字段值不在之间  
criteria.andXxxNotBetween(value1,value2)

例:执行一下方法

int updateByExample(@Param("record") TbContent record, @Param("example") TbContentExample example);

recore是从界面传过来的对象(需要修改的属性封装成的对象),example就是上面设置好值之后的example

执行查询的时候:xxxmapper.updateByExample(record,example)


举更详细的例子

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
User user = new User();
user.setPassword("wyw");
XxxMapper.updateByPrimaryKeySelective(user,example);
//相当于:update user set password='wyw' where username='admin'


  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis可以通过逆向工程生成实体类,步骤如下: 1. 在项目添加MyBatis Generator插件依赖。 2. 创建一个MyBatis Generator配置文件,通常命名为`generatorConfig.xml`。 3. 配置MyBatis Generator,包括数据源、实体类生成路径、表名和实体类名的映射关系等。 4. 运行MyBatis Generator,自动生成实体类。 以下是一个简单的MyBatis Generator配置文件示例: ```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> <!-- 数据库连接信息 --> <context id="mysql" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="root"> </jdbcConnection> <!-- 实体类生成路径 --> <javaModelGenerator targetPackage="com.example.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 映射文件生成路径 --> <sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- DAO接口生成路径 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 表名和实体类名映射关系 --> <table tableName="user" domainObjectName="User"></table> </context> </generatorConfiguration> ``` 运行MyBatis Generator可以使用命令行工具或者IDEA插件,具体操作可以参考MyBatis官方文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值