resultTpye
作为select标签的属性,可以使用两种
1-1.Java类型的全限定名称,以下为例:
resultType=“com.example.springboor.entity.Student”
先调用这了类的无参构造方法,创建对象,在把列值赋值给同名的属性,最后得到Java对象,如果dao接口返回值是list,mybatis把student对象放入list集合中.
1-2.返回简单数据类型的时候,直接写:
<select id="countAll" resultType="int">
select count(*) from student
</select>
1-3.返回map类型的时候(如果dao接口不用list,只允许返回一条记录,返回多条记录会报错)
dao接口
List<Map<String,Object>> queryAllMap();
mapper.xml文件
<select id="queryAllMap" resultType="map">
select * from student
</select>
输出为:
[
{
"name": "展示",
"id": 1,
"email": "[email protected]",
"age": 1
},
{
"name": "b",
"id": 2,
"email": "[email protected]",
"age": 2
},
{
"name": "c",
"id": 3,
"email": "[email protected]",
"age": 3
},
{
"name": "a",
"id": 4,
"email": "[email protected]",
"age": 1
}
]
2.自定义别名
在mybatis主配置文件中
可以谁便起别名,每个类型都必须单独定义
<typeAliases>
<typeAlias type="com.example.springboor.entity.Student" alias="student"></typeAlias>
</typeAliases>
或者包扫描的方式来取别名,它会扫描该包路径下所有的类,为其取别名,不区分大小写.
<typeAliases>
<package name="com.example.springboor.entity"/>
</typeAliases>