当前位置: 移动技术网 > IT编程>开发语言>Java > mybatis中的查询所有的几种方案

mybatis中的查询所有的几种方案

2020年07月30日  | 移动技术网IT编程  | 我要评论
mybatis中的查询所有的几种方案1.注解的方式dao层的代码@Select("select * from demo")public List<Demo> findAll1();无xml测试代码@Testpublic void testFindAll1() {List<Demo> demoList = demoDao.findAll1();System.out.println(demoList);}2.xml的方式(select里面

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);
	}

本文地址:https://blog.csdn.net/shaoming314/article/details/107671647

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网