【Mybatis 框架(自学)】Day06--2022/3/13

使用注解开发

注解仅应用于简单的sql语句

//方法很简单 无需用Mapper进行映射即可实现


public interface StudentMapper {
    //查找所有学生方法
    @Select("select * from tbstudent")
    List<Student> selectStudent();

    //添加学生方法:Int 或 Boolean 判断插入是否为真
    @Insert("insert into tbstudent (name,gender,phone) values(#{name},#{gender},#{phone})")
    int addStudent(Student student);

    //删除学生方法:Boolean 判断是否删除
    @Delete("delete from tbstudent where id = #{Sid}")
    boolean delStudent(@Param("Sid") int id);

    //修改学生方法:
    @Update("update tbstudent set name =#{name},gender =#{gender},phone =#{phone} where id =#{id}")
    int updateStudent(Student student);

    //查询学生方法:索引值查询
    @Select("select * from tbstudent where id =#{id}")
    List<Student> selectStudentById(@Param("id") Integer id);

    //查询学生方法:模糊查询
    @Select("select * from tbstudent where name like #{name}")
    List<Student> selectStudentByName(@Param("name") String name);

    //查询学生方法:Limit分页查询
    @Select("select * from tbstudent limit #{startIndex},#{pageSize}")
    List<Student> selectStudentByLimit(Map<String,Object> map);

    //查询学生方法:RowBounds查询
    @Select("select * from tbstudent")
    List<Student> selectStudentByRowBounds();
}

自动提交事务

//在MybatisUtils工具类中 可以自动设置事务的提交
public static SqlSession getConnection(){
    
 	 return sqlSessionFactory.openSession(true);//如果为true,则事务自动提交
    
}

Param()注解

使用它,可以更加规范代码,进行junit测试时,程序运行时,Param()的优先级最高

而其内容则是基本数据类型的参数或String类型

  • 基本数据类型 或 String类型 需要加上
  • 引用数据类型无需Param()
  • 如果只有一个基本数据类型可以忽略,但建议加上
  • 当我们在Sql中引用的字段就是Param()中的属性
	@Delete("delete from tbstudent where id = #{Sid}")
    boolean delStudent(@Param("Sid") int id);

LomBok

一个框架,实体类当中的getter与setter等自动生成,自行百度


posted @   VVS,冷漠的心  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

囚鸟该如何超越今生?

点击右上角即可分享
微信分享提示