模糊查询:
方法一:
写在xml文件:
<sql id="if-title-author">
<if test="title!=null">
title like "%"#{title}"%"
</if>
<if test="author!=null">
and author=#{author}
</if>
</sql>
<select id="queryBlogIF" parameterType="map" resultType="blog">
select *
from mybatis.blog
<where>
<include refid="if-title-author">
</include>
</where>
</select>
@Test
public void queryBlogIF(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap hashMap = new HashMap();
hashMap.put("title","java");
//hashMap.put("author","刘");
List<Blog> blogs =mapper.queryBlogIF(hashMap);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.close();
}
方法二:1.Java代码执行的时候,传递通配符%%
<select id="getUserLike" resultType="lyt.pojo.User">
select * from mybatis.user where name like #{value}
</select>
@Test
public void getUserLike(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserLike("%李%");
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
方法三:在Sql拼接中,使用通配符!
<select id="getUserLike" resultType="lyt.pojo.User">
select * from mybatis.user where name like concat('%',#{value},'%')
</select>
@Test
public void getUserLike(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserLike("李");
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
/**
* 查询范围内雷达当前目标数据---10秒内数据
* @param radarId
* @param minLon
* @param minLat
* @param maxLon
* @param maxLat
* @return
*/
@Select("select r.*,fs.mmsi from radar_record as r inner join " +
"(select time,station from radar_record where radar_id=${radarId} and time >= now()-interval 10 second ORDER BY time desc LIMIT 1) as t on r.time=t.time and r.radar_id=t.radar_id " +
" left join fusion_ship as fs ON rr.radar_id = fs.radar_id AND rr.target_id = fs.target_id"+
" where r.lat>${minLat} and r.lat<${maxLat} and r.lon>${minLon} and r.lon<${maxLon} ")
List<RadarRecordBean> radarRecordFusionShipByRadarIdAndRegion(Long radarId,Double minLon, Double minLat, Double maxLon, Double maxLat);
/**
* 通过雷达id获取雷达当前目标数据---10秒内数据
* @param radarId
* @return
*/
@Select("select r.*,fs.mmsi from radar_record as r inner join " +
"(select time,station from radar_record where radar_id=${radarId} and time >= now()-interval 10 second ORDER BY time desc LIMIT 1) as t on r.time=t.time and r.radar_id=t.radar_id " +
" left join fusion_ship as fs ON rr.radar_id = fs.radar_id AND rr.target_id = fs.target_id")
List<RadarRecordBean> radarRecordFusionShipByRadarId(Long radarId);