mybatis中的查询所有的几种方案
1.注解的方式
- dao层的代码
@Select("select * from demo")
public List<Demo> findAll1();
- 无xml
- 测试代码
@Test
public void testFindAll1() {
List<Demo> demoList = demoDao.findAll1();
System.out.println(demoList);
}
2.xml的方式(select里面使用resultType标签)
说明:测试默认entity属性和数据库列名相对应,否则就要起别名,使得别名和数据库列名相对应
- dao层的代码
public List<Demo> findAll12();
- xml
<select id="findAll12" resultType="com.fcmap.ssm.domain.Demo">
select * from demo
</select>
- 测试代码
@Test
public void testFindAll2() {
List<Demo> demoList = demoDao.findAll12();
System.out.println(demoList);
}
3.xml的方式(select里面使用resultMap标签)
- dao层代码
public List<Demo> findAll3();
- xml
<resultMap type="com.fcmap.ssm.domain.Demo" id="srm">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
</resultMap>
<select id="findAll3" resultMap="srm">
select * from demo
</select>
- 测试代码
@Test
public void testFindAll3() {
List<Demo> demoList = demoDao.findAll3();
System.out.println(demoList);
}
4.注解的方式+@Results+@Result
- dao层代码
@Select("select * from demo")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "name", column = "name")
})
public List<Demo> findAll4();
-
无xml
-
测试代码
@Test
public void testFindAll4() {
List<Demo> demoList = demoDao.findAll4();
System.out.println(demoList);
}
5.注解方式+xml方式
说明:要调用xml里面的reultMap标签
- dao层代码
@Select("select * from demo")
@ResultMap("srm")
public List<Demo> findAll5();
- 在xml引用写好的resultMap标签,引用里面的id的值
<resultMap type="com.fcmap.ssm.domain.Demo" id="srm">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
</resultMap>
- 测试代码
@Test
public void testFindAll5() {
List<Demo> demoList = demoDao.findAll5();
System.out.println(demoList);
}
6.返回值为hashmap的方式
说明:此时json数据是无序的
- dao层代码
@Select("select * from demo")
public List<HashMap<String,Object>> findall7();
-
无xm文件
-
测试代码
@Test
public void testFindAll7() throws JsonProcessingException {
List<HashMap<String, Object>> map = demoDao.findall7();
System.out.println(map);
System.out.println(new ObjectMapper().writeValueAsString(map));
}
- 返回的结果数据(json格式)
[{"name":null,"id":1},{"name":null,"id":2},{"name":null,"id":4},{"name":null,"id":5}]
7.返回值为linkedhashmap的方式
- dao层代码
@Select("select * from demo")
public List<LinkedHashMap<String,Object>> findall6();
-
无xml文件
-
测试代码
@Test
public void testFindAll6() throws JsonProcessingException {
List<LinkedHashMap<String, Object>> map = demoDao.findall6();
System.out.println(map);
System.out.println(new ObjectMapper().writeValueAsString(map));
}
- 返回的结果数据(json格式)
[{"id":1,"name":null},{"id":2,"name":null},{"id":4,"name":null},{"id":5,"name":null}]
注意点6和7两点需要说明
返回map需要在mybatis配置文件里面进行如下配置,否则回出现null值不显示的情况
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
<!-- 解决插入部分字段为空的问题 -->
<setting name="jdbcTypeForNull" value="NULL"/>
<setting name="callSettersOnNulls" value="true"/>
</settings>
<!-- <typeAliases>
<typeAlias type="com.queen.mybatis.bean.User"/>
package:为某个包下所有类批量起别名
name:指定包名(为当前包以及下面所有的后代包的每一个类都起一个默认别名,默认小写)
<package name="com.fcmap.ssm.model"/>
</typeAliases> -->
</configuration>