MyBatis 基本操作 - 注解版

目录

一,查询 - @select

1.1 全列查询

1.2 指定列查询

1.3 赋值问题

方法一:起别名

方法二:结果映射

方法三:添加配置

二,新增 - @Insert

2.1 使用对象插入

2.2 获取主键

三,删除 - @Delete

四,修改 - @Update 

补:MyBatis 的配置


一,查询 - @select

1.1 全列查询

@Mapper
public interface UserInfoMapper {
    @Select("select * from userInfo")
    public UserInfo queryAll();
}

1.2 指定列查询

@Mapper
public interface UserInfoMapper {
    //注意:MyBatis中方法名不允许重复!!!!
    //当只有一个参数时,sql参数与方法参数可以不同
    @Select("select * from userInfo where id = #{id}")
    public UserInfo queryUserInfoById(Integer id);

    //当需要对参数进行重命名时需要使用@Param注释
    //如果不使用@Param,那么sql参数与方法参数必须相同
    //注:如果使用阿里云创建的springboot项目,必须使用@Param注解
    @Select("select * from userInfo where id = #{id} and username = #{username}")
    public UserInfo queryUserInfo(@Param("id") Integer i, @Param("username") String name);
}

 注:加了@Param注解后,sql语句中的参数名只能与@Param()的参数名匹配!!!

1.3 赋值问题

可以发现,在赋值时,deleteFlag,createTime,updateTime 并没有被赋值,这是为什么?原因分析:

  • 当⾃动映射查询结果时,MyBatis 会获取结果中返回的列名并在 Java 类中查找相同名字的属性(忽略⼤⼩写)。 这意味着如果发现了 ID 列和 id 属性,MyBatis 会将列 ID 的值赋给 id 属性
  • 所以没有赋值的原因就是MySQL中的字段名与Java对象中的属性名不相同

 

方法一:起别名

    @Select("select id, username, password, age, gender, phone," +
            "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from userInfo " +
            "where id = #{id} and username = #{username}")
    public UserInfo queryUserInfo(@Param("id") Integer i, @Param("username") String name);

方法二:结果映射

    @Select("select * from userInfo where id = #{id}")
    @Results(id = "resultMap",value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    public UserInfo queryUserInfo(@Param("id") Integer i, @Param("username") String name);


    @Select("select * from userInfo where id = #{id}")
    @ResultMap(value = "resultMap")
    //使用过@Results注解后,其他方法可以直接使用@ResultMap注解获得相同效果
    public UserInfo queryUserInfoById(Integer id);

方法三:添加配置

mybatis:
  configuration:
    map-underscore-to-camel-case: true #驼峰自动转换

二,新增 - @Insert

2.1 使用对象插入

使用多个参数插入数据与上述查询的使用方法一样,这里不过多赘述,这里讲一下如何使用对象插入数据:

@Mapper
public interface UserInfoMapper {
    //这里演示一下使用对象插入
    @Insert("insert into userInfo (username, password, age, gender, phone) " +
            "values (#{username}, #{password}, #{age}, #{gender}, #{phone})")
    public Integer insert(UserInfo userInfo);

    //使用@Param注解
    @Insert("insert into userInfo (username, password, age, gender, phone) " +
            "values (#{userInfo.username}, #{userInfo.password}, #{userInfo.age}, #{userInfo.gender}, #{userInfo.phone})")
    public Integer insertByParam(@Param("userInfo") UserInfo userInfo);
    
    //这里的返回值是成功修改了几行数据,也可以返回void
}

2.2 获取主键

在有些情景中,我们需要获取到新插入数据的 id,如果想要拿到自增 id,需要在Mapper接口方法上添加一个@Option注解:

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userInfo (username, password, age, gender, phone) " +
            "values (#{username}, #{password}, #{age}, #{gender}, #{phone})")
    public Integer insert(UserInfo userInfo);
  • useGeneratedKeys = true:表示告诉MyBatis使用JDBC的getGeneratedKeys方法来检索数据库内部生成的主键(如果数据库支持的话)
  • keyProperty = ''id'':这个属性指定了userInfo对象中的 id 属性接收数据库生成的主键

三,删除 - @Delete

    @Delete("delete from userInfo where id = #{id}")
    public Integer deleteById(Integer id);

四,修改 - @Update 

    @Update("update userInfo set username = #{username} where id = #{id}")
    public Integer updateById(@Param("username") String username, @Param("id") Integer id);

补:MyBatis 的配置

mybatis:
  configuration: # 配置打印 MyBatis⽇志
    map-underscore-to-camel-case: true #驼峰自动转换
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql语句
  mapper-locations: classpath:mapper/**Mapper.xml #使用xml操作数据库时会用到

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一叶祇秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值