Hello大家好,本章我们集成通用 Mapper功能 。有问题可以联系我mr_beany@163.com。另求各路大神指点,感谢
通用 Mapper 是一个可以实现任意 MyBatis 通用方法的框架,项目提供了常规的增删改查操作以及 Example
相关的单表操作。通用 Mapper 是为了解决 MyBatis 使用中 90% 的基本操作,使用它可以很方便的进行开发,可以节省开发人员大量的时间。
<dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency>
这里我们重新配置MapperScannerConfigurer
打开MybatisConfigurer
将import org.mybatis.spring.mapper.MapperScannerConfigurer;修改为
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
创建core→universal文件夹
创建以下文件
1、Mapper
package com.example.demo.core.universal; import tk.mybatis.mapper.common.BaseMapper; import tk.mybatis.mapper.common.ConditionMapper; import tk.mybatis.mapper.common.IdsMapper; import tk.mybatis.mapper.common.special.InsertListMapper; /** * @Description: 定制版MyBatis Mapper插件接口,如需其他接口参考官方文档自行添加。 * @author 张瑶 * @date 2018/4/22 21:15 */ public interface Mapper<T> extends BaseMapper<T>, ConditionMapper<T>, IdsMapper<T>, InsertListMapper<T> { }
2、Service
package com.example.demo.core.universal; import org.apache.ibatis.exceptions.TooManyResultsException; import tk.mybatis.mapper.entity.Condition; import java.util.List; /** * @author 张瑶 * @Description: Service层基础接口,其他Service接口请继承该接口 * @date 2018/4/18 11:25 */ public interface Service<T> { /** * @param model * @Description: 持久化 * @Reutrn Integer */ Integer insert(T model); /** * @param id * @Description: 通过主鍵刪除 * @Reutrn Integer */ Integer deleteById(String id); /** * @param ids * @Description: 批量刪除 eg:ids -> “1,2,3,4” * @Reutrn Integer */ Integer deleteByIds(String ids); /** * @param model * @Description: 更新 * @Reutrn Integer */ Integer update(T model); /** * @param id * @Description: 通过ID查找 * @Reutrn T */ T selectById(String id); /** * @param fieldName * @param value * @throws TooManyResultsException * @Description: 通过Model中某个成员变量名称(非数据表中column的名称)查找,value需符合unique约束 * @Reutrn T */ T selectBy(String fieldName, Object value) throws TooManyResultsException; /** * @param fieldName javabean定义的属性名,不是数据库里的属性名 * @param value * @Description: 通过Model中某个成员变量名称(非数据表中column的名称)查找 * @Reutrn List<T> */ List<T> selectListBy(String fieldName, Object value); /** * @param ids * @Description: 通过多个ID查找//eg:ids -> “1,2,3,4” * @Reutrn List<T> */ List<T> selectListByIds(String ids); /** * @param condition * @Description: 根据条件查找 * @Reutrn List<T> */ List<T> selectByCondition(Condition condition); /** * @Description: 获取所有 * @Reutrn List<T> */ List<T> selectAll(); /** * @param record * @return List<T> * @Description: 根据实体中的属性值进行查询,查询条件使用等号 */ List<T> select(T record); /** * @param record * @return T * @Description: 根据实体中的属性值进行查询,查询条件使用等号 */ T selectOne(T record); }
3、AbsratctService
package com.example.demo.core.universal; import com.example.demo.core.ret.ServiceException; import org.apache.ibatis.exceptions.TooManyResultsException; import org.springframework.beans.factory.annotation.Autowired; import tk.mybatis.mapper.entity.Condition; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.util.List; /** * @Description: 基于通用MyBatis Mapper插件的Service接口的实现 * @author 张瑶 * @date 2018/4/18 11:28 */ public abstract class AbstractService<T> implements Service<T> { @Autowired protected Mapper<T> mapper; private Class<T> modelClass; // 当前泛型真实类型的Class @SuppressWarnings("unchecked") public AbstractService() { ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); modelClass = (Class<T>) pt.getActualTypeArguments()[0]; } @Override public Integer insert(T model) { return mapper.insertSelective(model); } @Override public Integer deleteById(String id) { return mapper.deleteByPrimaryKey(id); } @Override public Integer deleteByIds(String ids) { return mapper.deleteByIds(ids); } @Override public Integer update(T model) { return mapper.updateByPrimaryKeySelective(model); } @Override public T selectById(String id) { return mapper.selectByPrimaryKey(id); } @Override public T selectBy(String fieldName, Object value) throws TooManyResultsException { try { T model = modelClass.newInstance(); Field field = modelClass.getDeclaredField(fieldName); field.setAccessible(true); field.set(model, value); return mapper.selectOne(model); } catch (ReflectiveOperationException e) { throw new ServiceException(e.getMessage(), e); } } @Override public List<T> selectListBy(String fieldName, Object value) { try { T model = modelClass.newInstance(); Field field = modelClass.getDeclaredField(fieldName); field.setAccessible(true); field.set(model, value); return mapper.select(model); } catch (ReflectiveOperationException e) { throw new ServiceException(e.getMessage(), e); } } @Override public List<T> selectListByIds(String ids) { return mapper.selectByIds(ids); } @Override public List<T> selectByCondition(Condition condition) { return mapper.selectByCondition(condition); } @Override public List<T> selectAll() { return mapper.selectAll(); } @Override public List<T> select(T record){ return mapper.select(record); } @Override public T selectOne(T recoed){ return mapper.selectOne(recoed); } }
在 application.properties
中添加
mapper.mappers=com.example.demo.core.universal.Mapper
注意,之前user_info表主键为int类型,现修改为varchar;
注意,使用通用mapper需要在实体类的id加上 @Id
1、UserInfo
package com.example.demo.model; import javax.persistence.Column; import javax.persistence.Id; /** * @author 张瑶 * @Description: * @time 2018/4/18 11:55 */ public class UserInfo { /** * 主键 */ @Id private String id; /** * 用户名 */ @Column(name = "user_name") private String userName; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
2、UserInfoMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.dao.UserInfoMapper"> <resultMap id="BaseResultMap" type="com.example.demo.model.UserInfo"> <id column="id" jdbcType="VARCHAR" property="id"/> <result column="user_name" jdbcType="VARCHAR" property="userName"/> </resultMap> <sql id="Base_Column_List"> id,user_name </sql> </mapper>
3、UserInfoMapper
package com.example.demo.dao; import com.example.demo.core.universal.Mapper; import com.example.demo.model.UserInfo; /** * @author 张瑶 * @Description: * @time 2018/4/18 11:54 */ public interface UserInfoMapper extends Mapper<UserInfo>{ }
4、UserInfoService
package com.example.demo.service; import com.example.demo.core.universal.Service; import com.example.demo.model.UserInfo; /** * @author 张瑶 * @Description: * @time 2018/4/18 11:56 */ public interface UserInfoService extends Service<UserInfo>{ }
5、UserInfoServiceImpl
package com.example.demo.service.impl; import com.example.demo.core.universal.AbstractService; import com.example.demo.core.ret.ServiceException; import com.example.demo.dao.UserInfoMapper; import com.example.demo.model.UserInfo; import com.example.demo.service.UserInfoService; import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * @author 张瑶 * @Description: * @time 2018/4/18 11:56 */ @Service public class UserInfoServiceImpl extends AbstractService<UserInfo> implements UserInfoService{ @Resource private UserInfoMapper userInfoMapper; @Override public UserInfo selectById(String id){ UserInfo userInfo = userInfoMapper.selectByPrimaryKey(id); if(userInfo == null){ throw new ServiceException("暂无该用户"); } return userInfo; } }
6、UserInfoController
package com.example.demo.controller; import com.example.demo.core.ret.RetResponse; import com.example.demo.core.ret.RetResult; import com.example.demo.model.UserInfo; import com.example.demo.service.UserInfoService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; /** * @author 张瑶 * @Description: * @time 2018/4/18 11:39 */ @RestController @RequestMapping("userInfo") @Api(tags = {"用户操作接口"}, description = "userInfoControler") public class UserInfoController { @Resource private UserInfoService userInfoService; @PostMapping("/hello") public String hello() { return "hello SpringBoot"; } @ApiOperation(value = "查询用户", notes = "根据用户ID查询用户") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "query") }) @PostMapping("/selectById") public RetResult<UserInfo> selectById(@RequestParam String id) { UserInfo userInfo = userInfoService.selectById(id); return RetResponse.makeOKRsp(userInfo); } @PostMapping("/testException") public RetResult<UserInfo> testException(String id) { List a = null; a.size(); UserInfo userInfo = userInfoService.selectById(id); return RetResponse.makeOKRsp(userInfo); } @ApiOperation(value = "查询用户", notes = "分页查询用户所有") @ApiImplicitParams({ @ApiImplicitParam(name = "page", value = "当前页码", dataType = "Integer", paramType = "query"), @ApiImplicitParam(name = "size", value = "每页显示条数", dataType = "Integer", paramType = "query") }) @PostMapping("/selectAll") public RetResult<PageInfo<UserInfo>> selectAll(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) { PageHelper.startPage(page,size); List<UserInfo> userInfoList = userInfoService.selectAll(); PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfoList); return RetResponse.makeOKRsp(pageInfo); } }
ok,在没写sql的情况下,功能依旧好使
gitee.com/beany/mySpr…
写文章不易,如对您有帮助,请帮忙点下star
集成通用 Mapper功能已完成,后续功能接下来陆续更新,有问题可以联系我mr_beany@163.com。另求各路大神指点,感谢大家。