利用ssm框架搭建书本管理代码
时间: 2023-08-06 15:25:15 浏览: 109
很抱歉,由于篇幅限制,无法在此完整地呈现利用SSM框架搭建书本管理的所有代码。不过,我可以提供一个简单的示例代码,供你参考:
1. 实体类Book.java
```java
public class Book {
private int id;
private String name;
private String author;
private String publisher;
// 省略getter和setter方法
}
```
2. Mapper接口BookMapper.java
```java
public interface BookMapper {
List<Book> getAllBooks();
Book getBookById(int id);
void addBook(Book book);
void updateBook(Book book);
void deleteBook(int id);
}
```
3. Mapper映射文件BookMapper.xml
```xml
<!-- namespace定义Mapper接口的全限定名 -->
<mapper namespace="com.example.mapper.BookMapper">
<!-- 查询所有书本信息 -->
<select id="getAllBooks" resultType="com.example.entity.Book">
select * from book
</select>
<!-- 根据id查询书本信息 -->
<select id="getBookById" parameterType="int" resultType="com.example.entity.Book">
select * from book where id = #{id}
</select>
<!-- 添加书本信息 -->
<insert id="addBook" parameterType="com.example.entity.Book">
insert into book (name, author, publisher) values (#{name}, #{author}, #{publisher})
</insert>
<!-- 更新书本信息 -->
<update id="updateBook" parameterType="com.example.entity.Book">
update book set name = #{name}, author = #{author}, publisher = #{publisher} where id = #{id}
</update>
<!-- 根据id删除书本信息 -->
<delete id="deleteBook" parameterType="int">
delete from book where id = #{id}
</delete>
</mapper>
```
4. Service接口BookService.java
```java
public interface BookService {
List<Book> getAllBooks();
Book getBookById(int id);
void addBook(Book book);
void updateBook(Book book);
void deleteBook(int id);
}
```
5. Service实现类BookServiceImpl.java
```java
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookMapper bookMapper;
@Override
public List<Book> getAllBooks() {
return bookMapper.getAllBooks();
}
@Override
public Book getBookById(int id) {
return bookMapper.getBookById(id);
}
@Override
public void addBook(Book book) {
bookMapper.addBook(book);
}
@Override
public void updateBook(Book book) {
bookMapper.updateBook(book);
}
@Override
public void deleteBook(int id) {
bookMapper.deleteBook(id);
}
}
```
6. Controller类BookController.java
```java
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping("/list")
public String list(Model model) {
List<Book> books = bookService.getAllBooks();
model.addAttribute("books", books);
return "book_list";
}
@RequestMapping("/toAdd")
public String toAdd() {
return "book_add";
}
@RequestMapping("/add")
public String add(Book book) {
bookService.addBook(book);
return "redirect:/book/list";
}
@RequestMapping("/toUpdate")
public String toUpdate(int id, Model model) {
Book book = bookService.getBookById(id);
model.addAttribute("book", book);
return "book_update";
}
@RequestMapping("/update")
public String update(Book book) {
bookService.updateBook(book);
return "redirect:/book/list";
}
@RequestMapping("/delete")
public String delete(int id) {
bookService.deleteBook(id);
return "redirect:/book/list";
}
}
```
7. 前台页面book_list.jsp、book_add.jsp和book_update.jsp
略。
以上是一个简单的示例代码,希望对你有所帮助。
阅读全文