多平台统一管理软件接口,如何实现多平台统一管理软件接口
275
2022-12-04
spring+mybatis实现图书管理系统
一、流程
jsp页面发起请求-->控制器-->控制器通过一个service对象调用service方法-->service中通过xxxMapper对象调用dao中的方法-->查询数据库
二、图书管理系统
1、目录结构
2、Book实体类
package com.entity;
import java.io.Serialihttp://zable;
import java.util.Map;
import org.apache.ibatis.type.Alias;
public class Book implements Serializable{
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String author;
private String bookconcern;
private String date;
private String synopsis;
private String pic;
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic == null ? null : pic.trim();
}
public Book() {
super();
}
public Book(Integer id, String name, String author,String bookconcern,String date,String synopsis,String pic) {
super();
this.id = id;
this.name = name;
this.author = author;
this.bookconcern = bookconcern;
this.date =date;
this.synopsis = synopsis;
this.pic = pic == null ? null : pic.trim();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getBookconcern() {
return bookconcern;
}
public void setBookconcern(String bookconcern) {
this.bookconcern = bookconcern;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getSynopsis() {
return synopsis;
}
public void setSGJsBiynopsis(String synopsis) {
this.synopsis = synopsis;
}
public String toString() {
return "Book [id=" + id + ", name=" + name + ", author="
+ author + ", bookconcern=" + bookconcern + ", date=" + date + ", synopsis=" + synopsis +", pic=" + pic+"]";
}
}
3、BookMapper增删改查接口
package com.dao;
import java.util.List;
import org.springframework.web.multipart.MultipartFile;
import com.entity.Book;
public interface BookMapper {
public Book getBookById(Integer id);
public List
public void insertBook(Book book);
public void deleteBookById(Integer id);
public void updateBook(Book book);
public Book findById(Integer id);
}
4、BookMapper.xml实现增删改查操作
select * from book
insert into book
(name,author,bookconcern,date,synopsis,pic)values(#{name},#{author},#{bookconcern},#{date},#{synopsis},#{pic});
delete from book
where id=#{id}
update book set
name=#{name},author=#{author},bookconcern=#{bookconcern},date=#{date},synopsis=#{synopsis},pic=#{pic} where
id=#{id}
select *
from book where id=#{id};
5、控制类
package com.controller;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import com.entity.Book;
import com.service.BookService;
@Controller
public class BookController {
@Autowired
BookService bookService;
@RequestMapping("/getbooks")
public String books(Map
List
map.put("allBooks", books);
return "allbooks";
}
@RequestMapping(value="/insert",produces="text/html;charset=UTF-8")
public String insert(){
return "insert";
}
@RequestMapping(value="/insertBook")
public String insertBook(Book book,MultipartFile book_pic) throws IllegalStateException, IOException{
String originalFilename = book_pic.getOriginalFilename();
if(book_pic!=null && originalFilename!=null && originalFilename.length()>0){
String pic_path = "E:\\spring_mybatis\\picture\\";
String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
File newFile = new File(pic_path+newFileName);
book_pic.transferTo(newFile);
book.setPic(newFileName);
}
bookService.insertBook(book);
return "redirect:getbooks";
}
@RequestMapping(value="/deleteBookById")
public String deleteBookById(Integer id){
bookService.deleteBookById(id);
return "redirect:getbooks";
}
@RequestMapping(value="/findById")
public String findById(Model model,Integer id){
Book book=bookService.findById(id);
model.addAttribute("book",book);
return "update";
}
@RequestMapping(value="/updateBook")
public String updateBook(Book book,MultipartFile book_pic) throws IllegalStateException, IOException{
String originalFilename = book_pic.getOriginalFilename();
if(book_pic!=null && originalFilename!=null && originalFilename.length()>0){
String pic_path = "E:\\spring_mybatis\\picture\\";
String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
File newFile = new File(pic_path+newFileName);
book_pic.transferTo(newFile);
book.setPic(newFileName);
}
bookService.updateBook(book);
return "redirect:getbooks";
}
}
6、BookService操作
package com.service;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.dao.BookMapper;
import com.entity.Book;
@Service //标识这是一个业务类
public class BookService {
@Autowired //用@Autowired将BookMapper的接口对象注入到spring中
private BookMapper bookMapper;
@Autowired
private SqlSession sqlSession;
public List
return bookMapper.getBooks();
}
public void insertBook(Book book) {
bookMapper.insertBook(book);
}
public void deleteBookById(Integer id) {
bookMapper.deleteBookById(id);
}
public void updateBook(Book book) {
bookMapper.updateBook(book);
}
public Book findById(Integer id) {
return bookMapper.findById(id);
}
}
7、mybatis配置文件mybatis-config.xml
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
8、spring配置文件applicationContext.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring" xmlns:tx="http://springframework.org/schema/tx" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd"> expression="org.springframework.stereotype.Controller" />
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:tx="http://springframework.org/schema/tx"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd">
expression="org.springframework.stereotype.Controller" />
expression="org.springframework.stereotype.Controller" />
9、连接数据库信息dbconfig.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/login?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
10、web.xml配置
11、spring mvc配置文件spring-servlet.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc" xsi:schemaLocation="http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd">
12、jsp页面
(1)index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
(2)allbooks.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
(3)insert.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
(4)update.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
13、所需的jar包
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~