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

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Milan__Kundera/article/details/81837498

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代码博主前面文章里有,这里就不放了 

展开阅读全文

没有更多推荐了,返回首页