xml版本查看:https://www.cnblogs.com/binz/p/6564490.html
springboot3.x以前的版本查看 https://www.cnblogs.com/binz/p/17421063.html
springboot3.x查看 https://www.cnblogs.com/binz/p/17654403.html
1、pom引用
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.2</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <artifactId>spring-boot-starter-tomcat</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions> </dependency> <!-- web 容器使用 undertow 性能更强 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.4.7</version> </dependency>
<!-- 以前的tkmapper 不支持springboot3.x -->
<!-- 具体查看https://mapper.mybatis.io/--> <dependency> <groupId>io.mybatis</groupId> <artifactId>mybatis-mapper</artifactId> <version>2.1.1</version> </dependency>
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.2</version> </dependency> </dependencies>
2、新建自己的BaseMapper
import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; import java.util.Optional; import org.apache.ibatis.session.RowBounds; import com.github.pagehelper.Page; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ReflectUtil; import io.mybatis.mapper.Mapper; import io.mybatis.mapper.example.Example; import io.mybatis.mapper.example.LambdaExampleWrapper; public interface BaseMapper<T> extends Mapper<T,Long>{ List<T> selectList(T entity, RowBounds rowBounds); default T getById(Long id) { return selectByPrimaryKey(id).orElse(null); } default <RE extends BaseSerializable> RE getById(Long id,Class<RE> returnClass) { T t = getById(id); if(t != null) { return covert(t,returnClass); } return null; } default T one(T query) { return selectOne(query).orElse(null); } default <RE extends BaseSerializable> RE one(T query,Class<RE> returnClass) { Optional<T> optional = selectOne(query); if(optional.isPresent()) { T t = optional.get(); return covert(t,returnClass); } return null; } default <RE extends BaseSerializable> List<RE> select(T t,Class<RE> returnClass) { List<T> ts = selectList(t); return covertList(ts,returnClass); } default <RE extends BaseSerializable> Page<RE> selectPage(T t,Class<RE> returnClass) { Page<T> ts = (Page<T>) selectList(t); return (Page<RE>) covertList(ts,returnClass); } default <RE extends BaseSerializable> Page<RE> selectPageByExample(Example<T> example,Class<RE> returnClass) { Page<T> ts = (Page<T>) selectByExample(example); return (Page<RE>) covertList(ts,returnClass); } default <RE extends BaseSerializable> List<RE> selectByExample(Example<T> example,Class<RE> returnClass) { List<T> ts = selectByExample(example); return covertList(ts,returnClass); } default <RE extends BaseSerializable> RE selectOneByExample(Example<T> example,Class<RE> returnClass) { Optional<T> optional = selectOneByExample(example); if(optional.isPresent()) { T t = optional.get(); return covert(t,returnClass); } return null; } default <RE extends BaseSerializable> RE selectOneByExampleLimitOne(Example<T> example,Class<RE> returnClass) { T t = selectOneByExampleLimitOne(example); if(t != null) { return covert(t, returnClass); } return null; } default T selectOneByExampleLimitOne(Example<T> example) { RowBounds rowBounds = new RowBounds(0, 1); List<T> ts = selectByExample(example, rowBounds); if(ObjectUtil.isNotEmpty(ts)) { return ts.get(0); } return null; } default T selectOneByLimitOne(T t) { RowBounds rowBounds = new RowBounds(0, 1); List<T> ts = selectList(t,rowBounds); if(ObjectUtil.isNotEmpty(ts)) { return ts.get(0); } return null; } @SuppressWarnings("unchecked") default Class<T> thisTClass() { Class<?> class1 = getClass(); Class<?> interfaces = class1.getInterfaces()[0]; Type[] genericInterfaces = interfaces.getGenericInterfaces(); Type type = genericInterfaces[0]; if( type instanceof ParameterizedType){ ParameterizedType pType = (ParameterizedType) type; Type clazz = pType.getActualTypeArguments()[0]; if( clazz instanceof Class<?> ){ return (Class<T>) clazz; } } return null; } default <RE extends BaseSerializable> List<RE> covertList(List<T> ts,Class<RE> returnClass){ List<RE> responses; if(ts instanceof Page) { responses = new Page<>(); }else { responses = new ArrayList<>(); } for (T t : ts) { responses.add(covert(t,returnClass)); } return responses; } default <RE extends BaseSerializable> RE covert(T t , Class<RE> returnClass) { if(t != null) { RE response = null; try { response = ReflectUtil.newInstanceIfPossible(returnClass); BeanUtil.copy(t, response); } catch (Exception e) { e.printStackTrace(); } return response; } return null; } //自带的wrapper()个人觉得还缺点东西,就自己复制了一份出来微调了,根据情况使用,不需要就删除,需要对应的代码在下方 default LambdaExampleWrapper<T> lambdaWrapper() { return new LambdaExampleWrapper<>(BaseMapper.this, example()); } }View Code
3、新建表模型,注意注解使用和以前版本有区别,但是兼容一些之前javax.persistence的一些基本注解
import java.util.Date; import io.mybatis.provider.Entity; import lombok.Data; @Data @Entity.Table("system_user") public class User { @Entity.Column(id = true,insertable = false,updatable = false) private Long id; /** * 姓名 */ private String realname; /** * 手机号 */ private String mobile; /** * 密码 */ private String password; /** * 身份证号 */ private String idcard; /** * 头像 */ private String avatar; /** * 最后登录时间 */ private String lastLoginIp; /** * 最后登录时间 */ private Date lastLoginTime; /** * 创建人 */ private Long createBy; /** * 创建时间 */ private Date createTime; /** * 修改人 */ private Long updateBy; /** * update_time */ private Date updateTime; }View Code
4、创建对应业务的mapper继承BaseMapper
import com.xxx.core.base.BaseMapper; import com.xxx.system.model.User; public interface UserMapper extends BaseMapper<User>{ }
5、启动类扫描自己的mapper目录 @MapperScan
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.annotation.EnableScheduling; @EnableCaching @EnableScheduling @SpringBootApplication @MapperScan(basePackages="com.xxx.*.mapper") @ComponentScan(basePackages = { "com.xxx.*.config", "com.xxx.*.controller", "com.xxx.*.job" }) public class OperateApiStarted { public static void main(String[] args) { SpringApplication.run(OperateApiStarted.class, args); } }
标签:mapper,return,default,ts,springboot3,import,mybatis,returnClass,Class From: https://www.cnblogs.com/binz/p/17654403.html