我是靠谱客的博主 繁荣糖豆,这篇文章主要介绍mybatis——属性文件、全局参数、别名、类型转换器、resultMap(二)一.属性文件 配置数据库连接二 全局参数三 别名(使用别名后可以进行相关名字的替换)四 类型转换器,现在分享给大家,希望可以做个参考。

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

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

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

1 . 创建 db.properties 文件

填写数据库连接信息

复制代码
1
2
3
4
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/ssm?serverTimezone=GMT%2B8 username=root password=root

2 . 导入 

复制代码
1
<properties resource="db.properties" />

3 . 配置数据库信息

复制代码
1
2
3
4
5
6
<dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource>

二 全局参数

复制代码
1
2
3
<settings> <setting name="" value=""/> </settings>

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

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

四 类型转换器

本身有自带的类型转换器

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

复制代码
1
2
* 可以实现TypeHandler接口 * 或者选择继承实现他的BaseTypeHandler实现类

代码部分 把Boolean 转换成 varchar 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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 添加自定义转换器

复制代码
1
2
3
4
5
<!--自定义的类型转换器--> <typeHandlers> <!--单个转换--> <typeHandler handler="com.xiaonuo.typeHandler.BooleanAndCharTypeHandler" javaType="Boolean" jdbcType="VARCHAR" /> </typeHandlers>

映射文件中添加

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

查询

复制代码
1
2
3
4
5
6
7
8
<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>

插入

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

最后

以上就是繁荣糖豆最近收集整理的关于mybatis——属性文件、全局参数、别名、类型转换器、resultMap(二)一.属性文件 配置数据库连接二 全局参数三 别名(使用别名后可以进行相关名字的替换)四 类型转换器的全部内容,更多相关mybatis——属性文件、全局参数、别名、类型转换器、resultMap(二)一.属性文件内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部