SSM-6 登陆功能实现

一个系统基本的就是登陆功能。

这里用mysql数据库,mybatis 实现。


因为是测试,就先做个最简单的表

ID

username

password

加入一条用户,比如  1  aaa 111


先将欢迎页面写好。

<body>

<div id="login">
	<h1>系统登陆</h1>
	<form method="post" action="UserController/Login" name="LoginForm">
		<input type="text" required="required" placeholder="请输入用户名" name='username' />
		<br>
		<input type="password" required="required" placeholder="请输入密码" name="password" />
		<br>
		<input type="checkbox" name="checkbox" >保持登陆</input>
		<br>
		<button class=but type="submit" >登陆</button>		
	</form>
</div>

</body>
这里遇到一个问题,css样式不在webinf文件夹内,结果没调用出来,这个问题暂时放着后续解决。可能要解决一下项目路径关系,使页面访问到外部css文件。

暂时就简陋一点。

action="UserController/Login
写好处理对应URL的controller

代码如下:

@Controller
@RequestMapping("/usercontroller")
public class UserController {
	@Autowired
	UserService userService;
	
	User user_p;
	
	@RequestMapping(value="/login")
	public ModelAndView LoginByUser(User user){
		ModelAndView mav=new ModelAndView();
		user_p=this.userService.LoginByUser(user);
		
		if(user_p!= null){
			mav.setViewName("success");
			mav.addObject("id", user_p.getId()+"");
			mav.addObject("username", user_p.getUsername());
			mav.addObject("password", user_p.getPassword());
		}
		else{
			mav.setViewName("fail");
		}

		return mav;
	}
}


这里实用了modelandview类,实现参数传递。  具体modelandview更多功能后续学习,主要是jason数据/表/图等复杂数据的传递。


controller调用了相应的service进行业务逻辑处理

代码如下:省略了interface 接口类代码,看这个也能知道了。

@Transactional
@Service(value = "userService")
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
	
	@Override
	public User LoginByUser(User user) {
		// TODO Auto-generated method stub		
		return userMapper.loginbyuser(user);
	}

}


mapper和pojo部分用了一个神奇的工程,generatorSqlmapCustom  设定好数据库连接参数,直接获取表里各种数据格式,自动生成pojo和mapper,实在是NB了得。后期面对10多张,几十张表,自己写pojo,想想都害怕。这个工程代码网上可以找到,直接import进来,修改一下数据库的参数,生成包名字等,run as java application就行了,记得refresh一下,把生成的对应类复制倒自己的工程理。

生成代码如下:

mapper

package com.therp.mapper;

import com.therp.pojo.User;
import com.therp.pojo.UserExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface UserMapper {
    int countByExample(UserExample example);

    int deleteByExample(UserExample example);

    int deleteByPrimaryKey(Integear id);

    int insert(User record);

    int insertSelective(User record);

    List<User> selectByExample(UserExample example);

    User selectByPrimaryKey(Integer id);
    
    User loginbyuser(User user);//add by rdq 

    int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);

    int updateByExample(@Param("record") User record, @Param("example") UserExample example);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}

默认只有selectbyPrimaryKey,所以自己写了一个loginbyuser接口。

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.therp.mapper.UserMapper" >
  <resultMap id="BaseResultMap" type="com.therp.pojo.User" >
    <id column="ID" property="id" jdbcType="INTEGER" />
    <result column="username" property="username" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Example_Where_Clause" >
    <where >
      <foreach collection="oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause" >
    <where >
      <foreach collection="example.oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List" >
    ID, username, password
  </sql>
  <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.therp.pojo.UserExample" >
    select
    <if test="distinct" >
      distinct
    </if>
    <include refid="Base_Column_List" />
    from user
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from user
    where ID = #{id,jdbcType=INTEGER}
  </select>
  <!-- add by rdq  -->
  <select id="loginbyuser" resultMap="BaseResultMap" parameterType="com.therp.pojo.User" >
    select * from user
    where username = #{username,jdbcType=VARCHAR} AND password=#{password,jdbcType=VARCHAR}
   </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from user
    where ID = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.therp.pojo.UserExample" >
    delete from user
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.therp.pojo.User" >
    insert into user (ID, username, password
      )
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.therp.pojo.User" >
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        ID,
      </if>
      <if test="username != null" >
        username,
      </if>
      <if test="password != null" >
        password,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="username != null" >
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        #{password,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.therp.pojo.UserExample" resultType="java.lang.Integer" >
    select count(*) from user
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map" >
    update user
    <set >
      <if test="record.id != null" >
        ID = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.username != null" >
        username = #{record.username,jdbcType=VARCHAR},
      </if>
      <if test="record.password != null" >
        password = #{record.password,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map" >
    update user
    set ID = #{record.id,jdbcType=INTEGER},
      username = #{record.username,jdbcType=VARCHAR},
      password = #{record.password,jdbcType=VARCHAR}
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.therp.pojo.User" >
    update user
    <set >
      <if test="username != null" >
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        password = #{password,jdbcType=VARCHAR},
      </if>
    </set>
    where ID = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.therp.pojo.User" >
    update user
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR}
    where ID = #{id,jdbcType=INTEGER}
  </update>
</mapper>

<select id="loginbyuser" resultMap="BaseResultMap" parameterType="com.therp.pojo.User" >
    select * from user
    where username = #{username,jdbcType=VARCHAR} AND password=#{password,jdbcType=VARCHAR}
   </select>

这个是自己额外添加的,用于用户登录检索数据表。


最终跳转到success或fail页面。

success页面获取用户信息进行展示。   当然,实际工程中应该要考虑密码安全,比如用MD5加密等,这些以后一个一个加吧。

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
success!!!
<br>
ID:${id }
<br>
USERNAME:${username }
<br>
PASSWORD:${password }

</body>
</html>


最终输入 用户aaa 密码111

出现


success!!!
ID:1
USERNAME:aaa
PASSWORD:111


真是长舒一口气,这个功能实现,也相当于绝大部分的功能都能走通了,后面一个一个service controller 添加,一个页面一个页面写就ok了。


下一步,学习一下日志相关功能,然后加入加密功能,然后进行登陆拦截。具体erp系统因为需求不同,会先理一下基本的功能再进行实现,比如出入库管理,产品管理,客户管理,订单管理等。业务逻辑比较灵活,可以慢慢一个一个添加进去。然后还要学一学前端的一些知识。   暂定用bootstrap为主写一些页面,因为控件看起来还蛮舒服的,同时PC 手机适配相对简单,尽量一个页面适配不同屏大小。




之前的成果,都是网上找了不少资料自己归纳的,主要有

1 imooc视频,入门都是靠这个

2 spring实战,好书,就是只看了皮毛,空的时候还是得好好去看完

3 网上下了个淘淘商城培训视频,应该是培训机构用的教材,有代码有讲解有笔记,SSM框架搭建主要是参考了这个案例,然后结合自己搜到的资料搭起来

4 各种技术博客 知乎等找自己碰到的问题,求解答~


后面的路还要很长很长啊。继续努力。虽然都是业余时间在做,每天都能进步一点,收获一点,也是极好的。


新增:

为了避免刷新页面出现重新提交表单的问题,就将返回改为redirect 重定向,结果modelandview数据无法读取了,查了好久,才发现是重定向问题,model数据page有效,重定向后就无法读取了,可以借助session进行传递,非重要数据传递后记得删除。


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值