1.代码+配置文件,包含一些postgresql数据库特有语法,和函数。
foreach:
List<Student> selectList(List<Integer> idList);
<!-- in查询所有,不分页 -->
<select id="selectList" resultType="Student">
select id,(name_age).name as name,(name_age).age as age,ST_AsGeoJSON(ST_AsText(space)) as space
from student
<where>
id in
<foreach item="id" index="index" collection="idList" open="(" separator="," close=")">
#{id}
</foreach>
</where>
</select>
2.mybatis转换
转换类
package com.example.demo918.handlers;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@MappedTypes({Double.class})
public class MyTypeHandler implements TypeHandler<Double> {
@Override
public void setParameter(PreparedStatement preparedStatement, int i, Double aDouble, JdbcType jdbcType) throws SQLException {
}
@Override
public Double getResult(ResultSet resultSet, String s) throws SQLException {
System.out.println("s="+s);
return 1.0;
}
@Override
public Double getResult(ResultSet resultSet, int i) throws SQLException {
System.out.println("i="+i);
return 1.0;
}
@Override
public Double getResult(CallableStatement callableStatement, int i) throws SQLException {
return 1.0;
}
}
实体类
package com.example.demo918.entity;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class Test1 {
private Integer id;
private String stationNum;
private String observTime;
private Double precipitation;
private Double dryBulTemp;
}
配置文件:
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.example.demo918
configuration:
map-underscore-to-camel-case: true
type-handlers-package: com.example.demo918.handlers
mybatis xml:模糊查询
<!--土监测站基本信息-->
<select id="getSoilPollutionMonitoringInfo" resultType="com.zjzy.model.dto.soil.SoilPollutionMonitoringPointDTO">
SELECT m.JCDMC,m.DWBH,m.WRDJ,m.ZHWRZS,m.SSXZQ,m.DKMC,m.JD,M.WD
FROM `soil_pollution_monitoring_point` d , soil_pollution_monitoring_point_data m
<where>
d.DWBH=m.DWBH
<if test="WRDJ!=null">
and m.WRDJ=#{WRDJ}
</if>
<if test="DKMC!=null">
and m.DKMC like concat(#{DKMC},'%')
</if>
</where>
order by m.SSXZQ
</select>