• 模拟A卷


    网站数据分析

    一: 语言与环境

    • java hadoop
    • 环境要求: eclipse / 虚拟机 / linux / hadoop(hive,hdfs,mysql) / myeclipse(可选)

    二: 功能要求

    • 利用MapReduce对日志文件进行离线分析,将分析后的数据通过ssm可视化展示

    三: 日志内容 (customer.txt)

    13726230503,00-FD-07-A4-72-B8:CMCC,192.168.33.3,http://www.imooc.cn/stu,2017-08-04 15:30:20
    13826544101,5C-0E-8B-C7-F1-E0:CMCC,192.168.33.3,http://www.edu360.cn/teach,2017-08-04 15:35:20
    13926435656,20-10-7A-28-CC-0A:CMCC,192.168.33.4,http://www.csdn.cn/stu,2017-08-04 15:30:20
    13926251106,5C-0E-8B-8B-B1-50:CMCC,192.168.33.4,http://www.edu360.cn/job,2017-08-04 16:30:20
    18211575961,94-71-AC-CD-E6-18:CMCC-EASY,192.168.33.5,http://www.imooc.cn/job,2017-08-04 15:40:20
    13560439658,C4-17-FE-BA-DE-D9:CMCC,192.168.33.3,http://www.edu360.cn/stu,2017-08-05 15:30:20
    15920133257,5C-0E-8B-C7-BA-20:CMCC,192.168.44.3,http://www.edu360.cn/teach,2017-08-05 15:35:20
    13719199419,68-A1-B7-03-07-B1:CMCC-EASY,192.168.33.44,http://www.edu360.cn/stu,2017-08-05 15:30:20
    15013685858,5C-0E-8B-C7-F7-90:CMCC,192.168.33.46,http://www.edu360.cn/job,2017-08-05 16:30:20
    15989002119,E8-99-C4-4E-93-E0:CMCC-EASY,192.168.33.55,http://www.edu360.cn/job,2017-08-05 15:40:20
    13560439658,C4-17-FE-BA-DE-D9:CMCC,192.168.133.3,http://www.csdn.cn/register,2017-08-06 15:30:20
    13480253104,5C-0E-8B-C7-FC-80:CMCC-EASY,192.168.111.3,http://www.edu360.cn/register,2017-08-06 15:35:20
    13602846565,5C-0E-8B-8B-B6-00:CMCC,192.168.34.44,http://www.imooc.cn/pay,2017-08-06 15:30:20
    13922314466,00-FD-07-A2-EC-BA:CMCC,192.168.33.46,http://www.imooc0.cn/excersize,2017-08-06 16:30:20
    13502468823,5C-0A-5B-6A-0B-D4:CMCC-EASY,192.168.33.55,http://www.csdn.cn/job,2017-08-06 15:40:20
    13726230503,84-25-DB-4F-10-1A:CMCC-EASY,192.168.33.46,http://www.edu360.cn/excersize,2017-08-06 16:30:20
    13925057413,00-1F-64-E1-E6-9A:CMCC,192.168.33.25,http://www.edu360.cn/job,2017-08-06 15:40:20
    13760778710,00-FD-07-A4-7B-08:CMCC,192.168.33.36,http://www.csdn.cn/excersize,2017-08-06 16:30:20
    13726230503,C4-17-FE-BA-DE-D9:CMCC,192.168.33.55,http://www.imooc.cn/job,2017-08-06 15:40:20

    解题思路:

    • 第一步: 开启虚拟机,运行hadoop集群,将要解析的日志文件(customer.txt)加载到hdfs上
    • 第二步: 打开 eclipse 导入 lib支持包 , 编写MapReduce 对日志文件解析
    • 第三步: 拿到解析后的数据存入hive数据仓库,根据题目要求对hive进行查询存入新hive表
    • 第四步: 将hive表数据通过 sqoop 工具 导出数据给 mysql(这里的mysql一定是linux中的mysql)
    • 第五步: 通过SSM对linux中的mysql数据进行数据可视化

    细节说明:

    • 我们拿到数据进行展示要通过用户登录的方式进行数据的全查
    • 那么 我们就要提供对应的user进行用户登录审核
    • 我们除了需要在linux中的mysql拿到hive导入过来的数据表之外,还需要新建一张user仅供用户的登陆

    思路明确,开始编码

    一: 对 日志文件 通过 MapReduce 技术清洗
    • 运行linux,打开 hadoop 集群

      • [root@master ~]# bash ~/app/hadoop-2.6.0-cdh5.14.0/sbin/start-all.sh	
        [root@master ~]# jps 
        1360 NameNode
        2274 Jps
        1448 DataNode
        1896 NodeManager
        1804 ResourceManager
        1630 SecondaryNameNode
        
    • 将 日志文件 上传到 hdsf系统

      • 通过 filezilla 或者 xftp 将 windows日志文件传入到linux中,再运行命令将linux中的日志文件上传到hdfs

        [root@master MP]# hadoop fs -mkdir /mpfile   -- 新建一个hdfs文件夹用于存放 日志文件
        [root@master MP]# hadoop fs -put customer.txt /mpfile  -- 将 日志文件传入到 hdfs 平台
        
    • 编写 MapReduce 代码

      自行导入 lib 支持jar包
      
      mapper类
      package com.hp.a;
      
      import java.io.IOException;
      
      import org.apache.hadoop.io.LongWritable;
      import org.apache.hadoop.io.NullWritable;
      import org.apache.hadoop.io.Text;
      import org.apache.hadoop.mapreduce.Mapper;
      
      public class CustomerMapper extends Mapper<LongWritable, Text, Text, NullWritable>{
      	@Override
      	protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, NullWritable>.Context context)
      			throws IOException, InterruptedException {
              //拿到 一行 数据, 通过逗号分隔截取出每个元素存放到 字符串类型的 linus 数组中
      		String lines[] = value.toString().split(",");
              //对 lines 数组筛选出题目要求对应的下标元素 通过 上下文对象context传入到下一个阶段
      		context.write(new Text(lines[0]+"	"+lines[3]+"	"+lines[4]), NullWritable.get());
      	}
      }
      
      不需要写reduce代码
         
      进行提交
      package com.hp.a;
      
      import org.apache.hadoop.conf.Configuration;
      import org.apache.hadoop.fs.FileSystem;
      import org.apache.hadoop.fs.Path;
      import org.apache.hadoop.io.NullWritable;
      import org.apache.hadoop.io.Text;
      import org.apache.hadoop.mapreduce.Job;
      import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
      import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
      
      public class CustomerSubmitter {
      	public static void main(String[] args) throws Exception {
      		Configuration conf = new Configuration();
      		FileSystem fs = FileSystem.get(conf);
      		if(fs.exists(new Path(args[1]))) {
      			fs.delete(new Path(args[1]),true);
      		}
      		Job job = Job.getInstance();
      		job.setJarByClass(CustomerSubmitter.class);
      		job.setMapperClass(CustomerMapper.class);
      		job.setMapOutputKeyClass(Text.class);
      		job.setMapOutputValueClass(NullWritable.class);
      		FileInputFormat.setInputPaths(job, new Path(args[0]));
      		FileOutputFormat.setOutputPath(job, new Path(args[1]));
      		
      		job.waitForCompletion(true);
      	}
      }
      
      
      
    • 通过 eclipse 导出 MapReduce 代码jar包,上传到linux平台 进行执行

      [root@master MP]# hadoop jar 0521kaoshi.jar /mpfile/customer.txt /0604
      这个命令一共有几个参数:
      hadoop: 代表hadoop平台
      jar: 我们要执行jar文件
      0521kaoshi.jar: eclipse导出的执行jar
      /mpfile/customer.txt: hdfs上面的日志文件
      /0604: 输出清洗后数据存放的目录 -- 到时候我们要通过hive解析的数据就是在这里
      
    二: 打开 hive 创建 库 表 存放 MapReduce清洗的数据
    • 配置 hive 结构

      创建hive库
      create database kaoshi;
      使用hive库
      use kaoshi;
      创建hive表
      CREATE TABLE `cum_backup`(
        `phonenum` string, 
        `url` string, 
        `time` string)
      ROW FORMAT DELIMITED 
      FIELDS TERMINATED BY '	';
      
    • 加载hdfs数据给hive

      load data inpath '/0604/part-r-00000' into table cum_backup;
      
    • 使用 sqoop 将该表内容导入到 mysql 数据库

      先创建 mysql 数据库 和 表 进行存放
      创建mysql库
      create database kaoshi;
      使用mysql表
      use kaoshi;
      创建mysql表
      create table cum_backup(
      	phonenum varchar(32),
      	url varchar(32),
      	time varchar(32)
      );
      
      bin/sqoop export --connect jdbc:mysql://localhost:3306/kaoshi --username root --password mysql --table cum_backup --export-dir /user/hive/warehouse/kaoshi.db/cum_backup --input-fields-terminated-by "	";
      
      注意点: 这里所要注意的是 mysql和hive库名 账号密码 都要使用自己环境上面的
      
    • 在 linux 中 进入mysql 进行全查,确保数据都进来了

      mysql> show databases;
      +--------------------+
      | Database           |
      +--------------------+
      | information_schema |
      | kaoshi             |
      | metastore          |
      | mysql              |
      | performance_schema |
      | test               |
      +--------------------+
      mysql> use kaoshi;
      Database changed
      mysql> show tables;
      +------------------+
      | Tables_in_kaoshi |
      +------------------+
      | access_new       |
      | cum_backup       |
      | goods_new        |
      | user             |
      +------------------+
      4 rows in set (0.00 sec)
      mysql> select phonenum,url,time from cum_backup;
      +-------------+--------------------------------+---------------------+
      | phonenum    | url                            | time                |
      +-------------+--------------------------------+---------------------+
      | 13826544101 | http://www.edu360.cn/teach     | 2017-08-04 15:35:20 |
      | 15013685858 | http://www.edu360.cn/job       | 2017-08-05 16:30:20 |
      | 13922314466 | http://www.imooc0.cn/excersize | 2017-08-06 16:30:20 |
      | 15920133257 | http://www.edu360.cn/teach     | 2017-08-05 15:35:20 |
      | 15989002119 | http://www.edu360.cn/job       | 2017-08-05 15:40:20 |
      | 18211575961 | http://www.imooc.cn/job        | 2017-08-04 15:40:20 |
      | 13480253104 | http://www.edu360.cn/register  | 2017-08-06 15:35:20 |
      | 13502468823 | http://www.csdn.cn/job         | 2017-08-06 15:40:20 |
      | 13560439658 | http://www.csdn.cn/register    | 2017-08-06 15:30:20 |
      | 13560439658 | http://www.edu360.cn/stu       | 2017-08-05 15:30:20 |
      | 13602846565 | http://www.imooc.cn/pay        | 2017-08-06 15:30:20 |
      | 13719199419 | http://www.edu360.cn/stu       | 2017-08-05 15:30:20 |
      | 13726230503 | http://www.imooc.cn/job        | 2017-08-06 15:40:20 |
      | 13726230503 | http://www.imooc.cn/stu        | 2017-08-04 15:30:20 |
      | 13726230503 | http://www.edu360.cn/excersize | 2017-08-06 16:30:20 |
      | 13760778710 | http://www.csdn.cn/excersize   | 2017-08-06 16:30:20 |
      | 13925057413 | http://www.edu360.cn/job       | 2017-08-06 15:40:20 |
      | 13926251106 | http://www.edu360.cn/job       | 2017-08-04 16:30:20 |
      | 13926435656 | http://www.csdn.cn/stu         | 2017-08-04 15:30:20 |
      +-------------+--------------------------------+---------------------+
      
      创建 user 表
      create table user(username varchar(32),password varchar(32));
      插入 数据 用于登陆
      insert into user values("hello","ssm");
      
    • 完成了数据的清洗和存储,接下来就是ssm进行数据展示了

    三: 编写 SSM
    • 导入 依赖 jar 存放到 WEB-INF 下面的lib 添加到环境(Build Path)

    • 编写四个配置文件(1. applicationContext.xml 2. mvc-servlet.xml 3. web.xml 4. jdbc.properties)

        1. applicationContext.xml --(放在 WEB-INF 目录下)
        <?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:tx="http://www.springframework.org/schema/tx"
               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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
        
            <!-- 告知spring在哪一个包下面使用了注解 -->
        	<context:component-scan base-package="com.hp"></context:component-scan>
        	
        	
        	<!-- 读取小配置文件 jdbc.properties -->
        	<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
        
        	<!-- 配置连接数据库的相关参数 -->
        	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        		<property name="driverClass" value="${jdbc.driver}"></property>
        		<property name="jdbcUrl" value="${jdbc.url}"></property>
        		<property name="user" value="${jdbc.username}"></property>
        		<property name="password" value="${jdbc.password}"></property>
        	</bean>
        
        	<!-- 构建SqlSessionFactory -->
        	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        		<!-- sqlsession离不开数据源 注入数据源 -->
        		<property name="dataSource" ref="dataSource"></property>
        		<!-- 给实体类起别名 -->
        		<property name="typeAliasesPackage">
        			<value>com.hp.entity</value>
        		</property>
        		<!-- 注册mapper -->
        		<property name="mapperLocations">
        			<list>
        				<value>classpath:com/hp/dao/*Mapper.xml</value>
        			</list>
        	    </property>
        	</bean>
        	<!-- 创建dao实现类对象 -->
        	<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        		<!-- 告知spring给哪一个包下的接口创建实现类对象 -->
        		<property name="basePackage">
        			<value>com.hp.dao</value>
        		</property>
        	</bean>
        	
        	<!-- 引入控制事务 -->
        	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        		<!-- 控制事务需要数据源 -->
        		<property name="dataSource" ref="dataSource"></property>
        	</bean>
        	
        	<!-- 激活事务的注解 -->
        	<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
        </beans>
        <!-- <?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:tx="http://www.springframework.org/schema/tx"
        	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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
        
        	告知spring在哪一个包下面使用了注解
        	<context:component-scan base-package="com.hp"></context:component-scan>
        
        	读取小配置文件 jdbc.properties
        	<context:property-placeholder location="classpath:jdbc.properties" />
        
        	配置连接数据库的相关参数
        	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        		<property name="driverClass" value="${jdbc.driver}"></property>
        		<property name="jdbcUrl" value="${jdbc.url}"></property>
        		<property name="user" value="${jdbc.username}"></property>
        		<property name="password" value="${jdbc.password}"></property>
        	</bean>
        
        	构建sqlSessionFactory
        	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        		sqlsession离不开数据源, 注入数据源
        		<property name="dataSource" ref="dataSource"></property>
        		给实体类起别名
        		<property name="typeAliasesPackage">
        			<value>com.hp.entity</value>
        		</property>
        		注册mapper
        		<property name="mapperLocations">
        			<list>
        				<value>classpath:com/hp/dao/*Mapper.xml</value>
        			</list>
        		</property>
        	</bean>
        	
        	引入控制事务
        	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        		控制事务需要数据源
        		<property name="dataSource" ref="dataSource"></property>
        	</bean>
        	
        	激活事务的注解
        	<tx:annotation-driven transaction-manager="transactionManager"/>
        
        </beans> -->
        
      • 2.mvc-servlet.xml --(放在 WEB-INF 目录下)

        <?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:mvc="http://www.springframework.org/schema/mvc"
               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/mvc
               http://www.springframework.org/schema/mvc/spring-mvc.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd
                ">
        	
        	<!-- 指定SpringMVC控制器所在的包 -->
        	<context:component-scan base-package="com.hp"></context:component-scan>
        	
        	<!-- 开启springmvc的注解功能 -->
        	<mvc:annotation-driven></mvc:annotation-driven>
        	
        	<!-- 配置springmvc针对页面的视图解析器 -->
        	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        		<property name="prefix" value="/"></property>
        		<property name="suffix" value=".jsp"></property>
        	</bean>
        	
        	<!-- 配置一个对静态资源处理的处理器:防止静态资源无法访问 -->
        	<mvc:default-servlet-handler/>
        </beans>
        
      • 3.web.xml --(放在 WEB-INF 目录下)

        <?xml version="1.0" encoding="UTF-8"?>
        <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
          <display-name>kaoshiB</display-name>
          <!-- 编码格式 -->
          <filter>
          	<filter-name>encode</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>encode</filter-name>
          	<url-pattern>/*</url-pattern>
          </filter-mapping>
          
          <!-- 启动web容器时,自动装配ApplicationContext.xml的配置信息 -->
          <listener>
          	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
          
          <!-- 编写springmvc的核心入口 Servlet -->
          <servlet>
          	<servlet-name>mvc</servlet-name>
          	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          	<!-- 初始化 -->
          	<load-on-startup>0</load-on-startup>
          </servlet>
          
          <!-- 客户端所有请求都经过入口Servlet处理 -->
          	<servlet-mapping>
          		<servlet-name>mvc</servlet-name>
          		<url-pattern>/</url-pattern>
          	</servlet-mapping>
        </web-app>
        
      • 4.jdbc.properties(放在 src目录下)

        jdbc.driver=com.mysql.jdbc.Driver
        jdbc.url=jdbc:mysql://192.168.65.110:3306/kaoshi
        jdbc.username=root
        jdbc.password=mysql
        
        注意点: ip写你的linux的ip,通过ifconfig可以查看
        /kaoshi: 这个是 linux中mysql数据库名字
        
    三: 编写 后台核心代码
    • 创建 com.hp.entity --(用于映射数据库中表字段)

    • 创建 com.hp.dao --(用于数据访问)

    • 创建 com.hp.service --(事务控制 服务层)

    • 创建 com.hp.controller --(控制层 控制页面基本跳转)

      • 后台核心代码如下

      • 编写 com.hp.entity层

        Backup 实体类  属性和
        package com.hp.entity;
        /*
        这个类 字段尽量要和 数据表字段名称相同
        */
        public class Backup {
        	private String phonenum;
        	private String url;
        	private String time;
        	public String getPhonenum() {
        		return phonenum;
        	}
        	public void setPhonenum(String phonenum) {
        		this.phonenum = phonenum;
        	}
        	public String getUrl() {
        		return url;
        	}
        	public void setUrl(String url) {
        		this.url = url;
        	}
        	public String getTime() {
        		return time;
        	}
        	public void setTime(String time) {
        		this.time = time;
        	}
        	@Override
        	public String toString() {
        		return "Backup [phonenum=" + phonenum + ", url=" + url + ", time="
        				+ time + "]";
        	}
        	public Backup(String phonenum, String url, String time) {
        		super();
        		this.phonenum = phonenum;
        		this.url = url;
        		this.time = time;
        	}
        	public Backup() {
        		super();
        	}
        }
        
        创建user 表
        package com.hp.entity;
        
        public class User {
        	private int id;
        	private String username;
        	private String password;
        	public int getId() {
        		return id;
        	}
        	public void setId(int id) {
        		this.id = id;
        	}
        	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;
        	}
        	public User(int id, String username, String password) {
        		super();
        		this.id = id;
        		this.username = username;
        		this.password = password;
        	}
        	public User() {
        		super();
        		// TODO Auto-generated constructor stub
        	}
        	@Override
        	public String toString() {
        		return "User [id=" + id + ", username=" + username + ", password="
        				+ password + "]";
        	}
        }
        
      • 编写 com.hp.dao 层

        BackupDao 类
        
        package com.hp.dao;
        
        import java.util.ArrayList;
        
        import com.hp.entity.Backup;
        
        public interface BackupDao {
        	//全查
        	ArrayList<Backup> findAll();
        }
        
        BackupDaoMapper.xml文件
        <?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" >
        <mapper namespace="com.hp.dao.BackupDao">
        	<select id="findAll" resultType="Backup">
        		select * from cum_backup
        	</select>
        	
        	<insert id="insertBackup">
        		insert into cum_backup values(phonenum,sex,url,time,age);
        	</insert>
        </mapper>
        
        UserDao类
        package com.hp.dao;
        
        import org.apache.ibatis.annotations.Param;
        import org.springframework.stereotype.Repository;
        
        import com.hp.entity.User;
        
        @Repository
        public interface UserDao {
        	// 登 陆 
        	User login(String username);
        	
        	//插入
        	void insertUser(@Param("id")int id,@Param("username")String username,@Param("password")String password);
        }
        
        UserDaoMapper.xml文件
        <?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" >
        <mapper namespace="com.hp.dao.UserDao">
        	<select id="login" resultType="User">
        		select id,username,password from user where username=#{username}
        	</select>
        	
        	<insert id="insertUser">
        		insert into user(id,username,password) values(#{id},#{username},#{password})
        	</insert>
        </mapper>
        
      • 编写 com.hp.service 层

        package com.hp.service;
        
        import java.util.ArrayList;
        
        import com.hp.entity.Backup;
        
        public interface BackupService {
        	ArrayList<Backup> findAll();
        }
        
        package com.hp.service;
        
        import java.util.ArrayList;
        
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Service;
        
        import com.hp.dao.BackupDao;
        import com.hp.entity.Backup;
        
        @Service
        public class BackupServiceImpl implements BackupService {
        
        	@Autowired
        	private BackupDao backupdao;
        	
        	public ArrayList<Backup> findAll() {
        		return backupdao.findAll();
        	}
        }
        ```
        
        ```
        package com.hp.service;
        
        import com.hp.entity.User;
        
        public interface UserService {
        	//登陆
        	User login(String username);
        	
        	//插入
        	void insertUser(int id,String username,String password);
        }
        ```
        
        ```
        package com.hp.service;
        
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Service;
        import org.springframework.transaction.annotation.Transactional;
        
        import com.hp.dao.UserDao;
        import com.hp.entity.User;
        
        @Service
        @Transactional  //控制事务
        public class UserServiceImpl implements UserService {
        	
        	@Autowired
        	private UserDao userdao;
        
        	@Override
        	public User login(String username) {
        		User login = userdao.login(username);
        		return login;
        	}
        
        	@Override
        	public void insertUser(int id, String username, String password) {
        		userdao.insertUser(id,username,password);
        	}
        }
        ```
        
        
      • 编写 com.hp.Controller 层

        package com.hp.controller;
        
        import javax.servlet.http.HttpSession;
        
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.RequestMapping;
        import com.hp.entity.User;
        import com.hp.service.UserService;
        
        /*用于用户登录注册的跳转*/
        @Controller
        @RequestMapping(value="/login")
        public class UserController {
        	@Autowired
        	private UserService userservice;
        	
        	//登陆
        	@RequestMapping("/user")
        	public String login(String username,String password,HttpSession session){
        		System.out.println("Jsp"+"	"+"用户名:"+username+"	密码"+password);
        		User user = userservice.login(username);
        		if(user != null){
        			if(user.getPassword().equals(password)){
        				session.setAttribute("admin", user);
        				System.out.println("登陆成功");
        				return "forward:/back/findall";
        			}else{
        				System.out.println("密码错误");
        				return "login";
        			}
        		}else{
        			System.out.println("用户不存在");
        			return "login";
        		}
        	}
        	//插入
        	@RequestMapping(value="/insertUser")
        	public String insertUser(String id,String username,String password,HttpSession session){
        		userservice.insertUser(Integer.parseInt(id),username,password);
        		session.setAttribute("msg", username);
        		return "registersuccess";
        	}
        }
        
        
        package com.hp.controller;
        import java.util.List;
        
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Controller;
        import org.springframework.ui.Model;
        import org.springframework.web.bind.annotation.RequestMapping;
        
        
        import com.hp.entity.Backup;
        import com.hp.service.BackupService;
        /*用于用户全查的跳转*/
        @Controller
        @RequestMapping("/back")
        public class BackupController {
        	@Autowired
        	private BackupService backupservice;
        	
        	
        	@RequestMapping("/findall")
        	public String findall(Model model){
        		List<Backup> findall = backupservice.findAll();
        		System.out.println(findall);
        		model.addAttribute("findall",findall);
        		return "forwad:/findall.jsp";
        	}
        }
        
      • 前台 展示 代码

      • login.jsp 用户登录首页

        <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
        <html>
        <head>
        <title>用户登录</title>
        </head>
        <body>
        	<form action="/kaoshi/login/user" method="post">
        		<h2>用户登录</h2>
        		用户名:<input type="text" name="username"><br/>
        		密码:<input type="password" name="password"><br/>
        		<input type="submit" value="登陆">
        		<a href="/kaoshi/register.jsp"><input type="button" value="注册"></a>
        	</form>
        </body>
        </html>
        
        
      • 用户注册页面

        <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
        <html>
        <head>
        <title>用户注册</title>
        </head>
        <body>
        	<form action="${pageContext.request.contextPath}/login/insertUser" method="post">
        		<h2>用户注册</h2>
        		ID:<input type="text" name="id"><br/>
        		用户名:<input type="text" name="username"><br/>
        		密码:<input type="text" name="password"><br/>
        		<input type="submit" value="注册">
        	</form>
        </body>
        </html>
        
      • findall.jsp 全查显示页面

        <%@page contentType="text/html;charSet=UTF-8" isELIgnored="false"
        	language="java" pageEncoding="UTF-8"%>
        <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
        <c:set value="${pageContext.request.contextPath}" var="path" />
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <title>全查界面</title>
        </head>
        
        <body>
        	<center>
        		<h3>展示全查</h3>
        		<table border="1">
        			<tr>
        				<td>手机号</td>
        				<td>网址</td>
        				<td>访问时间</td>
        			</tr>
        			<c:forEach items="${requestScope.findall}" var="person">
        				<tr>
        					<td>${person.phonenum}</td>
        					<td>${person.url}</td>
        					<td>${person.time}</td>
        				</tr>
        			</c:forEach>
        		</table>
        	</center>
        </body>
        </html>
        
        
      • 注册 成功页面

        <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
        <html>
        <head>
        <title>用户注册成功</title>
        </head>
        <body>
        用户${username}注册成功
        <a href="/kaoshi/login.jsp">点击传送登陆页面</a>
        </body>
        </html>
        
        
  • 相关阅读:
    Software Update Services 2.0 概述
    Windows 2000组策略
    Windows 2000 默认安全策略设置
    C语言I博客作业06
    C语言I博客作业08
    C语言I博客作业09
    C语言I博客作业03
    C语言12019级秋季作业第一周作业
    C语言I博客作业02
    C语言I博客作业05
  • 原文地址:https://www.cnblogs.com/lszbk/p/13042375.html
Copyright © 2020-2023  润新知