mybatis——属性文件、全局参数、别名、类型转换器、resultMap(二)

config.xml中的配置文件需要 按照顺序 不然就会报错

properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?

一.属性文件 配置数据库连接

1 . 创建 db.properties 文件

填写数据库连接信息

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm?serverTimezone=GMT%2B8
username=root
password=root

2 . 导入 

<properties resource="db.properties" />

3 . 配置数据库信息

<dataSource type="POOLED">
    <property name="driver" value="${driver}"/>
    <property name="url" value="${url}"/>
    <property name="username" value="${username}"/>
    <property name="password" value="${password}"/>
</dataSource>

二 全局参数

<settings>
    <setting name="" value=""/>
</settings>

三 别名(使用别名后可以进行相关名字的替换)

<typeAliases>
    <!--单个设置别名-->
    <typeAlias type="com.xiaonuo.domain.Person" alias="Person"></typeAlias>
    <!--批量设置别名 别名就是不带前缀包名的类本来名字 忽略大小写-->
    <package name="com.xiaonuo.domain"></package>
</typeAliases>

四 类型转换器

本身有自带的类型转换器

这边记录下自定义的类型转换器

* 可以实现TypeHandler接口
* 或者选择继承实现他的BaseTypeHandler实现类

代码部分 把Boolean 转换成 varchar 

public class BooleanAndCharTypeHandler extends BaseTypeHandler<Boolean> {
    /*从Java(Boolean) -> JB(int)*/
    //true代表男
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Boolean aBoolean, JdbcType jdbcType) throws SQLException {
        if(aBoolean){
            preparedStatement.setString(i,"男");
        }
        else{
            preparedStatement.setString(i,"女");
        }
    }

    //根据列名拿
    @Override
    public Boolean getNullableResult(ResultSet resultSet, String s) throws SQLException {
        return resultSet.getString(s).equals("男") ? true : false ;
    }
    //根据下标拿
    @Override
    public Boolean getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return resultSet.getString(i).equals("男") ? true : false ;
    }

    //存储过程拿
    @Override
    public Boolean getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return callableStatement.getString(i).equals("男") ? true : false ;
    }
}

config.xml 添加自定义转换器

<!--自定义的类型转换器-->
<typeHandlers>
    <!--单个转换-->
    <typeHandler handler="com.xiaonuo.typeHandler.BooleanAndCharTypeHandler" javaType="Boolean" jdbcType="VARCHAR" />
</typeHandlers>

映射文件中添加

<!--如果数据库类型和类类型中能够精确识别 那么就使用 resultType 否则使用 resultMap-->
<!--如果数据库名字和类名字中能够精确识别 那么就使用 resultType 否则使用 resultMap-->

查询

<select id="selectAll" resultMap="selectIdMaper">
    select * from person
</select>
<resultMap id="selectIdMaper" type="Person">
    <id property="id" column="id"></id>
    <result property="name" column="name"></result>
    <result property="sex" column="sex" javaType="Boolean" jdbcType="VARCHAR"></result>
</resultMap>

插入

<insert id="insertStudent" parameterType="Person" keyProperty="id" useGeneratedKeys="true">
     insert into person values(#{id},#{name},#{sex,javaType=boolean , jdbcType = VARCHAR})
 </insert>

猜你喜欢

转载自blog.csdn.net/qq_40632760/article/details/87907902