Java(一百四十二)SSM框架整合(二)功能编写

58 阅读2分钟

上文中,我已经将我需要的配置全部都配置完成,数据库MyBatis相关的xml文件也已逆向生成完毕。

 

这里我开始来编写一个小功能登录,前端使用jsp代码。

 

我们来做第一个小功能,如标题所示,那就是登录。

我先将登录功能需要的文件创建出来,具体目录结构如下图所示:

11111111111111111

红框标注的文件就是登录功能需要的文件。

我们再一次来明确一下java的三层架构:

1.   controller(控制逻辑)层:它是负责在页面和程序之间传输数据的,还有作用是做页面跳转。页面由用户填写表单数据,点击提交按钮,页面的表单数据由传入Service层。

2.   Service层(业务逻辑层):负责的是对数据的处理。如果没有数据处理任务的话,此层只做单纯的数据传递作用,而后又到了dao层

3.   DAO层(数据库操作层):负责对数据向数据库增删改查的操作。

 

一:编写前端jsp文件

Index.jsp

<%--
   Created by IntelliJ IDEA.
   User: camellia
   Date: 2024/1/12
   To change this template use File | Settings | File Templates.
 --%><%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" language="java" %>
 <html>
 <head>
     <title>Title</title>
 </head>
 <body>
 <form action="user/login" method="get">
     name<input name="name">${name}
     password<input name="password">
     <button>登录</button>
     <button type="button" onclick="location.href='reg.jsp'">注册</button>
 </form>
 </body>
 </html>

 

List.jsp

<%--
   Created by IntelliJ IDEA.
   User: camellia
   Date2024/1/12
   To change this template use File | Settings | File Templates.
 --%>
 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 <html>
 <head>
     <title>Title</title>
 </head>
 <body>
 <h2>登录成功页面</h2>
 <h3 id="h3"></h3>
 </body>
 </html>
 <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
 <script>
     $(document).ready(function()
     {
         $.ajax({
             url:"list",
             type:"get",
             success:function(response){
                 alert(response)
                 $("#h3").html(response);
             }
         });
     });
 </script>

 

二:编写controller层代码

UserController.java

package com.controller;
 
 import com.service.UserService;
 import com.pojo.User;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
 
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 
 /**
  *
  */
 @Controller
 @RequestMapping("user")
 public class UserController
 {
     // 在controller中注入service
     @Autowired
     private UserService userService;
 
     @RequestMapping(value="list")
     @ResponseBody
     public String list(HttpServletResponse response)
     {
         return "机密信息,概不外泄!";
     }
 
     @RequestMapping("login")
     public String login(User user, HttpSession session)
     {
         User loginUser = userService.login(user);
         if(loginUser != null)
         {
             session.setAttribute("user",loginUser);
             System.out.println("登陆成功!");
             return "list";
         }
         return "redirect:login.jsp";
     }
 }

 

三:编写service层代码

UserService.java

package com.service;
 
 import com.pojo.User;
 
 public interface UserService
 {
     public User login(User user);
 }

 

实现类UserServiceImpl.java

package com.service.impl;
 
 import com.dao.UserDao;
 import com.pojo.User;
 import com.pojo.UserExample;
 import com.service.UserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.util.List;
 
 @Service
 @Transactional  // 被Spring管理
 public class UserServiceImpl implements UserService {
 
     // 在service中注入dao
     @Autowired
     private UserDao userDao;
 
     @Override
     public User login(User user) {
         UserExample example = new UserExample();
         UserExample.Criteria criteria = example.createCriteria();
         criteria.andNameEqualTo(user.getName());
         criteria.andPasswordEqualTo(user.getPassword());
         // 调用dao方法返回集合
         List<User> users = userDao.selectByExample(example);
         // 判断集合中数据数量
         return users.size() > 0 ? users.get(0) : null;
     }
 }

 

四:dao层代码

我这里dao层的代码没有特殊要求,数据库user表类文件以及对应的myBatis的xml文件上文中已经逆向生成过了,这里不再赘述。

 

五:数据库表sql

CREATE TABLE `user`  (
  `id` int(11NOT NULL AUTO_INCREMENT,
  `name` varchar(255CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
  `age` int(11NULL DEFAULT NULL,
  `role_id` int(11NULL DEFAULT NULL COMMENT 'role表主键',
  `password` varchar(255CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
  `money` varchar(255CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 55 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;

 

以上大概就能实现一个登录的小功能。

 

至此,SSM框架的整合也就初步的完成了。

 

有好的建议,请在下方输入你的评论。