mybatis中的配置及mysql的特有语句

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值