mybatis用注解替换xml,不再写.xml了

在Service里引用
    @Autowired
    private DemoMapper demoMapper;

下面展示一些 DemoMapper.java代码片

public interface DemoMapper{

 	/**
     * 查询页面信息-根据ID
     */
    @Select("select * from program where id = #{id}")
    Program getById(Long id);

    /**
     * 新增
     */
    @Insert({
            "<script> ",
            "  insert into program\n" +
                    "        <trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n" +
                    "            <if test=\"name != null and name != ''\">name,</if>\n" +
                    "            <if test=\"inDevId != null\">inDevId,</if>\n" +
                    "        </trim>\n" +
                    "        <trim prefix=\"values (\" suffix=\")\" suffixOverrides=\",\">\n" +
                    "            <if test=\"name != null and name != ''\">#{name},</if>\n" +
                    "            <if test=\"inDevId != null\">#{inDevId},</if>\n" +
                    "        </trim>",
            "</script>"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Program program);


}

用<script>标签就可以写<if>这些标签哦

@Options(useGeneratedKeys = true, keyProperty = "id") 是返回最新自增主键id

其实就是用注解方式替换.xml写法,可考虑: 这个文章

相关推荐
╰つ゛木槿3 小时前
Spring Boot 调用DeepSeek API的详细教程
java·spring boot·后端·deepseek
hhw1991124 小时前
c#面试题整理6
java·开发语言·c#
就叫飞六吧4 小时前
WangEditor快速实现版
node.js·mybatis
程序视点5 小时前
SpringBoot配置入门
java·spring boot·spring
Benaso5 小时前
Java,Golang,Rust 泛型的大体对比小记
java·golang·rust
程序员清风5 小时前
什么时候会考虑用联合索引?如果只有一个条件查就没有建联合索引的必要了么?
java·后端·面试
Seven975 小时前
【设计模式】掌握建造者模式:如何优雅地解决复杂对象创建难题?
java·后端·设计模式
自在如风。6 小时前
MyBatis-Plus 使用技巧
java·mybatis·mybatis-plus
XORE956 小时前
IDEA Generate POJOs.groovy 踩坑小计 | 生成实体 |groovy报错
java·spring·intellij-idea
heart000_16 小时前
基于SpringBoot的智能问诊系统设计与隐私保护策略
java·spring boot·后端