登录,查询增删改查 农业大学考试题2

package org.mypro.dao;

import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.mypro.entity.User;
import org.mypro.entity.UserExample;

public interface UserMapper {

    // 根据id删除管理员数据
    int deleteByPrimaryKey(Integer id);

    // 将管理员数据插入admin表中
    int insert(User record);

    // 根据where语句条件查询管理员数据
    List<User> selectByExample(UserExample example);
    
    Integer countByExample(UserExample example);

    // 根据id查询管理员数据
    User selectByPrimaryKey(Integer id);

    // 根据id修改管理员数据
    int updateByPrimaryKeySelective(User record);
}
 

<?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="org.mypro.dao.UserMapper">
    <resultMap id="BaseResultMap" type="org.mypro.entity.User">

        <id column="id" jdbcType="INTEGER" property="id" />

        <result column="username" jdbcType="VARCHAR" property="username" />

        <result column="name" jdbcType="VARCHAR" property="name" />

        <result column="password" jdbcType="VARCHAR" property="password" />

        <result column="sex" jdbcType="VARCHAR" property="sex" />

        <result column="birthday" jdbcType="VARCHAR" property="birthday" />

        <result column="tel" jdbcType="VARCHAR" property="tel" />

        <result column="address" jdbcType="VARCHAR" property="address" />

        <result column="role" jdbcType="VARCHAR" property="role" />
    </resultMap>

    <sql id="Base_Column_List">
        id,username,name,password,sex,birthday,tel,address,role
    </sql>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete from
        Userlnfo
        where id = #{id}
    </delete>
    <insert id="insert" parameterType="org.mypro.entity.User">
        insert into Userlnfo
        (id,username,name,password,sex,birthday,tel,address,role
        )
        values
        (#{id},#{username},#{name},#{password},#{sex},#{birthday},#{tel},#{address},#{role}
        )
    </insert>

    <select id="selectByExample" parameterType="org.mypro.entity.UserExample"
        resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from Userlnfo
        <where>
            <if test="where != null">
                ${where}
            </if>
        </where>
    </select>


    <select id="countByExample" parameterType="org.mypro.entity.UserExample"
        resultType="Integer">
        select
        count(*)
        from Userlnfo
        <where>
            <if test="where != null">
                ${where}
            </if>
        </where>
    </select>


    <select id="selectByPrimaryKey" parameterType="java.lang.Integer"
        resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from Userlnfo
        where id = #{id}
    </select>

    <update id="updateByPrimaryKeySelective" parameterType="org.mypro.entity.User">
        update Userlnfo
        <set>

            <if test="id != null">
                id = #{id},
            </if>

            <if test="username != null">
                username = #{username},
            </if>

            <if test="name != null">
                name = #{name},
            </if>

            <if test="password != null">
                password = #{password},
            </if>

            <if test="sex != null">
                sex = #{sex},
            </if>

            <if test="birthday != null">
                birthday = #{birthday},
            </if>

            <if test="tel != null">
                tel = #{tel},
            </if>

            <if test="address != null">
                address = #{address},
            </if>

            <if test="role != null">
                role = #{role},
            </if>
        </set>
        where id = #{id}
    </update>


</mapper>

package org.mypro.entity;

public class User {

    // 管理员id信息
    private Integer id;

    // 管理员id数据获取方法
    public Integer getId() {
        return id;
    }

    // 管理员id数据设置方法
    public void setId(Integer id) {
        this.id = id;
    }

    // 用户编码数据信息
    private String username;

    // 用户编码数据获取方法
    public String getUsername() {
        return username;
    }

    // 用户编码数据设置方法
    public void setUsername(String username) {
        this.username = username;
    }

    // 用户名称数据信息
    private String name;

    // 用户名称数据获取方法
    public String getName() {
        return name;
    }

    // 用户名称数据设置方法
    public void setName(String name) {
        this.name = name;
    }

    // 用户密码数据信息
    private String password;

    // 用户密码数据获取方法
    public String getPassword() {
        return password;
    }

    // 用户密码数据设置方法
    public void setPassword(String password) {
        this.password = password;
    }

    // 用户性别数据信息
    private String sex;

    // 用户性别数据获取方法
    public String getSex() {
        return sex;
    }

    // 用户性别数据设置方法
    public void setSex(String sex) {
        this.sex = sex;
    }

    // 出生日期数据信息
    private String birthday;

    // 出生日期数据获取方法
    public String getBirthday() {
        return birthday;
    }

    // 出生日期数据设置方法
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    // 用户电话数据信息
    private String tel;

    // 用户电话数据获取方法
    public String getTel() {
        return tel;
    }

    // 用户电话数据设置方法
    public void setTel(String tel) {
        this.tel = tel;
    }

    // 用户地址数据信息
    private String address;

    // 用户地址数据获取方法
    public String getAddress() {
        return address;
    }

    // 用户地址数据设置方法
    public void setAddress(String address) {
        this.address = address;
    }

    // 用户角色数据信息
    private String role;

    // 用户角色数据获取方法
    public String getRole() {
        return role;
    }

    // 用户角色数据设置方法
    public void setRole(String role) {
        this.role = role;
    }
}
 

package org.mypro.entity;

public class UserExample {

    // 管理员where语句内容
    private String where;

    // 管理员where语句内容获取方法
    public String getWhere() {
        return where;
    }

    // 管理员where语句内容设置方法
    public void setWhere(String where) {
        this.where = where;
    }
}
 

package org.mypro.front;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mypro.dao.UserMapper;
import org.mypro.entity.User;
import org.mypro.entity.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller

@RequestMapping(value = "/")

public class UserController {

    private static final Log logger = LogFactory.getLog(UserController.class);

    @Autowired
    private UserMapper admindao;

    @RequestMapping(value = "userRegist")
    public String regist(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        return "userRegist";
    }

    @RequestMapping(value = "registact")
    public String registact(HttpServletRequest request, HttpServletResponse response, User admin, String identity,
            HttpSession session, String repassword) throws Exception {

        if (!repassword.equals(admin.getPassword())) {

            request.setAttribute("message", "两次密码不一致");
            return "userRegist";
        } 

        UserExample example = new UserExample();

        example.setWhere("and username = '" + admin.getUsername() + "'");

        List admins = admindao.selectByExample(example);

        if (!admins.isEmpty()) {

            request.setAttribute("message", "该账号已存在");

            return "userRegist";

        } else {

            admindao.insert(admin);

            request.setAttribute("message", "注册成功,请登录");

            return "userRegist";

        }

    }

    
}
 

# Configure logging for testing: optionally with log file
log4j.rootLogger=DEBUG, stdout
# log4j.rootLogger=WARN, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

<?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>
 
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING" />
        <setting name="cacheEnabled" value="true"/>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
        <setting name="localCacheScope" value="SESSION"/>
    </settings>
 
 
</configuration>

<?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:p="http://www.springframework.org/schema/p"

    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-4.0.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-4.0.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- 1. 数据源 : DriverManagerDataSource -->

    <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/SysUserlinfo?characterEncoding=UTF-8" />

        <property name="username" value="root" />

        <property name="password" value="root" />

    </bean>

    <!--

        2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源

        MyBatis定义数据源,同意加载配置

    -->

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

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

        <property name="configLocation" value="classpath:mybatis-config.xml" />

    </bean>

    <!--

        3. mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory

        basePackage:指定sql映射文件/接口所在的包(自动扫描)

    -->

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <property name="basePackage" value="org.mypro.dao"></property>

        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>

    </bean>

    <!--

        4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源

    -->

    <bean id="txManager"

        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

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

    </bean>

    <!-- 5. 使用声明式事务

         transaction-manager:引用上面定义的事务管理器

     -->

    <tx:annotation-driven transaction-manager="txManager" />

</beans>

Manifest-Version: 1.0
Class-Path: 
 


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>注册</title>
</head>
<body>
<form action="registact.action" method="post">
                <h1>注册</h1>

                <div class="form-group">
                    <label>用户编码</label>
                    <!-- 定义 用户编码输入框 -->

                    <input name="username" required class="form-control">
                </div>
                <div class="form-group">
                    <label>用户名称</label>
                    <!-- 定义 用户名称输入框 -->

                    <input name="name" required class="form-control">
                </div>
                <div class="form-group">
                    <label>用户密码</label>
                    <!-- 定义 用户密码输入框 -->

                    <input name="password" required class="form-control">
                </div>
                <div class="form-group">
                    <label>确认密码</label>
                    <!-- 定义确认密码输入框 -->

                    <input name="repassword" required class="form-control">
                </div>
                <div class="form-group">
                    <label>用户性别</label>
                    <!-- 定义 用户性别输入框 -->

                    <input name="sex" required class="form-control">
                </div>
                <div class="form-group">
                    <label>出生日期</label>
                    <!-- 定义 出生日期输入框 -->

                    <input name="birthday" required class="form-control">
                </div>
                <div class="form-group">
                    <label>用户电话</label>
                    <!-- 定义 用户电话输入框 -->

                    <input name="tel" required class="form-control">
                </div>
                <div class="form-group">
                    <label>用户地址</label>
                    <!-- 定义 用户地址输入框 -->

                    <input name="address" required class="form-control">
                </div>
                <div class="form-group">
                    <label>用户角色</label>
                    <!-- 定义role下拉框 并定义其onchange方法rolechange(this)" -->

                    <select id='role' οnchange="rolechange(this)" name="role"
                        class="form-control">
                        <option>库管员</option>
                        <option>营业员</option>
                        <option>保洁</option>
                    </select>
                    <!-- 为下拉框添加自定义数据 -->


                </div>

                <div class="form_item">
                    <input qw-c="operationbutton" type="submit" value="注册">
                </div>
            </form>

    
    <script>
        if("${message}"){
        alert("${message}");
        }
   </script>
</body>
</html>

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

    <!-- 注解扫描包 -->
    <context:component-scan base-package="org.mypro" />

    <!-- 开启注解 -->
    <mvc:annotation-driven />

    <!--
        配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd
    -->
    <mvc:resources mapping="/resource/**" location="/resource/" />
    

    <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
        <property name="prefix" value="/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设定默认编码 -->
        <property name="defaultEncoding" value="UTF-8" />
        <!-- 设定文件上传的最大值5MB,100*1024*1024 -->
        <property name="maxUploadSize" value="104857600" />
        <property name="maxInMemorySize" value="20460" />
    </bean>
</beans>
 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <context-param>
  <param-name>MyApplicationTitle</param-name>
  <param-value>ssm</param-value>
 </context-param>
 <context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>/WEB-INF/classes/log4j.properties</param-value>
 </context-param>
 <context-param>
  <param-name>log4jRefreshInterval</param-name>
  <param-value>60000</param-value>
 </context-param>
 <!-- 设置Spring容器加载所有的配置文件的路径 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:spring-*.xml</param-value>
 </context-param>
 <!-- 解决工程编码过滤器 -->
 <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>
  <init-param>
   <param-name>forceEncoding</param-name>
   <param-value>true</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <!-- 加载Spring容器配置 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <listener>
  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
 </listener>
 <!-- 防止Spring内存溢出监听器 -->
 <listener>
  <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
 </listener>
 <!-- 配置SpringMVC核心控制器 -->
 <servlet>
  <servlet-name>springMVC</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 配置初始配置化文件,前面contextConfigLocation看情况二选一 -->
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath*:spring-mvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <!--为DispatcherServlet建立映射 -->
 <servlet-mapping>
  <servlet-name>springMVC</servlet-name>
  <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
  <url-pattern>*.action</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>userLogin.action</welcome-file>
 </welcome-file-list>
 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
</web-app>
 

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    This is my JSP page. <br>
    <a href="login.action">点击</a>
  </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值