Skip to content
网游世界
网游世界

吾生有涯,而知无涯。

  • 首页
  • PHP
    • ThinkPHP
    • FastAdmin
    • webman
  • JavaScript
    • jQuery
    • AdminLTE
  • Free Pascal
  • Java
    • JeeSite
    • 若依
    • ruoyi-vue-pro
  • 其它
    • 操作系统
    • 树莓派
    • 前端
    • Null
  • 关于
网游世界

吾生有涯,而知无涯。

JeeSite 验证数据唯一

3Vshej, 2023年12月29日 周五2023年12月29日 周五

在日常开发中,最常用的是数据唯一验证;如客户名称不能重复。因为,自己水平有限,在 JeeSite 中暂未找到合适的方法。

这里介绍一种笨的方法,通过在 mybatis 中增加查询 SQL来实现:

如,在 CustomerDao.xml 文件增加 getByCustomerName:

<?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.jeesite.modules.customer.dao.CustomerDao">
    
    <!-- 查询数据
    <select id="findList" resultType="Customer">
        SELECT ${sqlMap.column.toSql()}
        FROM ${sqlMap.table.toSql()}
        <where>
            ${sqlMap.where.toSql()}
        </where>
        ORDER BY ${sqlMap.order.toSql()}
    </select> -->

    <select id="getByCustomerName" resultType="Customer">
        SELECT
            *
        FROM
            `basic_customer` a
        where a.customer_name = #{customerName}
        LIMIT 1
    </select>
</mapper>

在 CustomerDao.java 中增加:

Customer getByCustomerName(String customerName);

在服务层 CustomerService.java 中增加:

@Autowired
private CustomerDao customerDao;

public Customer getByCustomerName(String customerName) {
    return customerDao.getByCustomerName(customerName);
}


/**
 * 保存数据(插入或更新)
 *
 * @param customer
 */
@Override
@Transactional
public void save(Customer customer) {

    Customer tmp = getByCustomerName(customer.getCustomerName());

    if (customer.getIsNewRecord()) {
        if (tmp != null) {
            throw new RuntimeException("客户名称已存在");
        }
    } else {
        if (tmp != null && !tmp.getId().equals(customer.getId())) {
            throw new RuntimeException("客户名称已存在");
        }
    }

    super.save(customer);
}

在控制器层 CustomerController.java 中修改

/**
 * 保存数据
 */
@RequiresPermissions("customer:customer:edit")
@PostMapping(value = "save")
@ResponseBody
public String save(@Validated Customer customer) {

    try {
        customerService.save(customer);
        return renderResult(Global.TRUE, text("保存客户信息成功!"));
    } catch (RuntimeException e) {
        return renderResult(Global.FALSE, text(e.getMessage()));
    }

}

 

相关文章:

  1. JeeSite 中增加手机固话验证 在 Form 页面输入框类中,增加 contact_phone。...
  2. JeeSite 后端数据验证 验证数据的有效性可以通过前端进行过滤,后端再进行验证。...
  3. JeeSite 缓存数据 CacheUtils 是 JeeSite 中的一个缓存工具类,用于操作全局缓存数据。而 SysCac......
  4. JeeSite 报表设计器使用说明 基于 Ureport2 在 Spring 之上纯 Java 的高性能报表引擎,通过迭代单元格可以实现......
Java JeeSite JeeSite 框架验证

文章导航

Previous post
Next post

近期文章

  • Android Studio Gradle 配置国内镜像
  • 为什么重新发明轮子
  • ruoyi-vue-pro 匿名访问
  • VUE 中接收 code 异常
  • 关于 AI

归档

  • 2025 年 4 月
  • 2025 年 3 月
  • 2025 年 2 月
  • 2025 年 1 月
  • 2024 年 12 月
  • 2024 年 11 月
  • 2024 年 10 月
  • 2024 年 9 月
  • 2024 年 8 月
  • 2024 年 7 月
  • 2024 年 6 月
  • 2024 年 5 月
  • 2024 年 4 月
  • 2024 年 3 月
  • 2024 年 2 月
  • 2024 年 1 月
  • 2023 年 12 月
除非特殊说明,本站作品采用知识共享署名 4.0 国际许可协议进行许可。
豫公网安备 41010402002622号 豫ICP备2020029609号-3
©2025 3Vshej