Mybatis_plus

自动填充公共字段

当数据库表中有许多公共字段 比如创建时间 更新时间... 在实体类的属性前加入@TableField注解

fill = FieldFill.INSERT表示在添加的时候需要填充

fill = FieldFill.INSERT_UPDATE表示添加和更新的时候都需要填充

实体类


@Data
public class Employee implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    private String username;

    private String name;

    private String password;

    private String phone;

    private String sex;
    //id_number---->idNumber
    private String idNumber;//使用驼峰命名法

    private Integer status;

    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    @TableField(fill = FieldFill.INSERT)
    private Long createUser;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateUser;

}

使用自定义SQL语句

在Mapper层使用@select注解自定义SQL语句

填充字段用${数据库字段名}(并不是实体类映射的字段名)

Mapper层




@Mapper
public interface DishMapper extends BaseMapper<Dish> {
    @Select("SELECT  DISTINCT" +
            " dish_flavor.id" +
            " FROM" +
            " dish_flavor,dish" +
            " WHERE" +
            " dish_flavor.dish_id = ${id}")
    List<Long> selectBydishAndDishflavor(@Param("id") Long  id);


}

配置文件

server:
  port: 8080
spring:
  application:
    name: 项目名
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:端口(3306)/数据库名?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
      username: xxxx
      password: xxxx
  redis:
    host: 192.168.xxx.xxx
    port: xxxx
    password: xxxx
    database: 0  #使用第几个表
mybatis-plus:
  configuration:
    #在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
      id-type: ASSIGN_ID

调用


@Service
@Slf4j
public class DishImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

   
    @Autowired
    private  DishMapper dishMapper;
   

    @Override
    public void moveByIds(List<Long> ids) {
        this.removeByIds(ids);
        for (Long id : ids) {
            List<Long> dishes = dishMapper.selectBydishAndDishflavor(id);
            dishFlavorService.removeByIds(dishes);
        }
    }


}

拦截器


                                          // 作用范围
@WebFilter(filterName = "LoginCheckFilter",urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {
    public static final AntPathMatcher  PATH_MATCHER=new AntPathMatcher();

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        HttpServletResponse response= (HttpServletResponse) servletResponse;
        log.info("拦截到了 {}",request.getRequestURI());
        //放行url 目录
        String[] strings=new String[]{"/employee/login","/employee/logout",
                "/backend/**","/front/**","/common/**",
                "/user/login"
        };
        //查看url
        String requestURI = request.getRequestURI();
        //判断是否需要拦截
        //不需要拦截 放行
        Long  emyId= (Long) request.getSession().getAttribute("Employee");
        Long userId=(Long) request.getSession().getAttribute("User");
        //在线程放入id
        BaseContext.setThreadLocal(emyId);
        BaseContext.setThreadLocal(userId);
        if(check(strings,requestURI))
        {
            log.info("放行{}",requestURI);
            filterChain.doFilter(request,response);
            return ;
        }
        // 拦截 判断登录状态
        //判断员工
        if (request.getSession().getAttribute("Employee")!=null)
        {
            log.info("已登录放行");
            filterChain.doFilter(request,response);
            return ;
        }
        // 判断前端用户
        if (request.getSession().getAttribute("User")!=null)
        {
            log.info("已登录放行");
            filterChain.doFilter(request,response);
            return ;
        }
        //这里后端没实现拦截 而是将数据通过浏览器响应发送给前端的拦截器判断是否放行
        log.info("拦截到了 {}",request.getRequestURI());
        log.info("未登录 跳转页面");
        log.info((String) request.getSession().getAttribute("Employee"));

        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));



    }
    boolean check(String[] strings,String requestURI)
    {
        for (String item : strings) {
            if(PATH_MATCHER.match(item,requestURI)) return true;
        }
        return false;
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值