其他分享
首页 > 其他分享> > myBatis输出参数

myBatis输出参数

作者:互联网

一、简单类型

  8种简单类型+String
  mapper.xml

	<select id="queryStudentCount"	 resultType="int">
		select count(*)  from student 
	</select>

Java接口中

	int queryStudentCount();

Test.java测试类

//查询学生总数
	public static void queryStudentCount() throws IOException {
		Reader reader = Resources.getResourceAsReader("conf.xml");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
		SqlSession session = sessionFactory.openSession();
		StudentMapper studentMapper = session.getMapper(StudentMapper.class);
		System.out.println(studentMapper.queryStudentCount());
		session.close();
	}

二、对象类型

a.返回值为单个对象时,输出参数为实体对象类型
b.返回值为对象集合时,在mapper.xml中的resultType是返回值集合的对象类型
  在java类中,返回值为List<对象类型>
  mapper.xml

	<select id="queryStuByStuno"	parameterType="int"	 resultType="student">
		select * from student where stuno = #{stuno}
	</select>
	<select id="queryAllStudents" resultType="student">
		select * from student
	</select>

  Java接口

	Student queryStuByStuno(int stuNo);//查询单个学生
	List<Student> queryAllStudents();//查询多个学生的集合

  Test.java测试类

//查询单个学生
	public static void queryStuByStuno() throws IOException {
		Reader reader = Resources.getResourceAsReader("conf.xml");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
		SqlSession session = sessionFactory.openSession();
		StudentMapper studentMapper = session.getMapper(StudentMapper.class);
		System.out.println(studentMapper.queryStuByStuno(1).toString());
		session.close();
	}
//查询全部学生
	public static void queryAllStudents() throws IOException {
		Reader reader = Resources.getResourceAsReader("conf.xml");
		SqlSessionFactory sessionFactoty = new SqlSessionFactoryBuilder().build(reader);
		SqlSession session = sessionFactoty.openSession();
		StudentMapper studentMapper = session.getMapper(StudentMapper.class);
		List<Student> students = studentMapper.queryAllStudents();
		System.out.println(students.toString());
		session.close();
	}	

三、HashMap

  HashMap本身是一个集合,可以存放多个数据。
  但是根据提示发现,返回值为HashMap是,查询结果只能是一个学生,此时是一个HashMap对象对应一个学生(no ,name)
  结论----》一个HashMap对应一个学生的多个元素(多个属性),对此可以用List<HashMap>存储
  HashMapA— no-----1
        name—zs
        age–22
  HashMapB— no-----2
        name—ww
        age–23
  mapper.xml

	<select id="queryStudentOutByHashMap"  resultType="HashMap">
		<!-- sql语句中字段的别名对应HashMap的key -->
		select stuno "no", stuname "name"  from student
	</select>

  Java接口

	List<HashMap<String, Object>> queryStudentOutByHashMap();

  Test.java测试类

//查询全部学生返回值用HashMap存储
		public static void queryStudentOutByHashMap() throws IOException {
			Reader reader = Resources.getResourceAsReader("conf.xml");
			SqlSessionFactory sessionFactoty = new SqlSessionFactoryBuilder().build(reader);
			SqlSession session = sessionFactoty.openSession();
			StudentMapper studentMapper = session.getMapper(StudentMapper.class);
			List<HashMap<String, Object>> studentsMap = studentMapper.queryStudentOutByHashMap();
			System.out.println(studentsMap.toString());
			session.close();
		}

四、resultMap

标签:xml,输出,studentMapper,HashMap,StudentMapper,session,参数,reader,myBatis
来源: https://blog.csdn.net/qq_39969068/article/details/112383951