在学习shiro的途中,在github发现了一个开源项目,所需的控件刚好是自己要学习的方向.
虽然还要学习完ssm的shiro与springboot的shiro,以及接下来的种种控件和类库,但学习这个开源项目刻不容缓
[SSM的shiro使用]
SSM简单的说,就是SpringMVC负责web部分,mybatis负责数据库部分,否则没有mybatis要写的jdbc是很费时间的.
所以基于ssm的shiro项目,就是Servlet部分修改为SpringMVC,数据库部分,修改为Mybatis,然后把shiro集成进去,用这样一个思路来把项目实现.
老式Servlet:
package com.ykmimi.gobang.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LognServlet */ @WebServlet(description = "五子棋登陆", urlPatterns = { "/login" }) public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); /////* 获取用户名和密码 String username = request.getParameter("username"); String password = request.getParameter("password"); PrintWriter pw = response.getWriter(); if(username.equals("admin") && password.equals("admin")) { pw.print("登陆成功!??"); request.getRequestDispatcher("gobang.jsp"); }else { pw.print("登陆失败,请核对账户及密码.??"); // request.getRequestDispatcher(""); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
而SpringMVC:
package com.ykmimi.controller; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import com.ykmimi.bean.User; @Controller public class UserController { @RequestMapping("login") public String login(User u,ModelMap map,HttpSession session) { System.out.println(u.getName()+":"+u.getPassword()); /*map.put("uname", u.getName()); map.put("upassword", u.getPassword());*/ session.setAttribute("User", u); return "show"; } }
可以看出Servlet的方式是通过request,response配合doGet,doPost请求进行数据的传递,而
SpringMVC是通过将一整个数据封装为bean对象,继而返回页面或者返回@ResponseBody形式(JSON).
上面将整个User用户放入了session中,这个好像不是很提倡的做法,不过这里是做个测试.
再看一眼老式的JDBC:
package com.ykmimi.ipchecker.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import com.ykmimi.ipchecker.entity.SearchingData; import com.ykmimi.ipchecker.util.JDBCUtil; public class IPAddressDao { /////* 插入新数据到表addressIp public void insertSomeData(SearchingData sdata) throws SQLException, ClassNotFoundException { Connection conn = null; PreparedStatement ps = null; String sql = "insert into addressIp values(nextval(‘seq_addressIp‘),?,?,current_date)"; try { conn= JDBCUtil.getConnection(); ps = conn.prepareStatement(sql); ps.setString(1, sdata.getIpDomain()); ps.setString(2, sdata.getIp()); ps.executeUpdate(); }finally { JDBCUtil.close(null, ps, null); } } }
当使用之前老式的JDBC时,要分别对insert,select,update,delete等操作进行区分,牵扯到的有将不同的语句执行不同的方法.
比如select查询要执行executeQuery(),而其它的执行上面代码中的executeUpdate(),以及每个SQL语句都将写在java类中,
这与"高内聚,低耦合"的Java信条不符合,(比如SQL要进行更改时),那么就是要将SQL语句进行在解耦,Mybatis框架实现了这一操作.
Mybatis可以参考之前ukzq自己写的两篇博客 Mybatis体系结构 Mybatis基本工作原理
[]
原文地址:https://www.cnblogs.com/ukzq/p/10227726.html