基本信息
源码名称:通用Mapper使用手册
源码大小:0.22M
文件格式:.zip
开发语言:Java
更新时间:2025-04-27
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

使用Mybatis的开发者,大多数都会遇到一个问题,就是要写大量的SQL在xml文件中,除了特殊的业务
逻辑SQL之外,还有大量结构类似的增删改查SQL。而且,当数据库表结构改动时,对应的所有SQL以及
实体类都需要更改。这工作量和效率的影响或许就是区别增删改查程序员和真正程序员的屏障。这时,
通用Mapper便应运而生。
通用Mapper就是为了解决单表增删改查,基于Mybatis的插件。开发人员不需要编写SQL,不需要在
DAO中增加方法,只要写好实体类,就能支持相应的增删改查方法

import com.mrcoder.sbmcommonmapper.mapper.PersonMapper;
import com.mrcoder.sbmcommonmapper.model.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;
import java.util.ArrayList;
import java.util.List;
@RestController
public class CommonMapperController {
@Autowired
private PersonMapper personMapper;
@GetMapping("/getList")
public List<Person> getList() {
//通用mapper的获取列表方法
return personMapper.selectAll();
}
@GetMapping("/getById/{id}")
public Person getById(@PathVariable("id") Integer id) {
//通用mapper的根据主键获取指定记录方法
return personMapper.selectByPrimaryKey(id);
}
@GetMapping("/insert")3.7、打印SQL
logback-spring.xml中加入如下语句:
public int insert() {
Person person = new Person();
person.setAge(10);
person.setName("p1");
//插入方法
return personMapper.insert(person);
}
@GetMapping("/update")
public int update() {
Person person = new Person();
person.setId(1);
person.setAge(10);
person.setName("p1-update");
//更新方法
return personMapper.updateByPrimaryKeySelective(person);
}
@GetMapping("/batchInsert")
public int batchInsert() {
Person p1 = new Person();
p1.setAge(10);
p1.setName("p2");
Person p2 = new Person();
p2.setAge(10);
p2.setName("p3");
List<Person> personList = new ArrayList<Person>();
personList.add(p1);
personList.add(p2);
//批量插入方法
return personMapper.insertList(personList);
}
@GetMapping("/query")
public List<Person> query() {
Example con = new Example(Person.class);
Example.Criteria criteria = con.createCriteria();
criteria.andEqualTo("age", 10);
//复杂条件查询方法
return personMapper.selectByExample(con);
}
}