<!-- 配置查询所有操作 --><selectid="findAll"resultType="com.keafmd.domain.User">
select * from user
</select>
1
2
3
4
1
2
3
4
测试查询结果
@TestpublicvoidtestFindAll(){
List<User> users = userDao.findAll();for(User user : users){
System.out.println(user);}}
1
2
3
4
5
6
7
1
2
3
4
5
6
7
输出结果:
名称会有值,是因为:mysql 在 windows 系统中不区分大小写!
修改映射配置
使用别名查询
<!-- 配置查询所有操作 --><selectid="findAll"resultType="com.keafmd.domain.User">
select id as userId,username as userName,birthday as userBirthday,sex as userSex,address as userAddress from user
</select>
<!-- 建立 User 实体和数据库表的对应关系
type 属性:指定实体类的全限定类名
id 属性:给定一个唯一标识,是给查询 select 标签引用用的。--><resultMaptype="com.itheima.keafmd.User"id="userMap"><idcolumn="id"property="userId"/><resultcolumn="username"property="userName"/><resultcolumn="sex"property="userSex"/><resultcolumn="address"property="userAddress"/><resultcolumn="birthday"property="userBirthday"/></resultMap><!-- id 标签:用于指定主键字段
result 标签:用于指定非主键字段
column 属性:用于指定数据库列名
property 属性:用于指定实体类属性名称-->
1
2
3
4
5
6
7
8
9
10
11
12
13
1
2
3
4
5
6
7
8
9
10
11
12
13
映射配置
<!-- 配置查询所有操作 --><selectid="findAll"resultMap="userMap">
select * from user
</select>
1
2
3
4
1
2
3
4
测试结果
@TestpublicvoidtestFindAll(){
List<User> users = userDao.findAll();for(User user : users){
System.out.println(user);}}