基于 ssm+bootstrsp 简单极易上手的小型图书系统
效果图:
步骤
第一步 idea
下载 idea, 怎么下载安装配置 idea
第二步 创建项目
打开 idea —> File —> new —> project —> maven —> create from archetype —> maven-archetype-webapp —> 接下来一般默认即可 先放上项目结构图
第三步
配置 maven 依赖,aliyun 镜像可以下载到所有 jar 包
```
<groupId>cn.com.lightTrace</groupId>
<artifactId>BookSystem</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- 1.日志 -->
<!-- 实现slf4j接口并整合 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
</dependency>
<!-- 2.数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- DAO: MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>
<!-- 3.Servlet web -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- 4.Spring -->
<!-- 1)Spring核心 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- 2)Spring DAO层 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- 3)Spring web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>BookSystem_V0</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
```
第四步:
配置相关 Spring 文件,我是分成三个来配置的,比以往整合在一块清晰很多。 spring-dao.xml:
```
<!-- 2.数据库连接池 --> <bean id="dataSource" > <!-- 配置连接池属性 --> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- c3p0连接池的私有属性 --> <property name="maxPoolSize" value="30" /> <property name="minPoolSize" value="10" /> <!-- 关闭连接后不自动commit --> <property name="autoCommitOnClose" value="false" /> <!-- 获取连接超时时间 --> <property name="checkoutTimeout" value="10000" /> <!-- 当获取连接失败重试次数 --> <property name="acquireRetryAttempts" value="2" /> </bean> <!-- 3.配置SqlSessionFactory对象 --> <bean id="sqlSessionFactory" > <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource" /> <!-- 配置MyBaties全局配置文件:mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-config.xml" /> <!-- 扫描entity包 使用别名 --> <property name="typeAliasesPackage" value="cn.com.entity" /> <!-- 扫描sql配置文件:mapper需要的xml文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml" /> </bean> <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 --> <bean > <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <!-- 给出需要扫描Dao接口包 --> <property name="basePackage" value="cn.com.dao" /> </bean>
```
spring-service.xml: 这里的报错是 idea 的装配错误,不影响项目运行
```
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="ye2_d7a7_a71c4b2 org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置基于注解的声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
```
spring-web.xml:
```
<!-- 2.静态资源默认servlet配置
(1)加入对静态资源的处理:js,gif,png
(2)允许使用"/"做整体映射
-->
<mvc:default-servlet-handler/>
<!-- 3.配置jsp 显示ViewResolver -->
<bean class="zcd_ab6f_6f0b63d org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 4.扫描web相关的bean -->
<context:component-scan base-package="cn.com.controller" />
```
第五步,
配置 jdbc.properties ,注意换上你的数据库密码哦
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/booksystem?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=5201314
logback.xml ,比 log4j 配置更简单
```
mybatis-config.xml
```
第六步
在 resources 目录下创建 mapper 文件夹,配置 BookMapper.xml
```
<select id="queryAll" resultType="Book">
SELECT
book_id,
name,
number,
detail
FROM
book
ORDER BY
book_id
LIMIT #{offset}, #{limit}
</select>
<update id="updateBook" parameterType="Book">
UPDATE book
SET NAME = #{name},
NUMBER = #{number},
detail = #{detail}
WHERE
book_id = #{bookId}
AND number > 0
</update>
<insert id="addBook">
INSERT INTO book(`book_id`, `name`, `number`,`detail`)
VALUES(#{bookId}, #{name}, #{number}, #{detail})
</insert>
<delete id="deleteBookById">
DELETE FROM book WHERE book_id = #{bookId}
</delete>
```
第七步
创建 dao 层,这里是前面 mapper 的实现接口,是在 mapper namespace=”cn.com.dao.BookDao”定义好的
``` package cn.com.dao;
import cn.com.entity.Book; import java.util.List; import org.apache.ibatis.annotations.Param;
/ * ${DESCRIPTION} * * @author lightTrace * @create 2017-10-26 21:37 /
public interface BookDao {
Book queryById(long id);
List
第八步 service 层,接口:
``` package cn.com.service;
import cn.com.entity.Book;
import java.util.List;
/
* ${DESCRIPTION}
*
* @author lightTrace
* @create 2017-10-26 21:52
/
public interface BookService {
Book getById(long bookId);
List
service 实现:
``` package cn.com.service.impl;
import cn.com.dao.BookDao; import cn.com.entity.Book; import cn.com.service.BookService; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
/ * ${DESCRIPTION} * * @author lightTrace * @create 2017-10-26 21:53 / @Service public class BookServiceImpl implements BookService { @Autowired private BookDao bookDao;
@Override
public Book getById(long bookId) {
return bookDao.queryById(bookId);
}
@Override
public List<Book> getList(int start, int pageNum) {
return bookDao.queryAll(start, pageNum);
}
@Override
public int addBook(Book book) {
return bookDao.addBook(book);
}
@Override
public int updateBook(Book book) {
return bookDao.updateBook(book);
}
@Override
public int deleteBookById(long id) {
return bookDao.deleteBookById(id);
}
} ```
第九步,建立两个实体类:
Book 实体类:
``` package cn.com.entity;
/ * ${DESCRIPTION} * * @author lightTrace * @create 2017-10-26 21:38 / public class Book { private long bookId; private String name; private int number; private String detail;
@Override
public String toString() {
return "Book{" +
"bookId=" + bookId +
", name='" + name + '\'' +
", number=" + number +
", detail='" + detail + '\'' +
'}';
}
public long getBookId() {
return bookId;
}
public void setBookId(long bookId) {
this.bookId = bookId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
} ```
User 实体类:
``` package cn.com.entity;
/ * ${DESCRIPTION} * * @author lightTrace * @create 2017-10-28 11:15 / public class User { private String userName; //用户名 private String password; //密码 public String getUserName() { return userName; }
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
} ```
第十步,
在 test 建立测试类调试我们的 dao 层,良好的测试习惯在我们业务代码越来越庞大的时候能准确修改测试新的业务或者 debug。 首先 BaseTest:
``` import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/* * 配置spring和junit整合,junit启动时加载springIOC容器 spring-test,junit / @RunWith(SpringJUnit4ClassRunner.class) // 告诉junit spring配置文件 @ContextConfiguration({ "classpath:Spring/spring-dao.xml", "classpath:Spring/spring-service.xml" }) public class BaseTest {
} ```
BookDaoTest:
``` public class BookDaoTest extends BaseTest {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private BookDao bookDao;
@Test
public void addBook() {
for (int i = 0; i < 10; i++) {
Book book = new Book();
book.setDetail("描述" + i);
book.setName("活着" + i);
book.setNumber(i + 100);
int num = bookDao.addBook(book);
}
}
@Test
public void queryById() {
Book book = bookDao.queryById(1);
System.out.println(book);
}
@Test
public void queryAll() {
List<Book> books = bookDao.queryAll(1, 1000);
System.out.println(books);
}
@Test
public void updateBook() {
Book book = new Book();
book.setBookId(101);
book.setDetail("描述---修改");
book.setName("活着---修改");
book.setNumber(100);
int num = bookDao.updateBook(book);
}
@Test
public void deleteBookById() {
bookDao.deleteBookById(100);
}
} ```
此处有报错是 idea 的装配问题,不影响项目运行, 一定要运行 addBook 方法添加几本书的信息数据到数据库 ,然后再依次运行下面的测试类测试相关功能。
第十一步
做一个登录页面,因为 Tomcat 默认是 index.jsp,我就把登录功能放在 index.jsp 中,首先把 WEB-INF 下的 static 资源都复制进你的项目,WEB-INF 目录下的 index.jsp 如下:
``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
```
登录跳转的是 form action=”${pageContext.request.contextPath}/user/login 所以我们在 controller 文件夹下写个 UserController:
``` package cn.com.controller;
import cn.com.entity.User; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
/ * ${DESCRIPTION} * * @author lightTrace * @create 2017-10-28 11:23 / @Controller @RequestMapping("/user") public class UserController { private Logger logger = LoggerFactory.getLogger(this.getClass()); @RequestMapping(value = "login", method = RequestMethod.POST) public String login(User user, HttpServletRequest request) { String password = user.getPassword(); System.out.println(password); String result = null; if(password.equals("123")){ result ="list"; }else { result ="login"; } return result; } } ```
并配置 web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1" metadata-complete="true">
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>seckill-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springMVC需要加载的配置文件
spring-dao.xml,spring-service.xml,spring-web.xml
Mybatis - > spring -> springmvc
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring/spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>seckill-dispatcher</servlet-name>
<!-- 默认匹配所有的请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
十二步
同理完善图书管理系统的列表页和详情页,在 WEB-INF 目录下新建 JSP 目录,然后创建 list.jsp:
``` <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String appPath = request.getContextPath(); %>
图书管理系统
图书列表 显示当前图书库存信息
```
detail.jsp:
``` <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String appPath = request.getContextPath(); %>
图书管理系统 - by ssm基础框架
图书详情 显示当前图书详细信息
图书编号 | 图书名字 | 图书数量 |
---|---|---|
${requestScope.get('book').bookId} | ${requestScope.get('book').name} | ${requestScope.get('book').number} |
《${requestScope.get('book').name}》 ${requestScope.get('book').detail}
```
然后是 BookController,针对图书管理的详情页和列表页以及删除功能:
``` package cn.com.controller;
import cn.com.entity.Book; import cn.com.service.BookService; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody;
/ * ${DESCRIPTION} * * @author lightTrace * @create 2017-10-26 21:57 / @Controller @RequestMapping("/book") public class BookController { private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private BookService bookService;
@RequestMapping(value = "/list", method = RequestMethod.GET)
private String list(Model model) {
List<Book> list = bookService.getList(0, 1000);
model.addAttribute("list", list);
return "list";// WEB-INF/jsp/"list".jsp
}
@RequestMapping(value = "/detail/{bookId}", method = RequestMethod.GET)
private String detail(@PathVariable("bookId") Long bookId, Model model) {
Book book = bookService.getById(bookId);
model.addAttribute("book", book);
return "detail";
}
@RequestMapping(value = "/add", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
@ResponseBody
private String add(Book book) {
Book hasBook = bookService.getById(book.getBookId());
int i = -2;
if (hasBook == null) {
i = bookService.addBook(book);
}
return i > 0 ? "success" : "error";
}
@RequestMapping(value = "/del/{bookId}", method = RequestMethod.GET)
@ResponseBody
private String deleteBookById(@PathVariable("bookId") Long id) {
int i = bookService.deleteBookById(id);
return i > 0 ? "success" : "error";
}
} ```
到这里一个简单的系统就好了,很简单,可丰富和完善的地方很多,然后就放到 Tomcat 中跑访问 http://localhost:8080/ 就可以了。 idea 配置 Tomcat 步骤: https://jingyan.baidu.com/album/4ae03de3e120de3eff9e6b86.html?picindex=13
参考文献
- 基于SSH架构的高校自助化图书管理系统的设计与实现(大连理工大学·杨议)
- 基于Ajax的网上图书销售管理系统的设计与实现(南昌大学·李姗)
- 图书在线销售系统的设计与实现(电子科技大学·王淼)
- 基于JSP的网上书店交易系统的设计与实现(吉林大学·徐迎新)
- 基于SSH架构的高校自助化图书管理系统的设计与实现(大连理工大学·杨议)
- 基于ASP.NET技术的中山图书Web管理系统的设计与实现(江西财经大学·陈宇)
- 基于B/S模式的社区图书馆管理系统的设计与实现(大连理工大学·丁娟娟)
- 基于B/S结构的图书销售管理系统的设计与实现(吉林大学·杨兴越)
- 网上购书电子商务系统(吉林大学·谷丰)
- 网上购书电子商务系统(吉林大学·谷丰)
- 基于JSP技术的网上书店的开发与实现(电子科技大学·黄贻培)
- 网上书店系统的设计与实现(厦门大学·杨一雄)
- 基于Web的图书管理系统的设计与实现(西安电子科技大学·丁侃)
- 基于SSH架构的高校自助化图书管理系统的设计与实现(大连理工大学·杨议)
- 基于JSP的网上书店交易系统的设计与实现(吉林大学·徐迎新)
本文内容包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主题。发布者:毕业设计工坊 ,原文地址:https://m.bishedaima.com/yuanma/35794.html