米米商城项目

米米商城

  • 1.开发环境
  • 2.项目功能
  • 3.项目搭建步骤
  • 4.配置文件
    • 4.1 pom.xml
    • 4.2 jdbc.properties
    • 4.3 Mybaties配置文件:SqlMapConfig.xml
    • 4.4 Spring配置文件
      • 4.4.1 applicationContext_dao.xml
      • 4.4.2 applicationContext_service.xml
    • 4.5 SpringMVC配置文件:springmvc.xml
    • 4.6 web.xml
  • 5.逆向工程
  • 6.登录功能
    • 6.1 MD5加密
    • 6.2 业务层代码
    • 6.3 控制层代码

1.开发环境

  • 1.服务端
    Spring+SpringMVC+Mybatis
  • 2.数据库
    MySQL8.0
  • 3.项目管理
    Maven
  • 4.前端
    jQuery+BootStrap+JavaScript
  • 5.开发工具
    Idea

2.项目功能

米米商城项目_第1张图片
米米商城项目_第2张图片

3.项目搭建步骤

1)新建Maven工程
2)修改目录,修改pom.xml文件
3)添加SSM框架的所有依赖
4)拷贝jdbc.porperties到resources目录下
5)新建applicationContext_dao.xml文件,进行数据访问层的配置
6)新建applicationContext_service.xml文件,进行业务逻辑层的配置
7)新建springmvc.xml文件,配置springmvc的框架
8)新建SqlMapConfig.xml文件,进行分页插件的配置
9)对web.xml文件进行配置
10)使用逆向工程生成pojo和mapper的文件
11)开发业务逻辑层,实现登录判断
12)开发控制器AdminAction,完成登录处理
13)改造页面,发送登录请求,验证登录

4.配置文件

在这里插入图片描述
米米商城项目_第3张图片
在这里插入图片描述

4.1 pom.xml



<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.0modelVersion>

  <groupId>com.bjpowernodegroupId>
  <artifactId>mimissmartifactId>
  <version>1.0version>
  <packaging>warpackaging>

  
  <properties>
    <junit.version>4.12junit.version>
    <spring.version>5.2.5.RELEASEspring.version>
    <mybatis.version>3.5.1mybatis.version>
    <mybatis.spring.version>1.3.1mybatis.spring.version>
    <mybatis.paginator.version>1.2.15mybatis.paginator.version>
    <mysql.version>8.0.22mysql.version>
    <slf4j.version>1.6.4slf4j.version>
    <druid.version>1.1.12druid.version>
    <pagehelper.version>5.1.1pagehelper.version>
    <jstl.version>1.2jstl.version>
    <servlet-api.version>2.5servlet-api.version>
    <jsp-api.version>2.0jsp-api.version>
    <jackson.version>2.9.6jackson.version>
  properties>
  <dependencies>

    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-beansartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aspectsartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jmsartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <version>${spring.version}version>
    dependency>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>${mybatis.version}version>
    dependency>
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>${mybatis.spring.version}version>
    dependency>
    <dependency>
      <groupId>com.github.miemiedevgroupId>
      <artifactId>mybatis-paginatorartifactId>
      <version>${mybatis.paginator.version}version>
    dependency>
    <dependency>
      <groupId>com.github.jsqlparsergroupId>
      <artifactId>jsqlparserartifactId>
      <version>3.1version>
    dependency>
    <dependency>
      <groupId>com.github.pagehelpergroupId>
      <artifactId>pagehelperartifactId>
      <version>${pagehelper.version}version>
    dependency>
    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>${mysql.version}version>
    dependency>
    
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>druidartifactId>
      <version>${druid.version}version>
    dependency>

    
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>${junit.version}version>
      <scope>testscope>
    dependency>


    
    <dependency>
      <groupId>jstlgroupId>
      <artifactId>jstlartifactId>
      <version>${jstl.version}version>
    dependency>
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.0.1version>
      <scope>providedscope>
    dependency>
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>jsp-apiartifactId>
      <scope>providedscope>
      <version>${jsp-api.version}version>
    dependency>
    
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
      <version>${jackson.version}version>
    dependency>
    <dependency>
      <groupId>org.jsongroupId>
      <artifactId>jsonartifactId>
      <version>20140107version>
    dependency>
    
    <dependency>
      <groupId>commons-iogroupId>
      <artifactId>commons-ioartifactId>
      <version>2.4version>
    dependency>
    <dependency>
      <groupId>commons-fileuploadgroupId>
      <artifactId>commons-fileuploadartifactId>
      <version>1.3.1version>
    dependency>
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>servlet-apiartifactId>
      <version>2.5version>
    dependency>
    <dependency>
      <groupId>com.github.pagehelpergroupId>
      <artifactId>pagehelperartifactId>
      <version>5.1.10version>
    dependency>
  dependencies>

  
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-compiler-pluginartifactId>
        <configuration>
          <source>1.8source>
          <target>1.8target>
          <encoding>UTF-8encoding>
        configuration>
      plugin>
    plugins>
    
    <resources>
      <resource>
        <directory>src/main/javadirectory>
        <includes>
          <include>**/*.propertiesinclude>
          <include>**/*.xmlinclude>
        includes>
        <filtering>falsefiltering>
      resource>
      <resource>
        <directory>src/main/resourcesdirectory>
        <includes>
          <include>**/*.propertiesinclude>
          <include>**/*.xmlinclude>
        includes>
        <filtering>falsefiltering>
      resource>
    resources>
  build>



project>

4.2 jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/xiaomissm?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
jdbc.username=数据库用户名
jdbc.password=数据库密码

4.3 Mybaties配置文件:SqlMapConfig.xml

Spring中整合了Mybaties中的大部分功能,因此mybaties配置文件中只配置了分页插件


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    settings>
    
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">plugin>
    plugins>
configuration>

4.4 Spring配置文件

整合了Mybaties,将Spring配置文件进了分层配置

4.4.1 applicationContext_dao.xml


<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">

    <context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}">property>
        <property name="url" value="${jdbc.url}">property>
        <property name="username" value="${jdbc.username}">property>
        <property name="password" value="${jdbc.password}">property>
    bean>

    <bean class="org.mybatis.spring.SqlSessionFactoryBean">

            <property name="dataSource" ref="dataSource">property>

            <property name="configLocation" value="classpath:SqlMapConfig.xml">property>

            <property name="typeAliasesPackage" value="com.bjpowernode.pojo">property>
    bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.bjpowernode.mapper">property>
    bean>
beans>

4.4.2 applicationContext_service.xml


<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:tx="http://www.springframework.org/schema/tx"
       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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">


        <context:component-scan base-package="com.bjpowernode.service">context:component-scan>

        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource">property>
        bean>

        <tx:advice id="myadvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="*select*" read-only="true"/>
                <tx:method name="*find*" read-only="true"/>
                <tx:method name="*get*" read-only="true"/>
                <tx:method name="*search*" read-only="true"/>
                <tx:method name="*insert*" propagation="REQUIRED"/>
                <tx:method name="*save*" propagation="REQUIRED"/>
                <tx:method name="*add*" propagation="REQUIRED"/>
                <tx:method name="*delete*" propagation="REQUIRED"/>
                <tx:method name="*remove*" propagation="REQUIRED"/>
                <tx:method name="*clear*" propagation="REQUIRED"/>
                <tx:method name="*update*" propagation="REQUIRED"/>
                <tx:method name="*modify*" propagation="REQUIRED"/>
                <tx:method name="*change*" propagation="REQUIRED"/>
                <tx:method name="*set*" propagation="REQUIRED"/>
                <tx:method name="*" propagation="SUPPORTS"/>
            tx:attributes>
        tx:advice>

        <aop:config>
            <aop:pointcut id="mypointcut" expression="execution(* com.bjpowernode.service.*.*(..))"/>
            <aop:advisor advice-ref="myadvice" pointcut-ref="mypointcut">aop:advisor>
        aop:config>
beans>

4.5 SpringMVC配置文件:springmvc.xml


<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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">


        <context:component-scan base-package="com.bjpowernode.controller">context:component-scan>

        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/admin/">property>
            <property name="suffix" value=".jsp">property>
        bean>

        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

        bean>

        <mvc:annotation-driven>mvc:annotation-driven>

beans>

4.6 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_4_0.xsd"
         version="4.0">


        <filter>
            <filter-name>encodefilter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
            <init-param>
                <param-name>encodingparam-name>
                <param-value>UTF-8param-value>
            init-param>
            <init-param>
                <param-name>forceRequestEncodingparam-name>
                <param-value>trueparam-value>
            init-param>
            <init-param>
                <param-name>forceResponseEncodingparam-name>
                <param-value>trueparam-value>
            init-param>
        filter>
    <filter-mapping>
        <filter-name>encodefilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>*.actionurl-pattern>
    servlet-mapping>


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext_*.xmlparam-value>
    context-param>


web-app>

5.逆向工程

用来快速生成pojo和mapper文件
米米商城项目_第4张图片
1.配置文件


DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
	<context id="testTables" targetRuntime="MyBatis3">
		<commentGenerator>
			
			<property name="suppressAllComments" value="true" />
		commentGenerator>
		
		<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mimissm?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true" userId="root" password="root">
			
			<property name="nullCatalogMeansCurrent" value="true"/>
		jdbcConnection>
		
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		javaTypeResolver>

		
		<javaModelGenerator targetPackage="com.bjpowernode.pojo"
			targetProject=".\src">
			
			<property name="enableSubPackages" value="false" />
			
			<property name="trimStrings" value="true" />
		javaModelGenerator>
        
		<sqlMapGenerator targetPackage="com.bjpowernode.mapper"
			targetProject=".\src">
			
			<property name="enableSubPackages" value="false" />
		sqlMapGenerator>
		
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.bjpowernode.mapper"
			targetProject=".\src">
			
			<property name="enableSubPackages" value="false" />
		javaClientGenerator>
		
		<table schema="" tableName="admin">table>
		<table schema="" tableName="product_info">table>
		<table schema="" tableName="product_type">table>


	context>
generatorConfiguration>

2.运行代码

package com.oracle.mybatis.tools;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

public class GeneratorSqlmap {

	public void generator() throws Exception{

		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		//指定 逆向工程配置文件
		File configFile = new File("generatorConfig.xml"); 
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration config = cp.parseConfiguration(configFile);
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
				callback, warnings);
		myBatisGenerator.generate(null);

	} 
	public static void main(String[] args) throws Exception {
		try {
			GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
			generatorSqlmap.generator();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}

6.登录功能

6.1 MD5加密

package com.bjpowernode.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {
    /**
     * 1.MD5(message-digest algorithm 5)信息摘要算法,
     *   它的长度一般是32位的16进制数字符串(如81dc9bdb52d04dc20036dbd8313ed055)
     * 2.由于系统密码明文存储容易被黑客盗取
     * 3.应用:注册时,将密码进行md5加密,存到数据库中,防止可以看到数据库数据的人恶意篡改。
     *       登录时,将密码进行md5加密,与存储在数据库中加密过的密码进行比对
     * 4.md5不可逆,即没有对应的算法,从产生的md5值逆向得到原始数据。
     *   但是可以使用暴力破解,这里的破解并非把摘要还原成原始数据,如暴力枚举法。
     *
     */
    public final static String getMD5(String str){
        try {
            MessageDigest md = MessageDigest.getInstance("SHA");//创建具有指定算法名称的摘要
            md.update(str.getBytes());                    //使用指定的字节数组更新摘要
            byte mdBytes[] = md.digest();                 //进行哈希计算并返回一个字节数组

            String hash = "";
            for(int i= 0;i<mdBytes.length;i++){           //循环字节数组
                int temp;
                if(mdBytes[i]<0)                          //如果有小于0的字节,则转换为正数
                    temp =256+mdBytes[i];
                else
                    temp=mdBytes[i];
                if(temp<16)
                    hash+= "0";
                hash+=Integer.toString(temp,16);         //将字节转换为16进制后,转换为字符串
            }
            return hash;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}

6.2 业务层代码

package com.bjpowernode.service.impl;

import com.bjpowernode.mapper.AdminMapper;
import com.bjpowernode.pojo.Admin;
import com.bjpowernode.pojo.AdminExample;
import com.bjpowernode.service.AdminService;
import com.bjpowernode.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zwghdu
 * @create 2021-08-29-16:55
 */
@Service
public class AdminServiceImpl implements AdminService {

    //在业务逻辑层中,一定给会有数据访问层的对象
    @Autowired
    AdminMapper adminMapper;

    @Override
    public Admin login(String name, String pwd) {
        //根据传入的用户名到db中查询相应的用户对象
        //如果有条件,则一定要创建AdminExample的对象,用来封装条件
        AdminExample example = new AdminExample();
        /**
         * 如何添加条件
         * select * from admin where a_name='admin'
         */
        //添加用户名a_name条件
        example.createCriteria().andANameEqualTo(name);

        List<Admin> list = adminMapper.selectByExample(example);
        //判断是否查到
        if(list.size()>0){
            Admin admin = list.get(0);
            //如果查询到用户对象,再进行密码的比对,注意密码是密文
            /**
             * 分析:
             * admin.getApass==>c984aed014aec7623a54f0591da07a85fd4b762d
             * pwd==>000000
             * 在进行密码对比时,要将传入的pwd进行MD5加密,再与数据库中查到的对象的密码进行对比
             */
            String miPwd = MD5Util.getMD5(pwd);
            if(miPwd.equals(admin.getaPass())){
                return admin;
            }
        }
        return null;
    }
}

6.3 控制层代码

package com.bjpowernode.controller;

import com.bjpowernode.pojo.Admin;
import com.bjpowernode.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zwghdu
 * @create 2021-08-29-17:17
 */
@Controller
@RequestMapping("/admin")
public class AdminAction {

    //切记:在所有的界面层,一定会有业务逻辑层的对象
    @Autowired
    AdminService adminService;

    //实现登录判断,并进行相应的跳转
    @RequestMapping("/login")
    public String login(String name, String pwd, HttpServletRequest request){
        Admin admin = adminService.login(name,pwd);
        if (admin != null){
            //登录成功
            request.setAttribute("admin",admin);
            return "main";
        }else {
            //登录失败
            request.setAttribute("errmsg","用户名或密码不正确!");
            return "login";
        }
    }

}

你可能感兴趣的:(项目,intellij-idea,maven,java)