SpringBoot搭建SSM项目二

一、创建数据库表和对应的java实体类

         1、创建t_user表

SET FOREIGN_KEY_CHECKS=0;
-- Table structure for `t_user`
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '用户表id',
  `user_id` varchar(255) NOT NULL COMMENT '用户id',
  `user_name` varchar(255) NOT NULL COMMENT '用户名',
  `pass_word` varchar(255) NOT NULL COMMENT '用户密码',
  `address` varchar(255) DEFAULT NULL COMMENT '用户地址',
  `picture` varchar(255) DEFAULT NULL COMMENT '用户头像',
  `gender` varchar(255) DEFAULT NULL COMMENT '性别',
  `age` int(11) DEFAULT NULL COMMENT '年龄',
  `eamil` varchar(255) DEFAULT NULL COMMENT '用户邮箱',
  `integral` int(11) DEFAULT '0' COMMENT '用户积分',
  `create_time` datetime DEFAULT NULL,
  `update_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
  `role_id` int(11) NOT NULL COMMENT '角色id',
  `state` int(11) NOT NULL DEFAULT '1' COMMENT '使用状态 0 注销  1使用',
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

  2、java试题类,这里只贴属性名称

package cn.fcw.bb.webapply.model;
import java.io.Serializable;
public class UserBean  implements Serializable {
    private String  userName;
    private String  passWord;
    private String  address;
    private int     age;
    private char    gender;
    private String  userId;
    private int     id;
    private int     roleId;
    private int     state;
}

3、在mpper报下创建dao层接口和相应xml实现

package cn.fcw.bb.webapply.mapper;
import cn.fcw.bb.webapply.model.UserBean;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface IUserMapper {
    /**查詢所有用戶*/
    List  findAll();
     /**使用id查询用户*/
    UserBean        findUserById(int  id);
    /**修改用户资料*/
    void            updateUser(UserBean userBean);
    /**用户登录*/
    UserBean        login(Map  map);
    /**用户注册*/
    void            register(@Param("userName")String name,@Param("passWord")String  pwd,@Param("roleId")int  roleId);
    
    /**查询用户名的个数*/
    int             findUserByName(String name);
    /**新增用户(造测试数据用)*/
    void            insertUser(UserBean userBean);
}

UserMaper.xml配置sql:





  
      
      
      
      
      
      
      
      
  

    
        user_name,pass_word,address,gender,user_id
    

    


    


    


    


    
        INSERT  INTO
            t_user
            (user_name,pass_word,role_id)
        VALUES
             (#{userName},#{passWord},#{roleId})
    

    
        UPDATE
           t_user
        SET
          user_name=#{userName},
          address=#{address}
        WHERE
           id=#{id}
    


    
        INSERT INTO
              t_user
             (user_name,pass_word,address,gender,user_id)
        VALUES
              (#{userName},#{passWord},#{address},#{gender},#{userId})
    

4、创建service接口和实现类

package cn.fcw.bb.webapply.service;
import cn.fcw.bb.webapply.base.BaseBean;
import cn.fcw.bb.webapply.model.UserBean;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
public interface IUserService {
    //此处省略异常抛出
    @Transactional
    List       findAll();
    
    @Transactional
    UserBean             findUserById(int  id);
    
   @Transactional
    BaseBean   register(String name,String pwd,int  roleId);

    @Transactional
    BaseBean   login(String  name,String pwd);

    @Transactional
    UserBean             updateUser(UserBean  userBean);

    @Transactional
    void                 insertUser(UserBean userBean);
 }

  服务实现类业务

package cn.fcw.bb.webapply.service.impl;
import cn.fcw.bb.webapply.base.BaseBean;
import cn.fcw.bb.webapply.mapper.IUserMapper;
import cn.fcw.bb.webapply.model.UserBean;
import cn.fcw.bb.webapply.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserMapper userMapper;

    @Override
    public List findAll() {
        return  userMapper.findAll();
    }

    @Override
    public UserBean findUserById(int id) {
        return userMapper.findUserById(id);
    }

    @Override
    public BaseBean register(String name, String pwd, int roleId) {
        BaseBean  baseBean=new BaseBean<>();
        int  num=  userMapper.findUserByName(name);
         if (num>0){
             baseBean.setMsg("该用户名已经被使用");
             baseBean.setRes(0);
         }else {
             Map  map=new HashMap<>();
             map.put("userName",name);
             map.put("passWord",pwd);
             map.put("rileId",1);
             userMapper.register(map);

             HashMap  userMap=new HashMap<>();
             userMap.put("userName",name);
             userMap.put("passWord",pwd);
             UserBean u=userMapper.login(userMap);
             if (u!=null){
                 baseBean.setMsg("恭喜您!注册成功");
                 baseBean.setData(u);
                 baseBean.setRes(1);
             }else {
                 baseBean.setMsg("注册成功失敗!");
                 baseBean.setData(null);
                 baseBean.setRes(0);
             }

         }
         return baseBean;
    }

    @Override
    public BaseBean login(String name,String pwd) {
        BaseBean  userBeanBaseBean=new BaseBean<>();
        int  num= userMapper.findUserByName(name);
        if (num==0){
            userBeanBaseBean.setMsg("该用户不存在");
            userBeanBaseBean.setRes(0);
        }else {
            Map  map=new HashMap<>();
            map.put("userName",name);
            map.put("passWord",pwd);
            UserBean u= userMapper.login(map);
           if (u==null){
               userBeanBaseBean.setMsg("登录密码错误");
               userBeanBaseBean.setRes(0);
           }else {
               int  state=u.getState();
               if (state==1) {
                   userBeanBaseBean.setMsg("登录成功");
                   userBeanBaseBean.setRes(1);
                   userBeanBaseBean.setData(u);
               }else {
                   userBeanBaseBean.setMsg("该用户已经注销");
                   userBeanBaseBean.setRes(1);
                   userBeanBaseBean.setData(null);
               }

           }
        }
        return userBeanBaseBean;
    }

    @Override
    public void insertUser(UserBean userBean) {
        userMapper.insertUser(userBean);
    }

    @Override
    public UserBean updateUser(UserBean userBean) {
        userMapper.updateUser(userBean);
        return  userMapper.findUserById(userBean.getId());
    }

}

  5、controller层业务

  配置启动项目后自动访问登录页面   

package cn.fcw.bb.webapply.ctl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {
    @RequestMapping("/")
    public String  index(){
        return "user/login";
    }
}

 创建登录的controller

package cn.fcw.bb.webapply.ctl;
import cn.fcw.bb.webapply.base.BaseBean;
import cn.fcw.bb.webapply.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService  userService;

   @RequestMapping("/userLogin")
    public String  login(String  userName, String passWord){
        BaseBean  u= userService.login(userName,passWord);
        System.out.println("BaseBean=="+u.toString());
        int  res=u.getRes();
        return res==1?"admin/main":"/";
    }
}

6、视图界面:

     加入静态资源jquery和bootstrap,引入easyui,动态页面整合freemarker模板,pom.xml加入依赖


        
            org.springframework.boot
            spring-boot-starter-freemarker
        

   在templates下创建user文件夹子,在user下创建login.ftl




天下新闻系统



















天下新闻管理系統欢迎您!

    

你可能感兴趣的:(javaweb)