• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

mybatis-querymethods: 参考spring data jpa的Query Methods的mybatis版本

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

mybatis-querymethods

开源软件地址:

https://gitee.com/gdouyang/mybatis-querymethods

开源软件介绍:

在使用spring data jpa的时候可以通过方法名来动态的创建查询语句,于是有了想把这个功能移植到mybaits的想法

通过方法名来动态生成sql查询

  • 通过QueryMethodsHelper来重新生成sqlSource
  • 通过QueryMethodsInterceptor来生成where条件

tkmapper版本

<dependency>    <groupId>tk.mybatis</groupId>    <artifactId>mapper</artifactId>    <version>4.1.5</version></dependency>

地址:https://mvnrepository.com/artifact/com.github.gdouyang/mybatis-querymethods

使用方式

<dependency>    <groupId>com.github.gdouyang</groupId>    <artifactId>mybatis-querymethods</artifactId>    <version>${version}</version></dependency>

spring boot方式(tkmapper)

@tk.mybatis.spring.annotation.MapperScan(		factoryBean = querymethods.tkmapper.QueryMethodsMapperFactoryBean.class)@EnableTransactionManagement@SpringBootApplicationpublic class QuickDuckApplication {	public static void main(String[] args) {		SpringApplication.run(QuickDuckApplication.class, args);	}}# 配置application.ymlmybatis:  config-location: classpath:mybatis-config.xml

spring boot方式(mybatis-plus)

import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@MapperScan(basePackages = "com.example.demo", factoryBean = querymethods.mybatisplus.QueryMethodsMapperFactoryBean.class)@SpringBootApplicationpublic class QuickDuckApplication {  public static void main(String[] args) {    SpringApplication.run(QuickDuckApplication.class, args);  }}# 配置application.ymlmybatis-plus:  config-location: classpath:mybatis-config.xml

mybatis-config.xml配置(放在src/main/resources下)

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>    <plugins>    <!-- 分页插件 -->    <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>    <!-- 查询方法插件 -->    <plugin interceptor="querymethods.intercepts.QueryMethodsInterceptor"></plugin> </plugins>  </configuration>

spring mvc方式(tkmapper)

<bean class="querymethods.tkmapper.MapperScannerConfigurer">    <property name="basePackage" value="org.mybatis.spring.sample.mapper" />    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /></bean># 配置拦截器<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">   <property name="plugins">    <array>	<bean class="com.github.pagehelper.PageInterceptor"></bean>        <bean class="querymethods.intercepts.QueryMethodsInterceptor"></bean>    </array>   </property></bean>

样例

在Mapper使用Select注解,给空字符串, 没有注解的需要在xml文件中配置

public interface CustomerMapper extends Mapper<Customer> {  @Select("")  Customer findByIdAndFirstName(Integer id, String name);  @Select("")  Customer findByIdOrFirstName(Integer id, String name);  @Select("")  Customer findById(Integer id);  @Select("")  Integer countById(Integer id);  @Select("")  List<Customer> findByFirstNameOrderByIdAsc(String name);  @Select("")  List<Customer> findByFirstNameStartingWith(String name);    @Select("")  List<Customer> findByFirstNameEndingWith(String name);    @Select("")  List<Customer> findByFirstNameContaining(String name);    @Select("")  List<Customer> findByFirstNameNotContaining(String name);    @Select("")  List<Customer> findByFirstNameLike(String name);    @Select("")  List<Customer> findByFirstNameNotLike(String name);    @Select("")  List<Customer> findByIdInOrId(List<Integer> idList, Integer id);    @Select("")  List<Customer> findByIdIn(List<Integer> idList);    @Select("")  List<Customer> findByIdNotIn(List<Integer> idList);    @Select("")  List<Customer> findByIdIsNull();    @Select("")  List<Customer> findByIdIsNotNull();    @Select("")  List<Customer> findByActiveTrue();    @Select("")  List<Customer> findByIdBetween(Integer from, Integer to);    @Select("")  List<Customer> findByIdAfter(Integer from);  @Select("")  List<Customer> findByIdGreaterThan(Integer from);  @Select("")  List<Customer> findByIdGreaterThanEqual(Integer from);    @Select("")  List<Customer> findByIdBefore(Integer from);  @Select("")  List<Customer> findByIdLessThan(Integer from);  @Select("")  List<Customer> findByIdLessThanEqual(Integer from);  @Select("")  String findFirstNameById(Integer id);  @Select("")  String findDistinctFirstNameById(Integer id);  @Delete("")  int deleteByFirstName(String name);  @Delete("")  int deleteByFirstNameAndId(String name, Integer id);}
CREATE DATABASE IF NOT EXISTS querymethods CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;use querymethods;DROP TABLE IF EXISTS `customer`;CREATE TABLE `customer` (  `id` bigint(20) NOT NULL AUTO_INCREMENT,  `first_name` varchar(255) DEFAULT NULL,  `last_name` varchar(255) DEFAULT NULL,  `active` bit(1) DEFAULT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB;

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap