We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
继承TypeHandler编写类型转换代码,测试中一直不生效
javaType和jdbcType相同的情况下,后面的会覆盖前面的TypeHandler,也可以理解相同的javaTypeh和jdbcType只能有一个TypeHandler
那么像覆盖默认的TypeHandler不要设置jdbcType即可
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--属性加载--> <properties resource="config.properties"> </properties> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <!--别名配置,如果使用包默认为类名--> <typeAliases> <package name="com.liuyt.beans"/> </typeAliases> <typeHandlers> <typeHandler handler="com.liuyt.utils.BooleanTypeHandler" javaType="Boolean" jdbcType="NUMERIC" /> </typeHandlers> <!--环境配置,default为默认制定的环境--> <environments default="develoment"> <!--环境配置--> <environment id="develoment"> <!--事务类型--> <!--JDBC:这种机制就是利用java.sql.Connection对象完成对事务的提交--> <!--MANAGED:这种机制mybatis自身不会去实现事务管理,而是让程序的Web容器或者Spring容器来实现对事务的管理。--> <!--简单理解JDBC自动commit,MANAGEND手动commit--> <transactionManager type="JDBC"></transactionManager> <!--数据类型--> <!--JNDI:从配置好的JNDI数据源获取连接,一般用于生产--> <!--POOLED:mybatis创建连接池,一般用于开发测试--> <!--UNPOOLED:每个数据库操作都创建连接,并关闭它,基本没用--> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="jdbc:mysql://localhost:3306/java_st?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <package name="com.liuyt.dao"/> </mappers> </configuration>
DeptMapper.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.liuyt.dao.DeptMapper"> <insert id="saveDept"> insert into dept(deptno,dname,loc,flag) values(#{deptNo},#{dname},#{loc},#{flag:NUMERIC}) </insert> <select id="deptFindById" resultType="Dept"> select * from dept where deptno=#{deptNo} </select> <select id="deptFindByFlag" resultType="Dept"> select * from dept where flag=#{falg} </select> </mapper>
BooleanTypeHandler.java
package com.liuyt.utils; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class BooleanTypeHandler implements TypeHandler { public void setParameter(PreparedStatement ps, int i, Object o, JdbcType jdbcType) throws SQLException { System.out.println("开始转换"); if (o == null) { ps.setInt(i, 4); return; } Boolean flag = (Boolean) o; if (flag == Boolean.TRUE) { ps.setInt(i, 2); } else { ps.setInt(i, 3); } } public Object getResult(ResultSet rs, String columnName) throws SQLException { int flag = rs.getInt(columnName); Boolean myFlag = Boolean.FALSE; if (flag == 3) { myFlag = Boolean.TRUE; } return myFlag; } public Object getResult(ResultSet resultSet, int i) throws SQLException { return null; } public Object getResult(CallableStatement callableStatement, int i) throws SQLException { return null; } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
问题
继承TypeHandler编写类型转换代码,测试中一直不生效
原因
注意
javaType和jdbcType相同的情况下,后面的会覆盖前面的TypeHandler,也可以理解相同的javaTypeh和jdbcType只能有一个TypeHandler
那么像覆盖默认的TypeHandler不要设置jdbcType即可
mybatis-config.xml
DeptMapper.xml
BooleanTypeHandler.java
The text was updated successfully, but these errors were encountered: