package di.controller;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import li.utils.JsonUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import di.dao.GisMapper;
import di.model.GisModel;
@Controller
@RequestMapping(value = "/gis")
public class GisController {
/**
* @ClassName GisController
* @Description 地理信息系统服务
* @Author libi
* @CreateDate 2016年3月24日
*/
private GisMapper gisMapper = null;
public GisController() {
// TODO Auto-generated constructor stub
if(gisMapper == null){
gisMapper = (GisMapper) getMapper(GisMapper.class);
}
}
@RequestMapping
public String gis(){
return "GISMap/index.html";
}
@RequestMapping(value = "/search")
public @ResponseBody List<GisModel> search(String key){
System.out.println(key);
return gisMapper.search("%"+key+"%");
}
@RequestMapping(value = "/getDetail")
public @ResponseBody Map<String,Object> getDetail(String kid, String type){
Map<String, Object> param = new HashMap<String, Object>();
param.put("kid", Integer.valueOf(kid));
param.put("type", type);
param.put("kcln", gisMapper.getKeyColumn(type));
LinkedHashMap<String,Object> map = (LinkedHashMap<String,Object>) gisMapper.getDetail(param);
System.out.println(map.size());
Map<String,Object> map2 = JsonUtils.changeToString(map);
map2.put("size",map.size());
System.out.print(map2);
return map2;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private Object getMapper(Class obj){
Reader reader = null;
try {
reader = Resources.getResourceAsReader("mybatis-config.xml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
SqlSessionFactory sqlSession = new SqlSessionFactoryBuilder().build(reader);
return sqlSession.openSession(true).getMapper(obj);
}
}
package di.dao;
import java.util.List;
import java.util.Map;
import di.model.GisModel;
@SuppressWarnings("rawtypes")
public interface GisMapper {
public List<GisModel> search(String key);
public Map getDetail(Map param);
public String getKeyColumn(String tbname);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="di.dao.GisMapper">
<select id="search" resultType="di.model.GisModel">
<![CDATA[
select intcitycid id, varcityname name, fltlongitude longitude, fltlatitude latitude, varoverview detail,
'tbcity' type from tbcity where varcityname like #{key}
union
select intdamid id,vardamname name,fltlongitude longitude,fltlatitude latitude,txtoverview detail,'tbdam' type
from tbdam where vardamname like #{key}
union
select intoildepotid id,varoildepname name,fltlongitude longitude,fltlatitude latitude,txtoverview detail,'tboildepot' type
from tboildepot where varoildepname like #{key}
]]>
</select>
<select id="getDetail" parameterType="java.util.Map" resultType="java.util.LinkedHashMap">
<![CDATA[
select * from ${type}
where ${kcln}=${kid}
]]>
</select>
<select id="getKeyColumn" parameterType="String" resultType="String">
<![CDATA[ 下面的语句是mysql的特有语句
select column_name
from information_schema.columns
where table_name=#{type} and column_name LIKE 'int%id'
]]>
</select>
</mapper>