基于javaweb+mysql的springboot+mybatis新闻管理系统(java+html+springboot+javabean+mysql+tomcat)
以下是一个简化的Spring Boot + MyBatis新闻管理系统的核心代码示例。
实体类(News.java)
public class News {
private Integer id;
private String title;
private String content;
// 省略getter和setter方法
}
Mapper接口(NewsMapper.java)
@Mapper
public interface NewsMapper {
List<News> selectAllNews();
News selectNewsById(Integer id);
int insertNews(News news);
int updateNews(News news);
int deleteNews(Integer id);
}
Mapper XML(NewsMapper.xml)
<mapper namespace="com.example.demo.mapper.NewsMapper">
<select id="selectAllNews" resultType="News">
SELECT * FROM news
</select>
<select id="selectNewsById" resultType="News">
SELECT * FROM news WHERE id = #{id}
</select>
<insert id="insertNews">
INSERT INTO news(title, content) VALUES(#{title}, #{content})
</insert>
<update id="updateNews">
UPDATE news SET title = #{title}, content = #{content} WHERE id = #{id}
</update>
<delete id="deleteNews">
DELETE FROM news WHERE id = #{id}
</delete>
</mapper>
服务接口(NewsService.java)
public interface NewsService {
List<News> getAllNews();
News getNewsById(Integer id);
void saveNews(News news);
void updateNews(News news);
void deleteNews(Integer id);
}
服务实现类(NewsServiceImpl.java)
@Service
public class NewsServiceImpl implements NewsService {
@Autowired
private NewsMapper newsMapper;
@Override
public List<News> getAllNews() {
return newsMapper.selectAllNews();
}
@Override
public News getNewsById(Integer id) {
return newsMapper.selectNewsById(id);
}
@Override
public void saveNews(News news) {
newsMapper.insertNews(news);
}
@Override
public void updateNews(News news) {
newsMapper.updateNews(news);
}
@Override
public void deleteNews(Integer id) {
newsMapper.deleteNews(id);
}
}
控制器(NewsController.java)
@RestController
@RequestMapping("/news")
public class NewsController {
@Autowired
private NewsService newsService;
@GetMapping("/")
public List<News> getAllNews() {
return newsService.getAllNews();
}
@GetMapping("/{id}")
public News getNewsById(@PathVariable Integer id) {
return newsS
评论已关闭