Mybatis全注解开发——动态SQL

前言

以前使用Mybatis的时候,使用XML文件来实现sql语句的书写。但是使用SpringBoot替代SSM以后,所有关于spring和其他的配置,SpringBoot全部默认配置好了。所以有没有一种方法来替代Mybatis的XML文件呢?那肯定是有的,就是mybatis自带的关于SQL的注解。但是有时候是需要根据不同的参数拼接SQL语句的,所以本篇主要介绍mybatis注解开发中的动态SQL。

一、mybatis注解sql

mybatis可以使用注解方式来代替XML文件方式来访问数据库,写一个interface,添加@Mapper注解,然后再方法上使用@Insert来新增、使用@Update来更新、使用@Select来查询,在注解中的属性里写SQL语句就可以了。接下来说一下动态SQL。

@Mapper
public interface NewsLWQDAO{

    @InsertProvider(method = "insertNews", type = NewsLWQProvider.class)
    @Options(useGeneratedKeys = true,keyProperty = "news.id")
    Integer insert(@Param("news")NewsDO news)throws Exception;

    @UpdateProvider(method = "updateNews", type = NewsLWQProvider.class)
    Integer updateById(@Param("news")NewsDO news)throws Exception;

    @Select("select * from news_table ")
    List<NewsDO> listAllNewsDO()throws Exception;

}

二、新增(insert)

新增使用 @InsertProvider 注解来进行动态sql 的拼接,该注解有两个属性,type是指定用那个类来拼接SQL,method是指定类中的方法名。

    @InsertProvider(method = "insertNews", type = NewsLWQProvider.class)
    @Options(useGeneratedKeys = true,keyProperty = "news.id")
    Integer insert(@Param("news")NewsDO news)throws Exception;

如果数据库主键使用的是主键自增,那么可以使用@Options注解把数据库自增的主键赋值给java类。

insertNews方法(省略部分属性):

 public String insertNews(@Param("news") NewsDO news){
        return new SQL(){
            {
                INSERT_INTO("trade_client.news_table");
				if(news.getUserId()!=null){
					VALUES("user_id","#{news.userId}");
				}
				if(news.getTitle()!=null){
					VALUES("title","#{news.title}");
				}
				if(news.getDetails()!=null){
					VALUES("details","#{news.details}");
				}
		    }
	     }.toString();
    }

注意insertNews方法的参数要和insert方法一致,最好使用@Param注解来标明参数的名字。

三、更新(update)

更新使用@UpdateProvider注解,其属性和新增的一样

    @UpdateProvider(method = "updateNews", type = NewsLWQProvider.class)
    Integer updateById(@Param("news")NewsDO news)throws Exception;

更新和新增的区别主要是关键字不一样。

    public String updateNews(@Param("news") NewsDO news){
        return new SQL(){
            {
                UPDATE("trade_client.news_table");
				if(news.getUserId()!=null){
					SET("user_id = #{news.userId}");
				}
				if(news.getTitle()!=null){
					SET("title = #{news.title}");
				}
				if(news.getDetails()!=null){
					SET("details = #{news.details}");
				}
		    }
	     }.toString();
    }

 

四、查询(select)

查询使用 @SelectProvider注解来动态拼接SQL。我们使用四个参数,这四个参数不确定是否有值,所以需要判断来拼接SQL语句。

    @SelectProvider(method = "listNewsByStatus", type = NewsLWQProvider.class)
    List<NewsDTO> listAllNewsByStatus(@Param("businessName")String businessName,@Param("newsStatus") Integer newsStatus,
							@Param("checkStatus")Integer checkStatus,@Param("hideStatus")Integer hideStatus)throws Exception;
    

NewsDTO中有UserDO的属性,所以我们使用左外连接查询了news_table和user_table,左外连接的关键字为LEFT_OUTER_JOIN。

    public String listNewsByStatus(@Param("businessName")String businessName,@Param("newsStatus")Integer newsStatus, 
    								@Param("checkStatus")Integer checkStatus,@Param("hideStatus")Integer hideStatus) {
        return new SQL() {
            {
                SELECT("a.user_id,b.user_name,a.contact_name,a.title,a.business_name,a.start_time,a.publish_time,"
                		+ "a.news_status,a.check_status,a.hide_status");
                FROM("trade_client.news_table a");
                LEFT_OUTER_JOIN("trade_users.user_table b on a.user_id = b.id");
                if (StringUtils.isNotBlank(businessName)) {
                    WHERE("a.business_name = #{businessName}");
                }
                if(!Objects.equals(newsStatus, null)) {
                    WHERE("a.news_status = #{newsStatus}");
                }
                if(!Objects.equals(checkStatus, null)) {
                    WHERE("a.check_status = #{checkStatus}");
                }
                if(!Objects.equals(hideStatus, null)) {
                    WHERE("a.hide_status = #{hideStatus}");
                }
            
            }
            
        }.toString();
    }

 

写在后面的话

Mybatis的注解功能应该是挺全的,基本的使用没有太大的问题,我们的项目里都是使用的这种方法。其注解方式比XML方式并不简单多少,只是少写了一些XML文件,让项目看起来更清爽一些。

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值