在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写法,可考虑: 这个文章