mybatis中特殊SQL的几种配置方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis中特殊SQL的几种配置方法相关的知识,希望对你有一定的参考价值。

参考技术A 1.in条件for循环
<select id="getBooList" resultMap="java.util.Map" parameterType="java.util.List" >
select booking_no, max(booking_general_seq) max_seq from interface_booking_t where booking_no in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#item
</foreach>
group by booking_no
</select>
2.insert并且返回主键
<insert id="insertAndGetId" parameterType="com.phy.mlisp.management.domain.Document">
<selectKey resultType="long" order="BEFORE" keyProperty="id">
SELECT MLISP_DOCUMENT_SEQ.NEXTVAL as ID from DUAL
</selectKey>
insert into MLISP_DOCUMENT_T (ID, NAME, ALIAS,
FILE_SIZE,CONTENT,STATUS,CREATE_BY,CREATE_DATE,LAST_UPDATE_BY,
LAST_UPDATE_DATE)
values (#id,#name,jdbcType=VARCHAR, #alias,jdbcType=VARCHAR,#fileSize,jdbcType=INTEGER,
#content,jdbcType=BLOB,
#status,jdbcType=INTEGER,#createBy,jdbcType=VARCHAR,SYSDATE,
#lastUpdateBy,jdbcType=VARCHAR,SYSDATE)
</insert>

3.批量insert
<insert id="batchInsert" parameterType="java.util.List">
insert into INTERFACE_BOOKING_CONTA_T (ID, BOOKING_ID, BOOKING_CONTAINER_SEQ,
CONTA_TYPE, CONTA_SIZE, CONTA_QUANTITY,
CONTA_SIZE_TYPE_ISO)
select interface_booking_conta_seq.NEXTVAL,A.* from(
<foreach collection="list" item="item" index="index" separator="UNION ALL" >
(select #item.bookingId,jdbcType=DECIMAL, #item.bookingContainerSeq,jdbcType=DECIMAL,
#item.contaType,jdbcType=VARCHAR, #item.contaSize,jdbcType=VARCHAR, #item.contaQuantity,jdbcType=DECIMAL,
#item.contaSizeTypeIso,jdbcType=VARCHAR from dual)
</foreach>) A
</insert>

4.批量更新
<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">
UPDATE WCP_DISPATCH_CONTA_T
<set>
<if test="item.dispatchId != null">
DISPATCH_ID = #item.dispatchId,jdbcType=DECIMAL,
</if>
</set>
WHERE DISPATCH_ID = #item.dispatchId,jdbcType=DECIMAL
</foreach>
</update>

SpringBoot + Mybatis系列Mapper接口注册的几种方式

SpringBoot项目中借助Mybatis来操作数据库,对大部分java技术栈的小伙伴来说,并不会陌生;我们知道,使用mybatis,一般会有下面几个

  • Entity: 数据库实体类
  • Mapper: db操作接口
  • Service: 服务类
  • xml文件:写sql的地方

本篇博文中主要介绍是Mapper接口与对应的xml文件如何关联的几种姿势(这个问题像不像"茴"字有几个写法)

一 环境准备

1. 数据库准备

使用mysql作为本文的实例数据库,新增一张表

CREATE TABLE `money` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL DEFAULT '' COMMENT '用户名',
  `money` int(26) NOT NULL DEFAULT '0' COMMENT '钱',
  `is_deleted` tinyint(1) NOT NULL DEFAULT '0',
  `create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;

2. 项目环境

本文借助 SpringBoot 2.2.1.RELEASE + maven 3.5.3 + IDEA进行开发

pom依赖如下

<dependencies>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>

db配置信息 application.yml

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/story?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password:

二 实例演示

前面基础环境搭建完成,接下来准备下Mybatis的Entity,Mapper等基础类

1. 实体类,Mapper类

数据库实体类MoneyPo

@Data
public class MoneyPo {
    private Integer id;

    private String name;

    private Long money;

    private Integer isDeleted;

    private Timestamp createAt;

    private Timestamp updateAt;
}

对应的Mapper接口(这里直接使用注解的方式来实现CURD)

public interface MoneyMapper {

    /**
     * 保存数据,并保存主键id
     *
     * @param po
     * @return int
     */
    @Options(useGeneratedKeys = true, keyProperty = "po.id", keyColumn = "id")
    @Insert("insert into money (name, money, is_deleted) values (#{po.name}, #{po.money}, #{po.isDeleted})")
    int save(@Param("po") MoneyPo po);

    /**
     * 更新
     *
     * @param id    id
     * @param money 钱
     * @return int
     */
    @Update("update money set `money`=#{money} where id = #{id}")
    int update(@Param("id") int id, @Param("money") long money);

    /**
     * 删除数据
     *
     * @param id id
     * @return int
     */
    @Delete("delete from money where id = #{id}")
    int delete(@Param("id") int id);

    /**
     * 主键查询
     *
     * @param id id
     * @return {@link MoneyPo}
     */
    @Select("select * from money where id = #{id}")
    @Results(id = "moneyResultMap", value = {
            @Result(property = "id", column = "id", id = true, jdbcType = JdbcType.INTEGER),
            @Result(property = "name", column = "name", jdbcType = JdbcType.VARCHAR),
            @Result(property = "money", column = "money", jdbcType = JdbcType.INTEGER),
            @Result(property = "isDeleted", column = "is_deleted", jdbcType = JdbcType.TINYINT),
            @Result(property = "createAt", column = "create_at", jdbcType = JdbcType.TIMESTAMP),
            @Result(property = "updateAt", column = "update_at", jdbcType = JdbcType.TIMESTAMP)})
    MoneyPo getById(@Param("id") int id);
}

对应的Service类

@Slf4j
@Service
public class MoneyService {
    @Autowired
    private MoneyMapper moneyMapper;

    public void basicTest() {
        int id = save();
        log.info("save {}", getById(id));
        boolean update = update(id, 202L);
        log.info("update {}, {}", update, getById(id));
        boolean delete = delete(id);
        log.info("delete {}, {}", delete, getById(id));
    }

    private int save() {
        MoneyPo po = new MoneyPo();
        po.setName("一灰灰blog");
        po.setMoney(101L);
        po.setIsDeleted(0);
        moneyMapper.save(po);
        return po.getId();
    }

    private boolean update(int id, long newMoney) {
        int ans = moneyMapper.update(id, newMoney);
        return ans > 0;
    }

    private boolean delete(int id) {
        return moneyMapper.delete(id) > 0;
    }

    private MoneyPo getById(int id) {
        return moneyMapper.getById(id);
    }
}

2. 注册方式

注意,上面写完之后,若不通过下面的几种方式注册Mapper接口,项目启动会失败,提示找不到MoneyMapper对应的bean

Field moneyMapper in com.git.hui.boot.mybatis.service.MoneyService required a bean of type 'com.git.hui.boot.mybatis.mapper.MoneyMapper' that could not be found.

2.1 @MapperScan注册方式

在配置类or启动类上,添加@MapperScan注解来指定Mapper接口的包路径,从而实现Mapper接口的注册

@MapperScan(basePackages = "com.git.hui.boot.mybatis.mapper")
@SpringBootApplication
public class Application {

    public Application(MoneyService moneyService) {
        moneyService.basicTest();
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

执行之后输出结果如下

2021-07-06 19:12:57.984  INFO 1876 --- [           main] c.g.h.boot.mybatis.service.MoneyService  : save MoneyPo(id=557, name=一灰灰blog, money=101, isDeleted=0, createAt=2021-07-06 19:12:57.0, updateAt=2021-07-06 19:12:57.0)
2021-07-06 19:12:58.011  INFO 1876 --- [           main] c.g.h.boot.mybatis.service.MoneyService  : update true, MoneyPo(id=557, name=一灰灰blog, money=202, isDeleted=0, createAt=2021-07-06 19:12:57.0, updateAt=2021-07-06 19:12:57.0)
2021-07-06 19:12:58.039  INFO 1876 --- [           main] c.g.h.boot.mybatis.service.MoneyService  : delete true, null

注意:

  • basePackages: 传入Mapper的包路径,数组,可以传入多个
  • 包路径支持正则,如com.git.hui.boot.*.mapper
    • 上面这种方式,可以避免让我们所有的mapper都放在一个包路径下,从而导致阅读不友好

2.2 @Mapper 注册方式

前面的@MapperScan指定mapper的包路径,这个注解则直接放在Mapper接口上

@Mapper
public interface MoneyMapper {
...
}

测试输出省略...

2.3 MapperScannerConfigurer注册方式

使用MapperScannerConfigurer来实现mapper接口注册,在很久以前,还是使用Spring的xml进行bean的声明的时候,mybatis的mapper就是这么玩的

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="xxx"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

对应的java代码如下:

@Configuration
public class AutoConfig {
    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置,这里使用mybatis注解方式,没有配置xml文件
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/*.xml"));
        return bean.getObject();
    }

    @Bean("sqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory storySqlSessionFactory) {
        return new SqlSessionTemplate(storySqlSessionFactory);
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.git.hui.boot.mybatis.mapper");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setSqlSessionTemplateBeanName("sqlSessionTemplate");
        return mapperScannerConfigurer;
    }
}

测试输出省略

3. 小结

本文主要介绍Mybatis中Mapper接口的三种注册方式,其中常见的两种注解方式

  • @MapperScan: 指定Mapper接口的包路径
  • @Mapper: 放在mapper接口上
  • MapperScannerConfigurer: 编程方式注册

那么疑问来了,为啥要介绍这三种方式,我们实际的业务开发中,前面两个基本上就满足了;什么场景会用到第三种方式?

  • 如写通用的Mapper(类似Mybatis-Plus中的BaseMapper)
  • 如一个Mapper,多数据源的场景(如主从库,冷热库,db的操作mapper一致,但是底层的数据源不同)

最后

最近我整理了整套《JAVA核心知识点总结》,说实话 ,作为一名Java程序员,不论你需不需要面试都应该好好看下这份资料。拿到手总是不亏的~我的不少粉丝也因此拿到腾讯字节快手等公司的Offer

Java进阶之路群,找管理员获取哦-!

以上是关于mybatis中特殊SQL的几种配置方法的主要内容,如果未能解决你的问题,请参考以下文章

mybatis配置文件报错的几种情况

读取配置文件的几种方法

java读取配置文件的几种方法

Java读取配置文件的几种方法

Eclipse配置SVN的几种方法及使用详情

Flask配置文件的几种方法