1、BaseService.java
import catl.mapper.BaseMapper;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
public abstract class BaseService<T, Id>{
@Resource
BaseMapper baseMapper;
public int save(T t) {
return baseMapper.insertSelective(t);
}
public int saveAll(List<T> list) {
return baseMapper.insertList(list);
}
public int delete(T t) {
return baseMapper.delete(t);
}
public int deleteById(Id id) {
return baseMapper.deleteByPrimaryKey(id);
}
public int update(T t) {
return baseMapper.updateByPrimaryKey(t);
}
public T getById(Id id) {
return (T) baseMapper.selectByPrimaryKey(id);
}
public List<T> getAll() {
return baseMapper.selectAll();
}
public List<T> searchByExample(Example example) {
return Collections.emptyList();
}
}
2、BaseMapper.java
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.special.InsertListMapper;
public interface BaseMapper<T> extends Mapper<T>, InsertListMapper<T> {
}
3、UserMapper.java
import catl.dao.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User>{
}
4、UserService.java
import catl.dao.User;
import org.springframework.stereotype.Service;
@Service
public class UserService extends BaseService<User,String> {
}
5、TestController.java
@Autowired
private UserService userService;
@GetMapping("/test")
@ApiOperation(value = "测试接口", httpMethod = "GET")
public RetInfo test(){
User user = new User();
user.setId("222323");
user.setName("ceshi");
userService.save(user);
return RetInfo.success("新增成功");
}
6、pom文件
<dependency>标签:mapper,return,springboot,service,java,baseMapper,改查,import,public From: https://www.cnblogs.com/hjwbkcom/p/16889877.html
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>