java中的mapper_Java的MyBatis框架中Mapper映射配置的使用及原理解析

本文详细介绍了MyBatis框架中Mapper的内置方法,包括countByExample、deleteByExample、deleteByPrimaryKey、insert、insertSelective、selectByExample、selectByPrimaryKey、updateByExampleSelective、updateByPrimaryKeySelective和updateByPrimaryKey等,通过实例展示了如何使用这些方法执行SQL操作。同时,解析了Mapper的XML配置文件读取和解析过程,阐述了MyBatis如何将Mapper配置与SQL语句关联并执行。
摘要由CSDN通过智能技术生成

Mapper的内置方法model层就是实体类,对应数据库的表。controller层是Servlet,主要是负责业务模块流程的控制,调用service接口的方法,在struts2就是Action。Service层主要做逻辑判断,Dao层是数据访问层,与数据库进行对接。至于Mapper是mybtis框架的映射用到,mapper映射文件在dao层用。

下面是介绍一下Mapper的内置方法:

1、countByExample ===>根据条件查询数量

int countByExample(UserExample example);

//下面是一个完整的案列

UserExample example = new UserExample();

Criteria criteria = example.createCriteria();

criteria.andUsernameEqualTo("joe");

int count = userDAO.countByExample(example);

相当于:select count(*) from user where username='joe'

2、deleteByExample ===>根据条件删除多条

int deleteByExample(AccountExample example);

//下面是一个完整的案例

UserExample example = new UserExample();

Criteria criteria = example.createCriteria();

criteria.andUsernameEqualTo("joe");

userDAO.deleteByExample(example);

相当于:delete from user where username='joe'

3、deleteByPrimaryKey===>根据条件删除单条

int deleteByPrimaryKey(Integer id);

userDAO.deleteByPrimaryKey(101);

相当于:

delete from user where id=101

4、insert===>插入数据

int insert(Account record);

//下面是完整的案例

User user = new User();

//user.setId(101);

user.setUsername("test");

user.setPassword("123456")

user.setEmail("674531003@qq.com");

userDAO.insert(user);

相当于:

insert into user(ID,username,password,email) values(101,'test','123456','674531003@qq.com');

5、insertSelective===>插入数据

int insertSelective(Account record);

6、selectByExample===>根据条件查询数据

List selectByExample(AccountExample example);

//下面是一个完整的案例

UserExample example = new UserExample();

Criteria criteria = example.createCriteria();

criteria.andUsernameEqualTo("joe");

criteria.andUsernameIsNull();

example.setOrderByClause("username asc,email desc");

List>list = userDAO.selectByExample(example);

相当于:select * from user where username = 'joe' and username is null order by username asc,email desc

//注:在iBator 生成的文件UserExample.java中包含一个static 的内部类 Criteria ,在Criteria中有很多方法,主要是定义SQL 语句where后的查询条件。

7、selectByPrimaryKey===>根据主键查询数据

Account selectByPrimaryKey(Integer id);//相当于select * from user where id = 变量id

8、updateByExampleSelective===&

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值