MyBatis注解式开发映射语句详解

2023-05-16 0 3,655

目录

前言

MyBatis中也提供了注解式开发⽅式,采⽤注解可以减少Sql映射⽂件的配置。 当然,使⽤注解式开发的话,sql语句是写在java程序中的,这种⽅式也会给sql语句的维护带来成本。

官⽅是这么说的:

MyBatis注解式开发映射语句详解

使⽤注解编写复杂的SQL是这样的:

@Update(\"<script> update table_name set grade=\'三年级\'”+
\" <if test=\\ \"name != null\\\"> , name = #{name} </if> ”+
\" <if test=\\ \"sex != null\\\"> , sex = #{sex}</if>”+
\" where num = #{num}</script>\")
void update(Student student);

原则:简单sql可以注解,复杂sql使⽤xml!使用注解式开发以后三兄弟之一的SqlMapper.xml文件就不需要了!

MyBatis注解式开发映射语句详解

1. @Insert注解

二兄弟之一CarMapper接口,用来编写方法

使用@Insert的注解方式,在注解上就可以写上SQL语句,对于SQL语句当中的变量就是pojo类Car对应的变量名

package com.bjpowernode.mybatis.mapper;
import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Insert;
public interface CarMapper {
    // 使用注解式开发,插入数据
    @Insert(\"insert into t_car values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})\")
    int insert(Car car);
}

二兄弟之二CarMapperTest,用来测试

package com.bjpowernode.mybatis.test;
import com.bjpowernode.mybatis.mapper.CarMapper;
import com.bjpowernode.mybatis.pojo.Car;
import com.bjpowernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
public class CarMapperTest {
    @Test
    public void testInsert(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        // 创建Car对象
        Car car = new Car(null, \"666\", \"丰田霸道\", 32.0, \"2023-1-9\", \"燃油车\");
        int count = mapper.insert(car);
        System.out.println(count);
        sqlSession.commit();
        sqlSession.close();
    }
}

执行结果:

MyBatis注解式开发映射语句详解

2. @Delete注解

二兄弟之一CarMapper接口,用来编写方法

package com.bjpowernode.mybatis.mapper;
import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Insert;
public interface CarMapper {
    // 使用注解式开发,删除数据
    @Delete(\"delete from t_car where id = #{id}\")
    int deleteById(Long id);
}

二兄弟之二CarMapperTest,用来测试

package com.bjpowernode.mybatis.test;
import com.bjpowernode.mybatis.mapper.CarMapper;
import com.bjpowernode.mybatis.pojo.Car;
import com.bjpowernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
public class CarMapperTest {
   @Test
    public void testDeleteById(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        int count = mapper.deleteById(40L);
        System.out.println(count);
        sqlSession.commit();
        sqlSession.close();
    }
}

执行结果:

MyBatis注解式开发映射语句详解

3. @Update注解

二兄弟之一CarMapper接口,用来编写方法

package com.bjpowernode.mybatis.mapper;
import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Insert;
public interface CarMapper {
    // 使用注解式开发,更新数据
    @Update(\"update t_car set car_num=#{carNum},brand=#{brand},guide_price=#{guidePrice},produce_time=#{produceTime},car_type=#{carType} where id = #{id}\")
    int update(Car car);
}

二兄弟之二CarMapperTest,用来测试

package com.bjpowernode.mybatis.test;
import com.bjpowernode.mybatis.mapper.CarMapper;
import com.bjpowernode.mybatis.pojo.Car;
import com.bjpowernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
public class CarMapperTest {
   @Test
    public void testUpdate(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        // 创建Car对象,根据id进行更新
        Car car = new Car(34L, \"666\", \"丰田霸道\", 32.0, \"2023-1-9\", \"燃油车\");
        int count = mapper.update(car);
        System.out.println(count);
        sqlSession.commit();
        sqlSession.close();
    }
}

执行结果:

MyBatis注解式开发映射语句详解

4. @Select注解

二兄弟之一CarMapper接口,用来编写方法

package com.bjpowernode.mybatis.mapper;
import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Insert;
public interface CarMapper {
    // 使用注解式开发,查询数据
    @Select(\"select * from t_car where id = #{id}\")
    Car selectById(Long id);
}

二兄弟之二CarMapperTest,用来测试

package com.bjpowernode.mybatis.test;
import com.bjpowernode.mybatis.mapper.CarMapper;
import com.bjpowernode.mybatis.pojo.Car;
import com.bjpowernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
public class CarMapperTest {
   @Test
    public void testSelectById(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = mapper.selectById(41L);
        System.out.println(car);
        sqlSession.close();
    }
}

执行结果:

MyBatis注解式开发映射语句详解

5. @Results注解

我们知道数据库表中的字段和pojo类的属性名有的是不一样的,我们之所以能够完整的查出数据,是因为在核心配置文件mybatis-config.xml当中配置了:启用驼峰命名⾃动映射

    <!--启⽤驼峰命名⾃动映射-->
    <settings>
        <setting name=\"mapUnderscoreToCamelCase\" value=\"true\"/>
    </settings>

如果我们不启用,不对应的字段就是null,查询的数据如下:

MyBatis注解式开发映射语句详解

那还有什么办法呢?还可以使用@Results注解!

注:从这里也能看出,使用注解的方式开发,对于简单点的SQL还行,对于稍微复杂的查询语句就太麻烦了!

package com.bjpowernode.mybatis.mapper;
import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.*;
public interface CarMapper {
    // 使用注解式开发,查询数据
    @Select(\"select * from t_car where id = #{id}\")
    @Results({
            @Result(property = \"id\",column = \"id\"),
            @Result(property = \"carNum\",column = \"car_num\"),
            @Result(property = \"brand\",column = \"brand\"),
            @Result(property = \"guidePrice\",column = \"guide_price\"),
            @Result(property = \"produceTime\",column = \"produce_time\"),
            @Result(property = \"carType\",column = \"car_type\"),
    })
    Car selectById(Long id);
}

这样计算我们不启用驼峰命名⾃动映射,也能正常查询数据

MyBatis注解式开发映射语句详解

结语:直到今天MyBatis的学习就完美撒花了,接下来就开始Spring的学习,敬请期待!

资源下载此资源下载价格为1小猪币,终身VIP免费,请先
由于本站资源来源于互联网,以研究交流为目的,所有仅供大家参考、学习,不存在任何商业目的与商业用途,如资源存在BUG以及其他任何问题,请自行解决,本站不提供技术服务! 由于资源为虚拟可复制性,下载后不予退积分和退款,谢谢您的支持!如遇到失效或错误的下载链接请联系客服QQ:442469558

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 MyBatis注解式开发映射语句详解 http://www.20zxx.cn/704951/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务