ssm图书管理系统中的分类管理代码
时间: 2023-08-17 11:06:10 浏览: 192
以下是基于SSM框架的图书分类管理代码:
1. 实体类 Category.java
```java
public class Category {
private Integer id;
private String name;
// getter和setter方法省略
}
```
2. DAO接口 CategoryMapper.java
```java
public interface CategoryMapper {
int deleteByPrimaryKey(Integer id);
int insert(Category record);
int insertSelective(Category record);
Category selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Category record);
int updateByPrimaryKey(Category record);
List<Category> selectAll();
}
```
3. DAO实现类 CategoryMapper.xml
```xml
<mapper namespace="com.example.mapper.CategoryMapper">
<resultMap id="categoryMap" type="Category">
<id column="id" property="id" />
<result column="name" property="name" />
</resultMap>
<select id="selectAll" resultMap="categoryMap">
SELECT * FROM category
</select>
<select id="selectByPrimaryKey" resultMap="categoryMap" parameterType="java.lang.Integer">
SELECT * FROM category WHERE id = #{id}
</select>
<insert id="insert" parameterType="Category" useGeneratedKeys="true" keyProperty="id">
INSERT INTO category(name) VALUES(#{name})
</insert>
<update id="updateByPrimaryKeySelective" parameterType="Category">
UPDATE category SET name = #{name} WHERE id = #{id}
</update>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
DELETE FROM category WHERE id = #{id}
</delete>
</mapper>
```
4. Service接口 CategoryService.java
```java
public interface CategoryService {
List<Category> listAll();
Category findById(Integer id);
void saveOrUpdate(Category category);
void delete(Integer id);
}
```
5. Service实现类 CategoryServiceImpl.java
```java
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
private CategoryMapper categoryMapper;
@Override
public List<Category> listAll() {
return categoryMapper.selectAll();
}
@Override
public Category findById(Integer id) {
return categoryMapper.selectByPrimaryKey(id);
}
@Override
public void saveOrUpdate(Category category) {
if (category.getId() == null) {
categoryMapper.insert(category);
} else {
categoryMapper.updateByPrimaryKeySelective(category);
}
}
@Override
public void delete(Integer id) {
categoryMapper.deleteByPrimaryKey(id);
}
}
```
6. Controller类 CategoryController.java
```java
@Controller
@RequestMapping("/category")
public class CategoryController {
@Autowired
private CategoryService categoryService;
@GetMapping("/list")
public String list(Model model) {
List<Category> categoryList = categoryService.listAll();
model.addAttribute("categoryList", categoryList);
return "category/list";
}
@GetMapping("/edit")
public String edit(Integer id, Model model) {
Category category = null;
if (id != null) {
category = categoryService.findById(id);
} else {
category = new Category();
}
model.addAttribute("category", category);
return "category/edit";
}
@PostMapping("/saveOrUpdate")
public String saveOrUpdate(Category category) {
categoryService.saveOrUpdate(category);
return "redirect:/category/list";
}
@GetMapping("/delete")
public String delete(Integer id) {
categoryService.delete(id);
return "redirect:/category/list";
}
}
```
以上就是SSM框架中图书分类管理的代码,其中包括实体类、DAO接口和实现、Service接口和实现、Controller类等。需要注意的是,这里只是示例代码,具体实现可能会有所不同。
阅读全文