JavaWeb项目之某某管理系统

之所以叫某某管理系统,是因为此项目只做了基本的增删改查操作,具体用于什么系统可根据具体情况进行更改

此项目基于Maven工程,所学知识都来源自狂神的视频

一、所用技术

mysql,servlet,由于学艺不精加上时间所限ssm框架只用上了mybatis和javaspring。

二、项目准备工作

2.1创建好一个maven项目,在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.cat</groupId>
  <artifactId>MyTest-01</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.2</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <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>
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp.jstl</groupId>
      <artifactId>jstl-api</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>RELEASE</version>
      <scope>compile</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.61</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.10</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.3.10</version>
    </dependency>

    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.13</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.3</version>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.16.10</version>
    </dependency>


  </dependencies>


  <!--在build中配置resources,来防止我们资源导出失败的问题-->
  <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>

2.2 项目所用数据库

数据库名为mybatis,其中只有一个表,表名为user

 2.3连接数据库

我所用的编辑器为idea,在idea中Database中点击加号连接mysql并选择好对应的数据库

 填好数据库名字和密码后点击连接,并点解Schemas选择好对应的数据库

 

 

 2.4 项目工程目录

1:UserMapper为持久层接口,UserMapper.xml为映射类,UserMapperImpl是加在UserMapper与UserDao之间的实现类,用于被javaspring容器接管,书写mybatis映射的代码

2:UserDao接口与UserDaoImpl实现类,用于创建spring容器,管理UserMapperImpl对象

3:CharacterEncodingFilter为过滤器

4:pojo为实体类,对应数据库中的user表

5:余下的分别是逻辑业务层和表现层

2.5 配置Tomcat

 

 

2.6 编写Mybatis与Spring的配置文件

 mybatis:由于spring配置文件已经把数据源进行配置绑定,因此Mybatis处不需要编写数据源

<?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核心配置文件-->
<configuration>

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

</configuration>

spring:

<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">


<!--数据源DataSource:使用spring的数据源对mybatis的配置进行替换-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

<!--SqlsessionFatory-->
    <bean id="sqlSessionFatory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--绑定数据源-->
        <property name="dataSource" ref="dataSource"/>
<!--绑定mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/cat/dao/*.xml"/>
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--所以通过构造器注入-->
        <constructor-arg index="0" ref="sqlSessionFatory"/>
    </bean>

    <bean id="userMapper" class="com.cat.dao.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>

<!--dao层-->
    <bean id="userDao" class="com.cat.dao.UserDaoImpl">

    </bean>

<!--service层-->
    <bean id="userService" class="com.cat.service.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>

</beans>

三、功能实现

3.1实现登录功能

  • 编写前端页

 其中的i标签是引入的icon矢量图片,${error}为预留的错误提示处

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>Login</title>
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css"/>
<%--icon矢量图引入--%>
    <link rel="stylesheet" href="http://at.alicdn.com/t/font_2837868_r3mvnz3f1c.css">
</head>
<body>

<form action="UserLogin.java" method="post">
    <div class="login-first-div">
            <h1>Login</h1>

            <div class="form">
                <div class="info">${error}</div>
                <div class="userinput">
                <i class="iconfont icon-yonghuming" aria-hidden="true"></i>
                <input type="text" placeholder="username" name="username">
            </div>

            <div class="userinput">
                <i class="iconfont icon-mima" aria-hidden="true"></i>
                <input type="password" placeholder="password" name="password">
            </div>
        </div>

        <div class="login-button">
            <input type="submit" value="登录" class="subbutton"/>
            <input type="reset" value="重置" class="rebutton"/>
        </div>
    </div>
</form>
</body>
</html>

css代码

body{
    background: url("../img/img1.png");
    background-size: 100% auto;
    background-repeat: no-repeat;
}

/*登录div*/
.login-first-div{
    border: 1px solid ;
    width: 27%;
    text-align: center;
    margin: 0 auto;
    padding: 20px 50px;
    background: rgba(0, 0, 0, 0.5);
    margin-top: 15%;
}
.login-first-div h1{
    color: white;
}

.userinput input{
    width: 200px;/*设置好合适的宽度*/
    border: 0;/*取消边界*/
    border-bottom: 5px solid white;/*修改下边界*/
    font-size: 18px;
    background:#ffffff00;/*透明*/
    color: white;
    padding: 5px 10px;
}

/*修改placeholder的颜色*/
::-webkit-input-placeholder {
    color:    white;
}

/*美化button*/
.login-button input{
    border: 0;/*取消边界 */
    width: 120px;
    height: 30px;
    margin-top: 18px;
    font-size: 18px;
    color: white;
    border-radius: 25px;
    background-image: linear-gradient(to right, #3898d9 15%, #135582 100%);
}

/*调整矢量图大小*/
i{
    color: white;

}

成品前端页面展示

 

  • 持久层代码编写

 思考问题,登录功能的实现就是从前端jsp处获取用户名和密码,再由持久层进行与数据库的访问,既然如此,则需要两个参数,既是用户名和密码

UserDao接口编写方法

  //查询有无用户
    public User getUser(String username, String password) throws SQLException;

UserDaoImpl实现方法

创建spring容器,管理userMapper对象

  public User getUser(String username, String password) throws SQLException {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        UserMapper mapper=(UserMapper)context.getBean("userMapper");
        User user=null;
        user=mapper.getUserById(username,password);
        return user;
    }

UserMapper接口

User getUserById(@Param("name") String name, @Param("pwd") String pwd);

UserMapper.xml映射文件

在此处编写sql语句

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.cat.dao.UserMapper">
    
    <select id="getUserById" parameterType="String" resultType="com.cat.pojo.User">
       select * from mybatis.user where name=#{name} and pwd=#{pwd}
    </select>

</mapper>

UserMapperImpl接口实现类

UserDaoImpl中通过spring容器将sqlSessionFatory注入到此类

package com.cat.dao;
import com.cat.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class UserMapperImpl implements UserMapper{
    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession){
        this.sqlSession=sqlSession;
    }

    @Override
    public User getUserById(String name, String pwd) {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user=null;
        user=mapper.getUserById(name,pwd);
        return user;
    }

}
  • Service层

 通过思考可知,要判断是否登录成功,则看持久层访问数据库后,返回的User对象是否为空,因此Service层的登录逻辑代码也要返回一个从持久层返回的User对象,再由上一层进行判断

UserService接口

public interface UserService {
    public User login(String username, String password);//将账号和密码作为参数传递给下一次

}

UserServiceImpl接口实现类

package com.cat.service;

import com.cat.dao.UserDao;
import com.cat.dao.UserDaoImpl;
import com.cat.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {

    private UserDao userDao;


    public void setUserDao(UserDaoImpl userDao){
        this.userDao=userDao;
    }
    @Override
    public User login(String username, String password) {
        User user=null;
        try {
            user=userDao.getUser(username,password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user;
    }

}
  • Servlet

 UserLogin

package com.cat.servlet;

import com.cat.pojo.User;
import com.cat.service.UserServiceImpl;
import com.cat.util.Constants;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class UserLogin extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取账号和密码
        String userCode=req.getParameter("username");
        String password=req.getParameter("password");
        
        User user=null;//声明一个空的user


        PrintWriter out=resp.getWriter();


        //Spring部分,创建Spring容器,管理UserService对象
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        UserServiceImpl userService = (UserServiceImpl)context.getBean("userService");

        //调用业务逻辑层的登录方法,获得返回的user对象
        user=userService.login(userCode,password);

        //测试登录是否接受到user
        System.out.println(user);
        if(user!=null){
            //跳转页面
            //将用户信息存储到Session中
            req.getSession().setAttribute(Constants.USER_SESSION,user);
            resp.sendRedirect("jsp/frame.jsp");
        }else{
            //将错误信息输出到前端页面
            req.setAttribute("error","密码错误");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

 在此附上Session的类

package com.cat.util;

public class Constants {
    public final static String USER_SESSION="userSession";
}

再web.xml中配置UserLogin 

<?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"
         metadata-complete="true">

  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.cat.servlet.UserLogin</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/UserLogin.java</url-pattern>
  </servlet-mapping>


</web-app>

  • 过滤器

为了防止网页出现中文乱码,也为了不用每一个Servlet类都编写一遍过滤,于是编写一个过滤器 

package com.cat.filter;

import javax.servlet.*;
import java.io.IOException;

public class CharacterEncodingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html;charset=UTF-8");
        filterChain.doFilter(servletRequest,servletResponse);
    }
    @Override
    public void destroy() {

    }
}

在web.xml中配置过滤器

<?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"
         metadata-complete="true">

  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.cat.servlet.UserLogin</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/UserLogin.java</url-pattern>
  </servlet-mapping>

  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>com.cat.filter.CharacterEncodingFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

到此登录功能已完成

3.2 添加功能的思路

从前端获取用户输入的信息,层层传递后添加到数据库即可,若添加成功,在持久层的方法就会返回一个大于0的整数,按照这个原理可设置层层验证方法。

除此之外此项目还要其他功能就不一一详谈,在此附上效果图

 

 

 由于本人能力有限,对于架构与相关代码方法还要底层原理的理解不够,可能会说错一些,望指出错误。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值