mybatis——将sql语句作为字符串变量传入实现结果集降序排列

studentMapping.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="mybatis.stu">
	<select id="getStu" parameterType="map" resultType="map">
		select 
		id,name,password,school,major
		from
		t_student where id>#{id} order
		by ${mysql}
	</select>
</mapper>
package test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class test {

	public static void main(String[] args) throws IOException {
		HashMap mapParam = new HashMap();
		mapParam.put("id", 3);
		mapParam.put("mysql", "id desc");
		String resource = "mybatis-config.xml";
		InputStream inp = Resources.getResourceAsStream(resource);
		SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(inp);
		SqlSession ss = ssf.openSession();
		List<Map> list = ss.selectList("getStu", mapParam);
		for (int i = 0; i < list.size(); i++) {
			Map map = list.get(i);
			System.out.println(map.get("id")+"__"
					+map.get("name")+"__"+map.get("password")+"__"+
					map.get("school")+"__"+map.get("major"));
		}
	}
}

mybatis-config.xml代码博主前面文章里有,这里就不放了 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页