场景分析
在MySQL中如果要检查某个字段的值是否在指定区间,或许有些伙伴会想到check约束,但是在MySQL中check约束是无效的,所以该篇博客将要解决在MySQL中check无效的问题
解决方案
因为MySQL无法使用check约束,那么只能在业务逻辑中自己编码来实现约束
1、首先模拟出一张数据表,方便测试
mysql> use sms;
Database changed
mysql> select * from student;
+----+------+----------+--------+------+-------+
| id | num | password | name | age | score |
+----+------+----------+--------+------+-------+
| 1 | 0001 | 111 | pink | 21 | 98 |
| 2 | 11 | 11 | ee | 12 | -45 |
| 3 | 0004 | 123 | linjie | 20 | 99 |
| 4 | 11 | 11 | ww | 11 | -89 |
| 5 | 007 | 123 | pojp | 23 | 99 |
| 6 | 008 | 123 | trtp | 23 | 99 |
| 7 | 009 | 123 | QQQp | 23 | 99 |
| 8 | 007 | 123 | pojp | 23 | 99 |
| 9 | 008 | 123 | trtp | 23 | 99 |
| 10 | 009 | 123 | QQQp | 23 | 99 |
| 11 | 007 | 123 | pojp | 23 | 99 |
| 12 | 008 | 123 | trtp | 23 | 99 |
| 13 | 009 | 123 | QQQp | 23 | 99 |
| 14 | 007 | 123 | pojp | 23 | 99 |
| 15 | 008 | 123 | trtp | 23 | 99 |
| 16 | 009 | 123 | QQQp | 23 | 99 |
+----+------+----------+--------+------+-------+
2、配置JdbcTemplate
至于你用什么来操作数据库,可自行试验,笔者这里采用JdbcTemplate
.
db.properties
jdbc.user=root
jdbc.password=1111
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///sms
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
applicationContext
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.linjie"></context:component-scan>
<!-- 导入资源文件
读取db.properties文件中的数据 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置C3P0数据源 -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!-- 配置Spring的jdbcTemplate
并注入一个dataSource数据源-->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
3、定义Dao接口和Dao实现类
IUserDao接口
package com.linjie.tx;
/**
* @author LinJie E-mail:xulinjie0105@gmail.com
* @version 创建时间:2018年5月12日 下午12:11:28
* Dao接口
*/
public interface IUserDao {
//修改用户年龄
public void changeAge(int score,int id) ;
}
UserDaoImpl实现类
这里就是解决check无效的核心业务逻辑(笔者对于重要的代码都采用图片贴出)
4、定义分数异常类,实现RuntimeException
package com.linjie.tx;
/**
* @author LinJie E-mail:xulinjie0105@gmail.com
* @version 创建时间:2018年5月12日 下午1:32:55
* 异常类
*/
public class ScoreException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
public ScoreException() {
super();
// TODO Auto-generated constructor stub
}
public ScoreException(String arg0, Throwable arg1, boolean arg2, boolean arg3) {
super(arg0, arg1, arg2, arg3);
// TODO Auto-generated constructor stub
}
public ScoreException(String arg0, Throwable arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
public ScoreException(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
public ScoreException(Throwable arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
}
5、测试(测试数据是将成绩减少200)
package com.linjie.tx;
/**
* @author LinJie E-mail:xulinjie0105@gmail.com
* @version 创建时间:2018年5月12日 下午12:26:53
* 测试
*/
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class txTest {
private ApplicationContext context = null;
private IUserDao iUserDao;
{
context = new ClassPathXmlApplicationContext("applicationContext.xml");
iUserDao = context.getBean(IUserDao.class);
}
@Test
public void test() {
iUserDao.changeAge(200, 1);
}
}
6、结果
可以看出减去200在核心检查逻辑中是不行的,ok,解决check在mysql的无效问题完成
if(selectScore < score) {
throw new ScoreException("分数异常");
}