项目工具

  • IDEA 2021.1
  • Mybatis 3.5.2
  • maven 3.6.1
  • Tomcat 8.0
  • spring-webmvc 5.1.9
  • 更多请见下方核心配置

项目数据库设计

主键
admin admin_id
class_info class_id
book_info book_id
lend_list ser_num
reader_card reader_id
reader_info reader_id

admin表

名字 数据类型
admin_id bigint
password varchar(15)
username varchar(15)

class_info 表

名字 数据类型
class_id int
class_name varchar(15)

book_info表

名字 数据类型
book_id bigint(auto increment)
name varchar(20)
author varchar(15)
publish varchar(20)
ISBN varchar(15)
introduction text
language varchar(4)
price decimal(10,2)
pub_date date
class_id int
number int

lend_list表

名字 数据类型
ser_num bigint
book_id bigint
reader_id bigint
lend_date date
back_date date

reader_card表

名字 数据类型
reader_id bigint
username varchar(15)
password varchar(15)

reader_info表

名字 数据类型
reader_id bigint(auto increment)
name varchar(10)
sex varchar(2)
birth date
address varchar(50)
phone varchar(15)

项目结构

  • Java
    • controller
    • Mapper
    • pojo
    • service
  • resources
    • applicationContext.xml
    • database.properties
    • mybatis-config.xml
    • spring-dao.xml
    • spring-mvc.xml
    • spring-service.xml
  • web
    • css
    • fonts
    • js
    • images
    • WEB-INF

注解:web中的css、fonts、js文件为BootStrap文件,用于前端。

项目结构原理

一张图说清楚:

图片源自狂神说微信公众号

核心配置

一些题外话

在学Spring的时候,有人说这是配置地狱,起初我还不以为意,直到亲手做项目,才感受到什么叫不听老人言,吃亏在眼前。

为了让自己以后不再吃配置的苦,我将把配置源文件的代码放在这里。

发车了

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--Your project-->
<groupId>com.hu</groupId>
<artifactId>ssm_build_book</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.3</version>
</dependency>

<!--Servlet - JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>

<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>

spring-service.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

<!--scan service-->
<context:component-scan base-package="com.hu.service"/>

<!--service class added into Spring-->
<bean id="BooksServicelmpl" class="com.hu.service.BooksServicelmpl">
<property name="bookMapper" ref="bookMapper"/>
</bean>

<bean id="adminServicelmpl" class="com.hu.service.adminServicelmpl">
<property name="adminMapper" ref="adminMapper"/>
</bean>

<!--Declare thing configuration-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--add data source-->
<property name="dataSource" ref="dataSource"/>
</bean>

<!--aop-->

</beans>

spring-mvc.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1.note driver-->
<mvc:annotation-driven/>
<!--2.static resource filter-->
<mvc:default-servlet-handler/>
<!--3.scan controller-->
<context:component-scan base-package="com.hu.controller"/>
<!--view-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

</beans>

spring-dao.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--1.connect database file-->
<context:property-placeholder location="classpath:database.properties"/>

<!--2.datasource-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<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 private property -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- close connection not auto commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- get connection over time -->
<property name="checkoutTimeout" value="10000"/>
<!-- when connection failure restart counts -->
<property name="acquireRetryAttempts" value="2"/>

</bean>
<!--3.sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--bind mybatis equipment file-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

<!--equip dao interface scan-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--add sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--scan-->
<property name="basePackage" value="com.hu.Mapper"/>
</bean>
</beans>

mybatis-config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<typeAliases>
<package name="com.hu.pojo"/>
</typeAliases>

<mappers>
<mapper class="com.hu.Mapper.bookMapper"/>
<mapper class="com.hu.Mapper.adminMapper"/>
</mappers>

</configuration>

database.properties

1
2
3
4
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=100122200

applicationContext.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
<import resource="spring-mvc.xml"/>
</beans>

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
version="4.0">
<!--DisapatchServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!--complexcode filter-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Session-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>

index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<%--
Created by IntelliJ IDEA.
User: 10251
Date: 2021/6/11
Time: 10:30
To change this template use File | Settings | File Templates.
--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>邂逅海棠</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>自由而无用</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<style>
body{
padding-top: 50px;
}
.start{
padding:40px 15px;
text-align: center;
}
.xh{
padding-left: 415px;
margin-top: 80px;
}
</style>
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a href="#" class="navbar-brand">自由而无用</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">首页</a></li>
<li><a href="${pageContext.request.contextPath}/book/login">图书管理系统</a></li>
<li><a href="${pageContext.request.contextPath}/book/about">关于我</a></li>
</ul>
</div>
</div>
</nav>
<div class=" container xh">
<img src="images/计算机学院院徽.png" alt="..." class="img-circle">
</div>
<div class="container ">
<div class="start">
<h1 id="note1" class="text-primary">唯实惟新 至诚致志</h1>
<p class="lead">欢迎来到自由而无用</p>
</div>
</div>
</body>
</html>

一套示例代码(实现用户登录功能)

controller层:

  • adminController.class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.hu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/book")
public class Book_manage {
@RequestMapping("/book_manage")
public String get_Book_manage(){

return "book_manage";
}
}
  • loginController.class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.hu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/book")
public class loginController {
@RequestMapping("/login")
public String getAbout(){

return "login";
}
}

Mapper层:

  • adminMapper.class
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.hu.Mapper;

import com.hu.pojo.admin;
import com.hu.pojo.books;
import java.util.List;
public interface adminMapper {

int addadmin(admin admin);
int deleteadminById(int id);
int updateadmin(admin admin);
admin queryadminById(int id);
List<admin> queryAlladmin();
}
  • adminMapper.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hu.Mapper.adminMapper">
<insert id="addadmin" parameterType="admin">
insert into library.admin(admin_id,password,username) values(#{admin_id},#{password},#{username})
</insert>

<delete id="deleteadminById" parameterType="int">
delete
from library.admin
where admin_id = #{admin_id};
</delete>

<update id="updateadmin" parameterType="admin">
update library.admin
set admin_id = #{admin_id},password=#{password},username=#{username}
where admin_id=#{admin_id}
</update>

<select id="queryadminById" parameterType="int" resultType="admin">
select * from library.admin where admin_id = #{admin_id}
</select>

<select id="queryAlladmin" resultType="admin">
select * from library.admin
</select>
</mapper>

Service层:

  • adminService.class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.hu.service;

import com.hu.pojo.admin;

import java.util.List;

public interface adminService {
int addadmin(admin admin);
int deleteadminById(int id);
int updateadmin(admin admin);
admin queryadminById(int id);
List<admin> queryAlladmin();
admin checkLogin(int admin_id,String password);

}
  • adminServicelmpl.class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.hu.service;

import com.hu.Mapper.adminMapper;
import com.hu.Mapper.bookMapper;
import com.hu.pojo.admin;

import java.util.List;

public class adminServicelmpl implements adminService{

private adminMapper adminMapper;
public void setAdminMapper(adminMapper adminMapper){
this.adminMapper = adminMapper;
}
@Override
public int addadmin(admin admin) {
return adminMapper.addadmin(admin);
}

@Override
public int deleteadminById(int id) {
return adminMapper.deleteadminById(id);
}

@Override
public int updateadmin(admin admin) {
return adminMapper.updateadmin(admin);
}

@Override
public admin queryadminById(int id) {
return adminMapper.queryadminById(id);
}

@Override
public List<admin> queryAlladmin() {
return adminMapper.queryAlladmin();
}

@Override
public admin checkLogin(int admin_id, String password) {
admin admin = adminMapper.queryadminById(admin_id);
if(admin!=null&&admin.getPassword().equals(password)){
return admin;
}else{
return null;
}
}
}

web层:

login.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<%--
Created by IntelliJ IDEA.
User: 10251
Date: 2021/6/9
Time: 22:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图书管理系统</title>
<link rel="stylesheet" href="../css/bootstrap.min.css">
<script src="../js/JQuery.js"></script>
<style>
.form-control{
width: 443px;
}
.btn-success{
width: 150px;
}
.btn-warning{
width: 150px;
}
.form-group{
margin: 5px;
padding: 10px;
padding-left: 375px;
}
.form-horizontal{
padding-top: 180px;
}

</style>
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a href="#" class="navbar-brand">自由而无用</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="${pageContext.request.contextPath}">首页</a></li>
<li class="active"><a href="#">图书管理系统</a></li>
<li><a href="${pageContext.request.contextPath}/book/about">关于我</a></li>
</ul>
</div>
</div>
</nav>

<form action="${pageContext.request.contextPath}/book/book_manage_2" method="post" class="form-horizontal " role="form">
<div class="form-group">
<label class="col-sm-2 control-label">账号:</label>
<div class="col-sm-10">
<input id="zh" type="text" class="form-control" placeholder="账号" name="admin_id">
</div>
</div>
<div class="form-group">
<label id="pwd" class="col-sm-2 control-label">密码:</label>
<div class="col-sm-10">
<input id="pwd_real" type="password" class="form-control" placeholder="密码" name="password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox">记住密码
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-2">
<button id="login" type="submit" class="btn btn-success"> 登录 </button>
</div>
<div class=" col-sm-offset-5">
<button id="forget" type="submit" class="btn btn-warning">忘记密码</button>
</div>
</div>
</form>
<script type="text/javascript">
$("#login").click(function checkpwdAndzh(){
var name = $("#zh").val();
var pass = $("#pwd_real").val();
if (name == "") {
alert("用户名不能为空");
return false;
} else if (pass == "") {
alert("密码不能为空");
return false;
} else {
return true;
}
});
var v =0;
if((${v})=="-1")
alert("密码或账号错误");

</script>
</body>
</html>