SSM框架搭建------基于简单的用户登录注册

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Oceansidexue/article/details/80903193
  1. 新建一个web project 为SSM
    这里写图片描述
  2. 将所有需要用到的jar包导入WebRoot/WEB-INF/libs下
    链接:https://pan.baidu.com/s/1Qast8z0Fb4m_ByN3kiuorQ
    密码:4ds3
  3. 配置WebRoot/WEB-INF/web.xml
    web配置的标签及详解
<?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">
    <!-- 设置欢迎页面 -->
  <welcome-file-list>
    <welcome-file>/WEB-INF/jsp/register.jsp</welcome-file>
  </welcome-file-list>

  <!-- 配置SpringMvc的前端控制器DispatcherServlet -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- springmvc配置文件的位置 -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springconfig.xml</param-value>
    </init-param>
  </servlet>
  <!--设置在jsp页面表单提交的action="*.action"行为 -->
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping> 

  <!-- post方式中文乱码过滤器 -->
  <filter>
    <filter-name>CharEncodingFilter</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>CharEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

4.配置db.properties

#jdbc.driver=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@10.10.21.3:1521:orcl
#jdbc.username=scott
#jdbc.password=tiger

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/studentdb?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456

5.配置springconfig.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:task="http://www.springframework.org/schema/task"
    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.2.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-4.2.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
                        http://www.springframework.org/schema/task
                        http://www.springframework.org/schema/task/spring-task-4.2.xsd
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">


    <!-- 整合mybatis -->
    <!-- 加载db.properties包含数据库的连接信息 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <!-- 连接池的最大数 -->
        <property name="maxActive" value="20"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="20"></property>
    </bean>
    <!-- 配置SqlSessionFactory -->
    <bean id="sqlSessionFactory" 
        class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 指定要扫描的mapper.xml文件的位置 -->
        <property name="mapperLocations" 
           value="classpath:com/qut/mapper/*.xml"></property>
    </bean>
    <!-- mapper扫描,扫描mapper接口类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.qut.mapper"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- 启动包的扫描功能,@controller等注解 -->
    <context:component-scan base-package="com.qut"></context:component-scan>

    <!-- 模型视图的解析,请求逻辑视图名,添加前后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 事务管理 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 支持@Transactional注解 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <mvc:annotation-driven />


</beans>

6.在com.qut.pojo包下面建一个User类与数据库里面的表对应

package com.qut.pojo;

public class User {

    private int id;
    private String name;
    private String pwd;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }


}

7.在com.qut.mapper包下面写一个mapper接口UserMapper.java,定义mapper接口
此方法即mapper接口开发方法,只需定义mapper接口,不用编写mapper接口实现类。只需要在spring配置文件中定义一个mapper扫描器,自动扫描包中的mapper接口生成代代理对象
注意mapper.xml的文件名和mapper的接口名称保持一致,且放在同一个目录

package com.qut.mapper;

import com.qut.pojo.User;

public interface UserMapper {

    /**
     * 插入用户信息
     * */
    public int insert(User user);
    /**
     * 查找用户
     * @param user
     * @return
     */
    public User findUser(User user);
}

8.配置mapper扫描器在com.qut.mapper包下新建UserMapper.xml
如果将mapper.xml和mapper接口的名称保持一致且放在一个目录 则不用在springconfig.xml中进行配置

ResultMap和ResultType的差别、ParameterMap和parameterType的差别

<?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命名空间,作用就是对sql进行分类化管理,理解sql隔离 
注意:使用mapper代理方法开发,namespace有特殊重要的作用
-->
<mapper namespace="com.qut.mapper.UserMapper">

    <insert id="insert" parameterType="com.qut.pojo.User">
        insert into user values(null,#{name},#{pwd})
    </insert>

    <select id="findUser" parameterType="com.qut.pojo.User"
        resultType="com.qut.pojo.User"
    >
        select * from user where name=#{name} and pwd=#{pwd}
    </select>

</mapper>

8.定义service接口
在com.qut.service下新建一个接口UserService.java

package com.qut.service;

import com.qut.pojo.User;

public interface UserService {

    public int register(User user);
    public User login(User user);
}

9.实现service接口
在com.qut.service.impl包下新建一个UserServiceImpl.java

package com.qut.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qut.mapper.UserMapper;
import com.qut.pojo.User;
import com.qut.service.UserService;

@Service
@Transactional(rollbackFor=Exception.class)
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    public int register(User user) {
        //调用dao操作数据库
        return userMapper.insert(user);
    }

    public User login(User user) {
        return userMapper.findUser(user);
    }

}

10.编写控制器
在com.qutcontroller包下新建一个UserController.java

package com.qut.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.qut.pojo.User;
import com.qut.service.UserService;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/register")
    public ModelAndView register(User user){

        ModelAndView view = new ModelAndView();
        int res = userService.register(user);
        if(res!=1){
            //回到注册界面
            view.setViewName("register");
        }else{
            //登录界面
            view.setViewName("login");
        }
        return view;
    }

    @RequestMapping("/login")
    public ModelAndView login(User user){
        User u = userService.login(user);
        ModelAndView view = new ModelAndView();
        if(u!=null){
            view.setViewName("main");
        }else{
            //添加错误信息
            view.setViewName("login");
        }
        return view;
    }

}

11.编写jsp显示页面

没有更多推荐了,返回首页