Mybatis使用记录

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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值