基于ssm的图书管理系统代码
时间: 2023-07-25 08:22:06 浏览: 117
基于SSM的图书管理系统源码
5星 · 资源好评率100%
这里给您提供一个基于 SSM 的简单图书管理系统的代码示例,仅供参考。
1. 数据库设计
```
CREATE TABLE `book` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '图书id',
`name` varchar(100) NOT NULL COMMENT '图书名称',
`author` varchar(50) NOT NULL COMMENT '作者',
`price` double NOT NULL COMMENT '价格',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='图书表';
```
2. 实体类
```
public class Book {
private Integer id;
private String name;
private String author;
private Double price;
// getter和setter省略
}
```
3. Dao层
```
public interface BookDao {
List<Book> queryAll();
Book queryById(Integer id);
int add(Book book);
int update(Book book);
int delete(Integer id);
}
```
```
@Repository
public class BookDaoImpl implements BookDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<Book> queryAll() {
String sql = "SELECT * FROM book";
List<Book> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Book.class));
return list;
}
@Override
public Book queryById(Integer id) {
String sql = "SELECT * FROM book WHERE id=?";
Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Book.class), id);
return book;
}
@Override
public int add(Book book) {
String sql = "INSERT INTO book(name, author, price) VALUES (?, ?, ?)";
return jdbcTemplate.update(sql, book.getName(), book.getAuthor(), book.getPrice());
}
@Override
public int update(Book book) {
String sql = "UPDATE book SET name=?, author=?, price=? WHERE id=?";
return jdbcTemplate.update(sql, book.getName(), book.getAuthor(), book.getPrice(), book.getId());
}
@Override
public int delete(Integer id) {
String sql = "DELETE FROM book WHERE id=?";
return jdbcTemplate.update(sql, id);
}
}
```
4. Service层
```
public interface BookService {
List<Book> queryAll();
Book queryById(Integer id);
boolean add(Book book);
boolean update(Book book);
boolean delete(Integer id);
}
```
```
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public List<Book> queryAll() {
return bookDao.queryAll();
}
@Override
public Book queryById(Integer id) {
return bookDao.queryById(id);
}
@Override
public boolean add(Book book) {
int num = bookDao.add(book);
return num > 0;
}
@Override
public boolean update(Book book) {
int num = bookDao.update(book);
return num > 0;
}
@Override
public boolean delete(Integer id) {
int num = bookDao.delete(id);
return num > 0;
}
}
```
5. Controller层
```
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping("/list")
public ModelAndView list() {
ModelAndView mav = new ModelAndView("book/list");
List<Book> books = bookService.queryAll();
mav.addObject("books", books);
return mav;
}
@RequestMapping("/toAdd")
public ModelAndView toAdd() {
ModelAndView mav = new ModelAndView("book/add");
mav.addObject("book", new Book());
return mav;
}
@RequestMapping("/add")
public String add(Book book) {
boolean result = bookService.add(book);
if (result) {
return "redirect:/book/list";
} else {
return "error";
}
}
@RequestMapping("/toUpdate/{id}")
public ModelAndView toUpdate(@PathVariable("id") Integer id) {
ModelAndView mav = new ModelAndView("book/update");
Book book = bookService.queryById(id);
mav.addObject("book", book);
return mav;
}
@RequestMapping("/update")
public String update(Book book) {
boolean result = bookService.update(book);
if (result) {
return "redirect:/book/list";
} else {
return "error";
}
}
@RequestMapping("/delete/{id}")
public String delete(@PathVariable("id") Integer id) {
boolean result = bookService.delete(id);
if (result) {
return "redirect:/book/list";
} else {
return "error";
}
}
}
```
6. 页面
list.jsp
```
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>图书列表</title>
</head>
<body>
<h2>图书列表</h2>
<table border="1">
<tr>
<th>ID</th>
<th>名称</th>
<th>作者</th>
<th>价格</th>
<th>操作</th>
</tr>
<c:forEach items="${books}" var="book">
<tr>
<td>${book.id}</td>
<td>${book.name}</td>
<td>${book.author}</td>
<td>${book.price}</td>
<td>
<a href="/book/toUpdate/${book.id}">修改</a>
<a href="/book/delete/${book.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<br>
<a href="/book/toAdd">添加图书</a>
</body>
</html>
```
add.jsp
```
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加图书</title>
</head>
<body>
<h2>添加图书</h2>
<form action="/book/add" method="post">
图书名称:<input type="text" name="name"><br>
作者:<input type="text" name="author"><br>
价格:<input type="text" name="price"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
```
update.jsp
```
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改图书</title>
</head>
<body>
<h2>修改图书</h2>
<form action="/book/update" method="post">
<input type="hidden" name="id" value="${book.id}">
图书名称:<input type="text" name="name" value="${book.name}"><br>
作者:<input type="text" name="author" value="${book.author}"><br>
价格:<input type="text" name="price" value="${book.price}"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
```
以上就是一个基于 SSM 的简单图书管理系统的代码示例,希望对您有所帮助。
阅读全文