【项目实战】图书管理系统(mybatis)
一、项目概况
1.总述
项目为图书管理系统,项目使用maven作为管理工具,采用了java+mybatis框架+mysql。
2.技术栈选择
mybatis,jsp,mysql
3.环境介绍
数据库:mysql5.7.36
框架:mybatis
项目结构:maven3.6.2
语言:Java
jdk版本:jdk8.0
IDE:IDEA2020.2
插件:lombok、junit
jar包依赖:
4.功能概述
该图书管理系统使用了jsp技术实现前后端连接,利用网页的方式将数据呈现在用户面前,图形化的操作界面便于用户操作。
5.功能结构
6.项目结构
二、数据结构描述
1.实体类User
2.mysql数据库结构
库名:lms
表名:user
字段名 | 含义 | 数据类型 | 格式 |
---|---|---|---|
id | 用户编号 | INT(20) | 主键,not null,auto_increment |
user_code | 账号 | VARCHAR(30) | not null |
user_name | 用户名 | VARCHAR(10) | not null |
password | 密码 | VARCHAR(20) | not null |
gender | 性别 | INT(2) | not null |
birthday | 生日 | DATE | 无 |
phone | 电话 | VARCHAR(20) | not null |
user_role | 角色 | INT(2) | not null |
create_by | 创建者 | INT(2) | not null |
creation_date | 创建日期 | DATETIME | not null |
modify_by | 修改者 | INT(2) | 无 |
modify_date | 修改日期 | DATETIME | 无 |
3.mapper接口
三、程序模块描述
1.登陆模块
2.密码修改模块
3.用户列表展示模块
4.注册用户模块
5.用户查询模块
6.用户修改模块
7.用户删除模块
四、前端
前端原生jsp代码,使用少量的css和js添加了部分动态效果,前端仅供功能实现。
五、代码清单
1.实体类User(使用lombok注解开发)
package com.zhong.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.util.Date;
@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public class User {
private int id;//用户id
private String userCode;//用户账号
private String userName;//用户名
private String password;//用户密码
private Integer gender;//性别
private Date birthday;//生日
private String phone;//电话
private Integer userRole;//用户角色
private Integer createBy;//创建者
private Date creationDate;//创建日期
private Integer modifyBy;//修改者
private Date modifyDate;//修改日期
private String roleName;//用户角色名
private Integer age;//年龄
}
2.Mapper映射
1)接口类 UserMapper
package com.zhong.dao.user;
import com.zhong.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface UserMapper {
//通过账户名(唯一)查询用户
User getUserByUserCode(@Param("userCode")String userCode);
//通过id(唯一)获取用户
User getUserById(@Param("id")int id);
//条件查询用户。用户名称,性别,年龄,电话号码,用户角色(分页from和pageSize)
List<User> getUserList(Map map);
//增加用户
int addUser(User user);
//通过条件统计用户数,性别,用户名称,年龄,用户角色
int getUserCount(Map map);
//通过id删除用户
int deleteById(@Param("id")int id);
//修改某一个用户
int modify(User user);
//修改当前用户的密码
int updatePwd(@Param("id")int id, @Param("pwd") String pwd);
}
2)UserMapper.xml
<?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.zhong.dao.user.UserMapper">
<!--通过账户名(唯一)查询用户-->
<select id="getUserByUserCode" resultType="user">
select * from lms.user where user_code = #{userCode};
</select>
<!--通过id(唯一)获取用户-->
<select id="getUserById" resultType="user">
select * from lms.user where id = #{id};
</select>
<!--条件查询用户。用户名称,性别,年龄,电话号码,用户角色(分页from和pageSize)-->
<select id="getUserList" parameterType="map" resultType="user">
select * from lms.user,lms.role where user_role = role.id
<if test="userName != null">
and user_name = #{userName}
</if>
<if test="gender != null">
and gender = #{gender}
</if>
<if test="birthday != null">
and birthday = #{birthday}
</if>
<if test="phone != null">
and phone = #{phone}
</if>
<if test="userRole != null">
and user_role = #{userRole}
</if>
<trim prefix="limit">
<if test="from != null and pageSize != null">
#{from},#{pageSize}
</if>
</trim>
</select>
<!--增加用户-->
<insert id="addUser" parameterType="user">
insert into lms.user(id,user_code,user_name,password,gender,birthday,phone,user_role,create_by,creation_date)
values (#{id},#{userCode},#{userName},#{password},#{gender},#{birthday},#{phone},#{userRole},#{createBy},#{creationDate});
</insert>
<!--通过条件统计用户数,性别,用户名称,年龄,用户角色-->
<select id="getUserCount" parameterType="map" resultType="_int">
select count(1) from lms.user
<where>
<if test="gender != null">
and gender = #{gender}
</if>
<if test="userName != null">
and user_name = #{userName}
</if>
<if test="birthday != null">
and birthday = #{birthday}
</if>
<if test="userRole != null">
and user_role = #{userRole}
</if>
</where>
</select>
<!--通过id删除用户-->
<delete id="deleteById">
delete from lms.user where id = #{id};
</delete>
<!--修改某一个用户-->
<update id="modify" parameterType="user">
update lms.user
<set>
<if test="userCode != null">
user_code = #{userCode},
</if>
<if test="userName != null">
user_name = #{userName},
</if>
<if test="password != null">
`password` = #{password},
</if>
<if test="gender != null">
gender = #{gender},
</if>
<if test="birthday != null">
birthday = #{birthday},
</if>
<if test="phone != null">
phone = #{phone},
</if>
<if test="userRole != null">
user_role = #{userRole},
</if>
<if test="modifyBy != null">
modify_by = #{modifyBy},
</if>
<if test="modifyDate != null">
modify_date = #{modifyDate},
</if>
</set>
where id = #{id}
</update>
<!--修改当前用户的密码-->
<update id="updatePwd">
update lms.user set password = #{pwd} where id = #{id};
</update>
</mapper>
3.工具类
1.常量工具类
package com.zhong.utils;
//存放常量,便于后期修改维护
public class Constants {
public final static String USER_SESSION = "user";
public final static int PAGE_SIZE = 2;
}
2.sqlSession工具类
package com.zhong.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MybatisUtils {
//sqlSession工厂
private static SqlSessionFactory sqlSessionFactory;
//静态代码块,直接项目跑起来直接装载
static {
//设置要读取的资源路径
String resource = "mybatis-config.xml";
//声明一个输入流,方便后面读取
InputStream inputStream = null;
try {
//通过ibatis包下的资源读取的方法,通过提前设定好的路径将mybatis的核心配置文件读取进来,用来建造工厂
inputStream = Resources.getResourceAsStream(resource);
//new一个建造者,通过建造者创建一个带核心配置文件的工厂
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//通过工厂打开一个对数据库的会话
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
3.分页支持工具类
package com.zhong.utils;
public class PageSupport {
//当前页码-来自于用户输入
private int currentPageNo = 1;
//总数量(表)
private int totalCount = 0;
//页面容量
private int pageSize = 0;
//总页数-totalCount/pageSize(+1)
private int totalPageCount = 1;
public int getCurrentPageNo() {
return currentPageNo;
}
public void setCurrentPageNo(int currentPageNo) {
if(currentPageNo > 0){
this.currentPageNo = currentPageNo;
}
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
if(totalCount > 0){
this.totalCount = totalCount;
//设置总页数
this.setTotalPageCountByRs();
}
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
if(pageSize > 0){
this.pageSize = pageSize;
}
}
public int getTotalPageCount() {
return totalPageCount;
}
public void setTotalPageCount(int totalPageCount) {
this.totalPageCount = totalPageCount;
}
public void setTotalPageCountByRs(){
if(this.totalCount % this.pageSize == 0){
this.totalPageCount = this.totalCount / this.pageSize;
}else if(this.totalCount % this.pageSize > 0){
this.totalPageCount = this.totalCount / this.pageSize + 1;
}else{
this.totalPageCount = 0;
}
}
}
4.配置文件
1)db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/lms?useUnicode=true&&characterEncoding=UTF-8&&useSSL=false
user=root
password=123456
2)mybatis-config.xml
<?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>
<!--读取配置文件中的数据库连接信息-->
<properties resource="db.properties"/>
<settings>
<!--设置日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--开启驼峰命名,映射数据库到对象属性名-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--开启缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
<!--给实体类取别名,为了后面配置mapper.xml时不用写全限定名,方便使用-->
<typeAliases>
<!--包扫描-->
<package name="com.zhong.pojo"/>
</typeAliases>
<!--mybatis核心配置文件部分,配置使用的环境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--mapper.xml注册-->
<mappers>
<mapper resource="com/zhong/dao/book/BookMapper.xml"/>
<mapper resource="com/zhong/dao/role/RoleMapper.xml"/>
<mapper resource="com/zhong/dao/user/UserMapper.xml"/>
<mapper resource="com/zhong/dao/borrow/BorrowMapper.xml"/>
</mappers>
</configuration>
3)pom.xml
<?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>
<groupId>com.zhong</groupId>
<artifactId>lms</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!--web开发依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<!--数据库驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--jstl标签库依赖-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!--junit测试依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!--mybatis框架依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!--lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>
<!--配置文件导出,为了能够使用同一个包下的mapper.xml文件-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
六、测试中的遇到的问题、难点以及解决过程
1.参数传递
在前端和后端之间的交互中,前端通过el表达式取值,并且作用域不同首先取值也不同。后端通过方法参数获得前端的值,再通过在请求中携带参数,或者将参数放在session中让前端取得。这种间极容易报空指针异常。后端需要对前端传递过来的参数做校验,前端需要清楚后端传递的参数的作用范围,从对应的范围中取值,才能取到正确的值。
七、心得
项目开发累计6天,不上课的时候就开始写代码。最后一天下午写这个文档。整个项目累计千行代码(1714)。
第一个,自己一个人完成开发的项目。用于复习mybatis和jsp等。前后端交互中用户线完成度最高,其他线基本都是用户线的重复代码,业务逻辑基本一致。
遇到问题还是得面向百度编程,在调试的过程中思考程序怎么样运行,其中的前后顺序是什么,考虑参数之间如何传递,这都有助于我定位问题,思考解决方式。
不足是很多工作前期没有做好规划,等到后期开发一半了,已经很难再去推倒重做了,并且工具类封装度不够高,很多代码中共用了一套逻辑并没有抽象出来做工具类。常量封装也不足。