十二、Mybatis逆向工程生成的代码使用详解

阅读本篇文章请先查看十一、Mybatis逆向工程代码的生成和介绍

使用逆向工程代码

将上篇文章中逆向生成的mapper和pojo包下的所有内容拷贝到你的项目所在的工程,如下所示:
在这里插入图片描述
我这里也是从前面集成Spring的项目中复制出来的一新项目,还不清楚怎么搭建环境的可以先看看八、Mybatis整合spring之Mapper接口代理实现dao层

增删改查操作

mybatis逆向工程生成的Mapper接口一共提供了11个方法,如下所示:

package blog.csdn.net.mchenys.mapper;

import blog.csdn.net.mchenys.pojo.User;
import blog.csdn.net.mchenys.pojo.UserExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface UserMapper {
	//1.根据自定义条件查询记录的条数
    int countByExample(UserExample example);
    
    //2.根据自定义条件删除记录
    int deleteByExample(UserExample example);

    //3.根据主键删除记录
    int deleteByPrimaryKey(Integer id);

    //4.插入对象所有字段
    int insert(User record);

    //5.插入对象不为空的字段
    int insertSelective(User record);

    //6.根据自定义条件查询结果
    List<User> selectByExample(UserExample example);

    //7.根据主键查询
    User selectByPrimaryKey(Integer id);

    //8.根据条件更新所有非空字段,@Param注解可以指定传入参数的名称,
    //例如这里record表示的就是User,在UserMapper.xml可以通过record来访问User中的属性
    int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);
    
    //9.根据条件更新所有字段
    int updateByExample(@Param("record") User record, @Param("example") UserExample example);

    //10.根据主键将对象中不为空的值更新至数据库
    int updateByPrimaryKeySelective(User record);

    //11.根据主键将对象所有字段更新至数据库
    int updateByPrimaryKey(User record);
}

下面分别给出各个方法的使用示例

countByExample

// 根据自定义条件查询记录的条数
@Test
public void testCountByExample() {

	UserMapper userMapper = (UserMapper) applicatonContext.getBean("userMapper");

	// 创建where条件的封装类
	UserExample userExample = new UserExample();
	// 添加一个条件,用户名非空,sex=1
	userExample.createCriteria().andUsernameIsNotNull().andAddressEqualTo("广州");
	// 添加一个or条件,或者地址是上海的用户
	userExample.or().andAddressEqualTo("上海");

	// 对应的sql语句就是:
	// select count(*) from user WHERE ( username is not null and address = ? ) or(
	// address = ? )

	// 根据条件查询符合条件的个数
	int countByExample = userMapper.countByExample(userExample);
	System.out.println("countByExample=" + countByExample);
}

deleteByExample

// 根据自定义条件删除记录
@Test
public void testDeleteByExample() {
	UserMapper userMapper = (UserMapper) applicatonContext.getBean("userMapper");
	// 创建where条件的封装类
	UserExample userExample = new UserExample();
	// 添加一个条件id=16
	userExample.createCriteria().andIdEqualTo(16);

	// 对应的sql语句就是:delete from user WHERE ( id = ? )

	userMapper.deleteByExample(userExample);

	// 自动commit
}

deleteByPrimaryKey

// 根据主键删除记录
@Test
public void testDeleteByPrimaryKey() {
	UserMapper userMapper = (UserMapper) applicatonContext.getBean("userMapper");

	// 对应的sql语句:delete from user where id = ?
	userMapper.deleteByPrimaryKey(31);

}

insert

// 插入对象所有字段
@Test
public void testInsert() {
	UserMapper userMapper = (UserMapper) applicatonContext.getBean("userMapper");

	User record = new User();
	record.setUsername("小城");
	record.setAddress("深圳");

	// 对应的sql语句:insert into user (id, username, birthday, sex, address) values (?,
	// ?, ?, ?, ?)
	// 对应的参数是:null, 小城(String), null, null, 深圳(String),由此可知未赋值的字段都被设置了null
	userMapper.insert(record);

}

insertSelective

// 插入对象不为空的字段
@Test
public void testInsertSelective() {
	UserMapper userMapper = (UserMapper) applicatonContext.getBean("userMapper");
	User record = new User();
	record.setUsername("小陈");
	record.setAddress("深圳");

	// 对应的sql语句:insert into user ( username, address ) values ( ?, ? )
	// 参数: 小陈(String), 深圳(String)

	userMapper.insertSelective(record);
}

selectByExample

// 根据自定义条件查询结果
@Test
public void testSelectByExample() {
	UserMapper userMapper = (UserMapper) applicatonContext.getBean("userMapper");
	UserExample example = new UserExample();
	// 添加一个条件,姓名模糊查询,并且地址是广州
	example.createCriteria().andUsernameLike("%小%").andAddressEqualTo("广州");

	// 对应的sql语句:select id, username, birthday, sex, address from user WHERE (
	// username like ? and address = ? )

	// 对应的参数:%小%(String), 广州(String)
	List<User> user = userMapper.selectByExample(example);
	System.out.println(user);

}

selectByPrimaryKey

// 根据主键查询
@Test
public void testSelectByPrimaryKey() {
	UserMapper userMapper = (UserMapper) applicatonContext.getBean("userMapper");

	// 对应的sql语句:select id, username, birthday, sex, address from user where id = ?
	User user = userMapper.selectByPrimaryKey(1);
	System.out.println(user);
}

updateByExampleSelective

// 根据条件更新所有非空字段
@Test
public void testUpdateByExampleSelective() {
	UserMapper userMapper = (UserMapper) applicatonContext.getBean("userMapper");
	// 修改的内容
	User record = new User();
	record.setUsername("阿胜");
	record.setAddress("广州");
	// where条件
	UserExample example = new UserExample();
	example.createCriteria().andIdEqualTo(25);

	// 对应的sql语句:update user SET username = ?, address = ? WHERE ( id = ? )
	// 对应的参数:阿胜(String), 广州(String), 25(Integer)
	userMapper.updateByExampleSelective(record, example);
}

updateByExample

// 根据条件更新所有字段
@Test
public void testUpdateByExample() {
	UserMapper userMapper = (UserMapper) applicatonContext.getBean("userMapper");
	// 修改的内容
	User record = new User();
	record.setUsername("老王");
	record.setId(45);

	// where条件
	UserExample example = new UserExample();
	example.createCriteria().andUsernameEqualTo("王二狗");

	// 对应的sql语句:update user set id = ?, username = ?, birthday = ?, sex = ?, address
	// = ? WHERE ( username = ? )
	// 对应的参数:45(Integer), 老王(String), null, null, null, 王二狗(String)

	userMapper.updateByExample(record, example);
}

updateByPrimaryKeySelective

// 根据主键将对象中不为空的值更新至数据库
@Test
public void testUpdateByPrimaryKeySelective() {
	UserMapper userMapper = (UserMapper) applicatonContext.getBean("userMapper");
	// 修改的内容
	User record = new User();
	record.setUsername("王二狗");
	record.setId(45);
	
	//对应的sql语句:update user SET username = ? where id = ? 
	//参数:王二狗(String), 45(Integer)
	userMapper.updateByPrimaryKeySelective(record);
}

updateByPrimaryKey

// 根据主键将对象中更新至数据库
@Test
public void testUpdateByPrimaryKey() {
	UserMapper userMapper = (UserMapper) applicatonContext.getBean("userMapper");
	// 修改的内容
	User record = new User();
	record.setUsername("老王");
	record.setId(45);
	
	//对应的sql语句:update user set username = ?, birthday = ?, sex = ?, address = ? where id = ? 
	//参数:老王(String), null, null, null, 45(Integer)
	userMapper.updateByPrimaryKey(record);
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值