tk.mybatis
程序员文章站
2022-06-19 11:38:06
1.依赖tk.mybatis mapper-spring-boot-starter 2.1.5
1.依赖
<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
2.mapper
import tk.mybatis.mapper.common.Mapper;
public interface CommonMapper<T> extends Mapper<T>{
}
3.service
3.1.CommonService
import java.util.List;
public interface CommonService<T> {
/**
* 插入(null也会插入)
* @param model
* @return
*/
int insert(T model);
/**
* 插入(null)不会插入
* @param model
* @return
*/
int insertSelective(T model);
int delete(T model);
/**
* 更新null
* @param model
* @return
*/
int update(T model);
/**
* 不更新null
* @param model
* @return
*/
int updateSelective(T model);
T one(T model);
T one(Integer key);
List<T> list();
List<T> list(T model);
}
3.2.CommonServiceImpl
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gr.common.CommonMapper;
import com.gr.common.CommonService;
@Service
public class CommonServiceImpl<T> implements CommonService<T>{
@Autowired
private CommonMapper<T> mapper;
@Override
public int insert(T model) {
return mapper.insert(model);
}
@Override
public int insertSelective(T model) {
return mapper.insertSelective(model);
}
@Override
public int delete(T model) {
return mapper.delete(model);
}
@Override
public int update(T model) {
return mapper.updateByPrimaryKey(model);
}
@Override
public int updateSelective(T model) {
return mapper.updateByPrimaryKeySelective(model);
}
@Override
public T one(T model) {
return mapper.selectOne(model);
}
@Override
public T one(Integer key) {
return mapper.selectByPrimaryKey(key);
}
@Override
public List<T> list() {
return mapper.selectAll();
}
@Override
public List<T> list(T model) {
return mapper.select(model);
}
}
4.启动类
从tk.mybatis.spring.annotation.MapperScan引入@MapperScan("com.xx.mapper")注解
5.业务mapper
import com.gr.common.CommonMapper;
import com.gr.entity.Members;
public interface MembersMapper extends CommonMapper<Members>{
}
6.业务service
6.1.接口
import com.gr.common.CommonService;
import com.gr.entity.Members;
public interface MembersService extends CommonService<Members>{
}
6.2.实现类
import org.springframework.stereotype.Service;
import com.gr.common.impl.CommonServiceImpl;
import com.gr.entity.Members;
import com.gr.service.MembersService;
@Service
public class MembersServiceImpl extends CommonServiceImpl<Members> implements MembersService{
}
7.controller
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gr.service.MembersService;
@RestController
@RequestMapping("/qrcode/members")
public class MembersController {
@Autowired
private MembersService membersService;
@RequestMapping("/list")
public Map<String, Object> list(){
Map<String, Object> map = new HashMap<String, Object>();
map.put("list", membersService.list());
return map;
}
}
8.pagehelper
8.1.依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
8.2.实现
@RequestMapping("/list")
public Map<String, Object> list(Integer pageNo, Integer pageSize){
PageHelper.startPage(pageNo,pageSize);
Map<String, Object> map = new HashMap<String, Object>();
PageInfo<Members> pageInfo = new PageInfo<>(membersService.list());
map.put("list", pageInfo);
map.put("code", 200);
return map;
}
9.结束
本文地址:https://blog.csdn.net/qq_42130732/article/details/111996204