2021-06-24

 用SSM框架写了一个注册登录的小程序也算是自己的入门之作。今晚稍作修改,拿出来给大家学习,程序也许会有BUG,还请见谅。新版教程点这里哦,里面还有一个完整的项目教程

下面说一下我的运行环境:

    Myeclipse 2017 CI 1

    MySQL 5.7
    Tomcat 8.5

 

在数据库中新建user表:

有几个主要的配置文件,先了解下每个配置文件的作用。

1. web.xml:当服务启动时首先会去加载web.xml这个资源文件,里面包括了对前端控制器、乱码问题等配置。

2.applicatonContext.xml : 一般配置数据源,事物,注解 等。

在这里我使用的是applicatonContext-*.xml的形式将DAO层、Service层、Transaction层分开配置,这样便于管理

分别为applicatonContext-dao.xml、applicatonContext-service.xml、applicatonContext-transaction.xml

分开配置时,需要在web.xml中配置上下文位置

3.springmvc.xml: 里面配置的是控制层的 ,如视图解析器静态资源, mvc 文件上传,拦截器等。

4.SqlMapConfig.xml: 该配置文件为MyBatis的配置文件,里面无需配置,一切交给spring管理,但是xml文件基础配置要有。

    持久层相关配置文件 applicationContext-dao.xml

    <?xml version="1.0" encoding="UTF-8"?>
            xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
     
        
        
     
        
        
            
            
            
            
        
     
        
        
            
            
            
            
            
            
        
     
        
        
            
            
        
   

    applicationContext-service.xml

    <?xml version="1.0" encoding="UTF-8"?>
            xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
        
        
   

  applicationContext-trans.xml

    <?xml version="1.0" encoding="UTF-8"?>
            xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
     
        
        
            
            
        
     
        
        
            
                
                
                
                
                
                
                
                
                
                
                
            
        
     
        
        
                             pointcut="execution(* com.zhu.service.*.*(..))" />
        
     
   

springmvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
            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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        
        
     
        
        
     
        
        
            
            
            
            
        
   

  SqlMapConfig.xml这个无需设置,但是必须要有

    <?xml version="1.0" encoding="UTF-8" ?>
    br />     "http://mybatis.org/dtd/mybatis-3-config.dtd">
   
     
   

配置文件写好了,接下来就开始写代码

先从Mapper开始写:

    先写一个接口UserMapper.java

    package com.zhu.mapper;
     
    import com.zhu.pojo.User;
     
    public interface UserMapper {
        //登录验证
        User logincheck(User user);
        //注册
        void register(User user);
    }

    2.            在写一个UserMapper.xml与之相关联

    <?xml version="1.0" encoding="UTF-8" ?>
    br />     PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
   
       
            select *
            from user
           
                
                    and `name` = #{name}
                
                
                    AND `pwd` = #{pwd}
                
            
       
        
       
           insert into
           user(name,pwd)
           value(#{name},#{pwd})
       
   

    3.接下来就是Service层了,先写一个接口:UserService.java,内容与UserMapper.java一样

    4.然后写一个类实现UserService.java,代码比较简单,不做过多解释,大家不要忘了要给给它标记@Service

    package com.zhu.service;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
     
    import com.zhu.mapper.UserMapper;
    import com.zhu.pojo.User;
    @Service
    public class UserServiceImpl implements UserService {
       //属性注入
       @Autowired
       private UserMapper usermapper;
     
        @Override
        public User logincheck(User user) {
            User u = usermapper.logincheck(user);
            return u;
        }
     
        @Override
        public void register(User user) {
            usermapper.register(user);
            
        }
     
    }

   5.最后就是Controller层了,Controller层的代码主要是调用Service层实现的方法,实现一些操作,并与前端进行交互。

      UserController.java

    package com.zhu.controller;
     
    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 org.springframework.web.bind.annotation.RequestParam;
     
    import com.zhu.pojo.User;
    import com.zhu.service.UserService;
     
    @Controller
    public class UserController {
        @Autowired
         private UserService userService;
        //转向登录页面
        @RequestMapping("tologin")
        public String tologin(){
            return "login";    
        }
        //登录验证
        @RequestMapping("login")
        public String  login(@RequestParam("username") String username,
                @RequestParam("password") String password,Model model){
            User user = new User();
            user.setName(username);
            user.setPwd(password);
            if(userService.logincheck(user) != null){
                model.addAttribute("username",username);
                return "index";
            }
            else{
                model.addAttribute("error","账号或密码错误");
                return "login";
            }
            }
        //转向注册页面
        @RequestMapping("toregister")
        public String toregister(){
            return "register";    
        }
        //注册
        @RequestMapping("register")
        public String  register(@RequestParam("username") String username,
                @RequestParam("password") String password){
            User user = new User();
            user.setName(username);
            user.setPwd(password);
            userService.register(user);        
            return "login";
         }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值