用户工具

站点工具


java:mybatis
  • BaseDO
@Data
@SuppressWarnings("serial")
public class BaseDO implements Serializable {
	/** 由com.baomidou.mybatisplus.mapper.LogicSqlInjector自动维护 */
	@TableLogic
	private boolean deleted;
	/** 由MyBatisConfig.optimisticLockerInterceptor自动维护 */
	@Version
	private int version;
	/** 由MySQL自动维护 */
	private Date createAt;
	private Date updateAt;
	/** 由LogAspect.logMapper自动维护 */
	private Long createBy;
	private Long updateBy;
}
  • DemoBaseDO
@Data
@SuppressWarnings("serial")
@TableName("app_demo_base")
@EqualsAndHashCode(callSuper=true) 
public class DemoBaseDO extends BaseDO {
    /** 默认ID_WORKER,mybatis-plus.global-config.id-type:2,应用可以自定义 */
    @TableId
    private Long id;
    /** 标题 */
    private String title;
    /** 发布时间 */
    private Date publish;
    /** 正文 */
    private String content;
    /** 价格 */
    private BigDecimal price;
}
  • mybatis-plus
mybatis-plus:
  mapper-locations: classpath:/mapper/**/*Mapper.xml
  typeAliasesPackage: com.ifast.**.domain
  global-config:
    id-type: 2
    field-strategy: 1
    db-column-underline: false
    capital-mode: false
    sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector
    logic-not-delete-value: 0
    logic-delete-value: 1
  configuration:
    map-underscore-to-camel-case: false
    cache-enabled: false
  • MybatisConfig
@Configuration
public class MyBatisConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
    	return new OptimisticLockerInterceptor();
    }
}
  • Wrapper

Mapper,让mapper.xml不再必须,提供CRUD操作,类似Dao接口
支持:insert,updateById(user),selectById,deleteById

  public interface UserMapper extends BaseMapper<User> { }
  selectList(new EntityWrapper<User>().eq("sex","0").between("age", "18", "50"))
  selectPage(new Page<User>(1,10), new EntityWrapper<User>().eq("name", "张三"))
  Wrapper.like(StringUtils.isNotEmpty(title), "title", title);  //condition, field, value
  • resultType
string:String、List<String>
hashmap、java.util.HashMap:Map<String, Object>、List<Map<String, Object>>
com.bean.Student:Student、List<Student>
<typeAliases>
  <typeAlias type="com.bean.Student" alias="student"/>
  <package name="包名"/> //批量扫描包,别名为类名小写
</typeAliases>
  • where条件共用于count和page查询
<sql id="userWhere"><if test="params != null"><where>
  <if test="params.mobile != null and params.mobile != ''">AND tubi.username LIKE CONCAT('%',#{params.mobile},'%')</if>
</where></if></sql>
<if test="user.status == 0">AND u.status in ('0','2')</if>
<include refid="userWhere"/>
java/mybatis.txt · 最后更改: 2018/09/14 13:54 由 admin