mybatis中的配置及mysql的特有语句
2016-04-09 14:43
941 查看
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>
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>
相关文章推荐
- 常用mysql命令
- ubuntu 下mysql 不显示当前的数据库名解决方法
- mysql 时间类型小结
- mysql 数据类型选择
- MySQL删除重复记录只保留一条
- Mysql中使某个用户只能访问指定的数据库
- mysql通过sql文件导入数据时出现乱码的解决办法
- mysql数据库优化 SQL语句优化
- mysql数据库导出的方法
- 更改wamp的mysql密码
- MySQL中tinytext、text、mediumtext和longtext详解
- 高性能MySql
- mysql中视图更新详解
- MySQL之DDL,DQL,DML,DCL
- MySQL--基本内容
- MySQL常见配置参数调优
- user表中存在多条相同user不同host用户信息时MySQL该匹配哪条记录登录?
- SQL Server移植到MySQL(利用工具Sqlyog)
- windows x64 安装 MYSQL教程
- SQL脚本用Navicat导入MySQL数据库出现编码问题