}
}
}
@Mapper
public interface BlogMapper {
@Insert(“INSERT INTO article(title, author) VALUES(#{title}, #{author})”)
int create(Blog blog);
}
@RestController
public class BlogController {
@Autowired
public BlogService blogService;
/**
-
通过id修改文章
-
@param blog
-
@return
*/
@PostMapping(value = “/updateById”)
public Object updateById(@RequestBody Blog blog) {
if (StringUtils.isBlank(blog.getTitle())) {
return ResponseUtil.fail(ResultEnums.BAD_ARGUMENT_VALUE.getCode(), “请输入文章标题”);
}
if (StringUtils.isBlank(blog.getAuthor())) {
return ResponseUtil.fail(ResultEnums.BAD_ARGUMENT_VALUE.getCode(), “请输入文章作者”);
}
return blogService.updateById(blog);
}
}
@Service
public class BlogServiceImpl implements BlogService {
@Autowired
private BlogMapper blogMapper;
@Override
public Object updateById(Blog blog) {
int count = blogMapper.updateById(blog);
if (count > 0) {
return ResponseUtil.ok(“修改成功”);
} else {
return ResponseUtil.fail(ResultEnums.SERIOUS.getCode(), “修改失败”);
}
}
}
@Mapper
public interface BlogMapper {
@Update(“UPDATE article SET title=#{title},author=#{author} WHERE id=#{id}”)
int updateById(Blog blog);
}
@RestController
public class BlogController {
@Autowired
public BlogService blogService;
/**
-
查询所有文章
-
@return
*/
@GetMapping(value = “/getAll”)
public List getAll() {
return blogService.getAll();
}
/**
-
通过标题查询文章
-
@param title
-
@return
*/
@GetMapping(value = “/getByTitle”)
public Object getByTitle(String title) {
if (StringUtils.isBlank(title)) {
return ResponseUtil.fail(ResultEnums.BAD_ARGUMENT_VALUE.getCode(), “请输入文章标题”);
}
return blogService.getByTitle(title);
}
}
@Service
public class BlogServiceImpl implements BlogService {
@Autowired
private BlogMapper blogMapper;
@Override
public List getAll() {
return blogMapper.getAll();
}
@Override
public List getByTitle(String title) {
return blogMapper.getByTitle(title);
}
}
@Mapper
public interface BlogMapper {
@Select(“SELECT * FROM article”)
@Results({
@Result(column = “id”, property = “id”, javaType = Integer.class),
@Result(property = “title”, column = “title”, javaType = String.class),
@Result(property = “author”, column = “author”, javaType = String.class)
})
List getAll();
@Select(“SELECT * FROM article WHERE title = #{title}”)
@Results({
@Result(column = “id”, property = “id”, javaType = Integer.class),
@Result(property = “title”, column = “title”, javaType = String.class),
@Result(property = “author”, column = “author”, javaType = String.class)
})
List getByTitle(String title);
}
@RestController
public class BlogController {
@Autowired
public BlogService blogService;
/**
-
通过ID删除文章
-
@param id
-
@return
*/
@PostMapping(value = “/deleteById”)
public Object deleteById(Integer id) {
if (null == id || 0 == id.longValue()) {
return ResponseUtil.fail(ResultEnums.BAD_ARGUMENT_VALUE.getCode(), “请输入文章id”);
}
return blogService.deleteById(id);
}
}
@Service
public class BlogServiceImpl implements BlogService {
@Autowired
private BlogMapper blogMapper;
@Override
public Object deleteById(Integer id) {
int count = blogMapper.deleteById(id);
if (count > 0) {
return ResponseUtil.ok(“删除成功”);
} else {
return ResponseUtil.fail(ResultEnums.SERIOUS.getCode(), “删除失败”);
}
}
}
@Mapper
public interface BlogMapper {
@Delete(“DELETE FROM article WHERE id = #{id}”)
int deleteById(Integer id);
}
========================================================================
修改application.properties 配置文件
#指定bean所在包
mybatis.type-aliases-package=com.fish.chapter6.domain
#指定映射文件
mybatis.mapperLocations=classpath:mapper/*.xml
@Mapper
public interface BlogMapper {
int create(Blog blog);
}
INSERT INTO article (title, author) VALUES (#{title}, #{author})
@Mapper
public interface BlogMapper {
int updateById(Blog blog);
}
UPDATE article SET title = #{title}, author = #{author} WHERE id = #{id}
@Mapper
public interface BlogMapper {
List getAll();
List getByTitle(@Param(“title”) String title);
}
select
from article
AND title = #{title,jdbcType=VARCHAR}
select
from article
@Mapper
public interface BlogMapper {
int deleteById(@Param(“id”) Integer id);
}
DELETE FROM article WHERE id = #{id}
更多mybatis数据访问操作的使用请参考:[MyBatis官方中文参考文档
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

读者福利
由于篇幅过长,就不展示所有面试题了,感兴趣的小伙伴
更多笔记分享
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!**
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

读者福利
由于篇幅过长,就不展示所有面试题了,感兴趣的小伙伴
[外链图片转存中…(img-KwwbxRC6-1713456291953)]
[外链图片转存中…(img-R1j0TesA-1713456291955)]
[外链图片转存中…(img-6RUg4CQX-1713456291958)]
更多笔记分享
[外链图片转存中…(img-nItagtuX-1713456291964)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!