Use MyBatis and Spring Boot Plus in some common operations

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/winnershili/article/details/87201330

nonsense

CSDN today's navigation bar can be really happy, ah,

Red bar, as well as the Chinese style shades look great!

JPA by wind and water before use, so think of this family of MyBatis next door look! 

The two camps are not too much to do anything directly on the code evaluation

 

table

Some readers will ask, how there is this comment Entity 

I'm just too lazy to build the table .. use JPA to automatically create a table you can also ignore the following table JPA annotations are built with only @TableId is today's hero

The last field is the surface property of the link JPA no longer appearing in the database using @Transient MyBatisPlus used TableFiled (exist = false)

However, there is no case here and tombstone 

@Entity
public class SysUserToken implements Serializable {

	private static final long serialVersionUID = -638877678952488002L;

	/**编号 */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @TableId(type = IdType.AUTO)
    private Long id;
    
    /**用户ID */
    private String userId;
    
    /**生成的token */
	private String token;
	
	/**过期时间 */
	@Temporal(TemporalType.TIMESTAMP)
	private Date expireTime;
	
	/**更新时间 */
	@Temporal(TemporalType.TIMESTAMP)
	private Date updateTime;
	
	@TableField(exist = false)
	@Transient
	private String link;

    //省略 get set 方法
	
}

Inquire

1. If you use this MyBatis xml thing if you if there is no code generation!

I write a large-scale system is expected to be exhausted it, look at the results I want it, first take this most ancient way 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.avicsafety.webapp.base.mapper.UserTokenMapper">
	<resultMap id="userMap" type="com.avicsafety.webapp.base.entity.SysUserToken" >
	      <id property="id" column="id" javaType="java.lang.Long"></id>
	      <result property="userId" column="user_id" javaType="java.lang.String"/>
	      <result property="token" column="token" javaType="java.lang.String"/>
	</resultMap>
	<select id="findListByUserId" resultMap="userMap" parameterType="java.lang.String">
	        select * from sys_user_token where user_id=#{id}
	</select>
</mapper>
public interface UserTokenMapper extends BaseMapper<SysUserToken> {

	List<SysUserToken> findListByUserId(String uuid);

}

 

List<SysUserToken> list = userTokenMapper.findListByUserId("9fd8b5ad663718060f77136e65329f4f");

A place where the wrong time will run error, so do not hesitate to use

Finally, plus scan path in the configuration

mybatis-plus.mapper-locations: classpath:mapper/base/*.xml

2. Look at annocation query implementation of

	@Select("SELECT id,user_id FROM sys_user_token WHERE token = #{token}")
	@Results({@Result(property = "link", column = "user_id")})
	List<SysUserToken> findListByToken(@Param("token") String token);
        List<SysUserToken> list = userTokenMapper.findListByToken("9fd8b5ad663718060f77136e65329f4f");
        list.forEach(sysUserToken -> System.out.println(sysUserToken.getId()+"::"+sysUserToken.getLink()));

He only needs to add a comment at the top of the interface, and instantly feels a bit like the JPA there

You do not have to configure this xml feel comfortable point

 

And then on top of the foundation up a paging query

@Select("SELECT * FROM sys_user_token WHERE token = #{token} and user_id = #{user_id}")
	List<SysUserToken> findPageByUserIdAndToken(Page<SysUserToken> page,  @Param("token") String token, @Param("user_id") String user_id);
        Page<SysUserToken> page = new Page<SysUserToken>(1,20);
    	List<SysUserToken> list = userTokenMapper.findPageByUserIdAndToken(page,"9fd8b5ad663718060f77136e65329f4f","9fd8b5ad663718060f77136e65329f4f");
        list.forEach(sysUserToken -> System.out.println(sysUserToken.getId()+"::"+sysUserToken.getLink()));

Do not forget to add MP configuration

    /**
     * 分页插件配置
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

3. Finally, take a look at MyBatisPlus recommended implementation method, the Mp attentive ah 

QueryWrapper<SysUserToken> queryWrapper= new QueryWrapper<SysUserToken>().eq("user_id", "9fd8b5ad663718060f77136e65329f4f").eq("token", "9fd8b5ad663718060f77136e65329f4f");
        userTokenMapper.selectObjs(queryWrapper);

 

Wherein  QueryWrapper  is a very strong condition query construction means has N number of methods may be used

Very easy to use, but did not comment flexible ah! But has been able to meet more than 60% of the ninja scenes're welcome SQL slut wise part of it

 

And Page configuration page is super simple and JPA objects are met recommended Pageable

        QueryWrapper<SysUserToken> queryWrapper= new QueryWrapper<SysUserToken>().eq("user_id", "9fd8b5ad663718060f77136e65329f4f").eq("token", "9fd8b5ad663718060f77136e65329f4f");
        Page<SysUserToken> page = new Page<SysUserToken>(1,20);
        IPage<SysUserToken> list = userTokenMapper.selectPage(page, queryWrapper);
        list.getRecords().forEach(System.out::println);

 

Tombstone Configuration

 

mybatis-plus.global-config..db-config.sql-injector=com.baomidou.mybatisplus.mapper.LogicSqlInjector
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0

 

    /**
     * 逻辑删除配置
     */
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }

Add comments directly in the entity, all methods are logically deleted, then I really do Guards ah JPA tombstone really changed a lot of things

	@TableLogic
	private Boolean del = false;

Display configuration SQL statements

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

 

 

Guess you like

Origin blog.csdn.net/winnershili/article/details/87201330