在MyBatisPlus中添加注解信息即可

//实体类(此处需要添加对应的MyBatisPlus主键信息)
public class Notice{
    private int noticeId;
    private String release_date;
    private String content;
}
//方案一:自定义SQL(自增主键)
 @Insert("insert into notice(release_date,content)values(#{releaseDate},#{content})")
//keyProperty对象的属性,keyColumn数据库表主键
 @Options(useGeneratedKeys = true,keyProperty = "noticeId",keyColumn = "notice_id")
 int addNotice(Notice notice);
//方案二:MyBatisPlus的insert方法(自增主键)
 @TableId(value = "notice_id",type=IdType.AUTO);//需要在实体类的主键属性上添加此句
 private int noticeId;

测试

@Test
public void test2(){
    Notice notice1 = new Notice();
    notice1.setContent("我是测试数据3");
    notice1.setReleaseDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    //自定义SQL
    int notice = activityMapper.addNotice(notice1);
    System.out.println(notice+"------------>"+notice1);
}
@Test
public void test3(){
	Notice notice = new Notice();
    notice.setContent("封装的id");
    notice.setReleaseDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    int i = noticeMapper.insert(notice);
    System.out.println(i+"------->"+notice);
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐