springboot新闻表和新闻类型表的的增删改查怎么写
时间: 2024-05-10 20:17:56 浏览: 56
首先,需要定义对应的实体类和DAO接口。
假设新闻实体类为`News`,包含属性`id`、`title`、`content`、`typeId`等;新闻类型实体类为`NewsType`,包含属性`id`、`typeName`等。
News.java:
```java
public class News {
private Integer id;
private String title;
private String content;
private Integer typeId;
// getter、setter 省略
}
```
NewsType.java:
```java
public class NewsType {
private Integer id;
private String typeName;
// getter、setter 省略
}
```
DAO接口为`NewsDao`和`NewsTypeDao`。
NewsDao.java:
```java
public interface NewsDao {
void addNews(News news);
void deleteNews(Integer id);
void updateNews(News news);
News getNewsById(Integer id);
List<News> getNewsList();
}
```
NewsTypeDao.java:
```java
public interface NewsTypeDao {
void addNewsType(NewsType newsType);
void deleteNewsType(Integer id);
void updateNewsType(NewsType newsType);
NewsType getNewsTypeById(Integer id);
List<NewsType> getNewsTypeList();
}
```
接着,在DAO接口中定义好增删改查的方法之后,需要实现这些方法。
NewsDaoImpl.java:
```java
@Repository
public class NewsDaoImpl implements NewsDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void addNews(News news) {
String sql = "insert into news(title, content, typeId) values (?, ?, ?)";
jdbcTemplate.update(sql, news.getTitle(), news.getContent(), news.getTypeId());
}
@Override
public void deleteNews(Integer id) {
String sql = "delete from news where id = ?";
jdbcTemplate.update(sql, id);
}
@Override
public void updateNews(News news) {
String sql = "update news set title = ?, content = ?, typeId = ? where id = ?";
jdbcTemplate.update(sql, news.getTitle(), news.getContent(), news.getTypeId(), news.getId());
}
@Override
public News getNewsById(Integer id) {
String sql = "select * from news where id = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(News.class));
}
@Override
public List<News> getNewsList() {
String sql = "select * from news";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(News.class));
}
}
```
NewsTypeDaoImpl.java:
```java
@Repository
public class NewsTypeDaoImpl implements NewsTypeDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void addNewsType(NewsType newsType) {
String sql = "insert into news_type(typeName) values (?)";
jdbcTemplate.update(sql, newsType.getTypeName());
}
@Override
public void deleteNewsType(Integer id) {
String sql = "delete from news_type where id = ?";
jdbcTemplate.update(sql, id);
}
@Override
public void updateNewsType(NewsType newsType) {
String sql = "update news_type set typeName = ? where id = ?";
jdbcTemplate.update(sql, newsType.getTypeName(), newsType.getId());
}
@Override
public NewsType getNewsTypeById(Integer id) {
String sql = "select * from news_type where id = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(NewsType.class));
}
@Override
public List<NewsType> getNewsTypeList() {
String sql = "select * from news_type";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(NewsType.class));
}
}
```
最后,在Service层中调用DAO中的方法即可。
NewsServiceImpl.java:
```java
@Service
public class NewsServiceImpl implements NewsService {
@Autowired
private NewsDao newsDao;
@Override
public void addNews(News news) {
newsDao.addNews(news);
}
@Override
public void deleteNews(Integer id) {
newsDao.deleteNews(id);
}
@Override
public void updateNews(News news) {
newsDao.updateNews(news);
}
@Override
public News getNewsById(Integer id) {
return newsDao.getNewsById(id);
}
@Override
public List<News> getNewsList() {
return newsDao.getNewsList();
}
}
```
NewsTypeServiceImpl.java:
```java
@Service
public class NewsTypeServiceImpl implements NewsTypeService {
@Autowired
private NewsTypeDao newsTypeDao;
@Override
public void addNewsType(NewsType newsType) {
newsTypeDao.addNewsType(newsType);
}
@Override
public void deleteNewsType(Integer id) {
newsTypeDao.deleteNewsType(id);
}
@Override
public void updateNewsType(NewsType newsType) {
newsTypeDao.updateNewsType(newsType);
}
@Override
public NewsType getNewsTypeById(Integer id) {
return newsTypeDao.getNewsTypeById(id);
}
@Override
public List<NewsType> getNewsTypeList() {
return newsTypeDao.getNewsTypeList();
}
}
```
阅读全文