mybatis3.3+struts2.3.24+mysql5.1.22开发环境搭建图文教程

时间:2021-05-19

一、新建Web工程,并在lib目录下添加jar包
主要jar包:struts2相关包,mybatis3.3相关包,mysql-connector-java-5.1.22-bin.jar, gson-2.1.jar

二、配置web.xml,添加一个过滤器StrutsPrepareAndExecuteFilter,处理所有*.action请求;

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://.google.gson.Gson;public class UserAction { Logger logger = Logger.getLogger(UserAction.class); private UserService userService = new UserService(); public void addUser(){ PrintWriter out = null; try{ HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/html;charset=UTF-8"); String account = request.getParameter("account"); String name = request.getParameter("name"); String address = request.getParameter("address"); User user = new User(); user.setAccount(account); user.setAddress(address); user.setName(name); userService.add(user); out = response.getWriter(); out.write(new Gson().toJson("success")); }catch(Exception e){ e.printStackTrace(); logger.error(e.getMessage()); if(out != null) out.write(new Gson().toJson("fail")); }finally{ out.flush(); out.close(); } } public void queryAllUser(){ PrintWriter out = null; try { HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/html;charset=UTF-8"); Gson gson = new Gson(); List<User> userList= userService.queryAllUser(); String gsonStr = gson.toJson(userList); out = response.getWriter(); out.write(gsonStr); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); if(out != null) out.write(new Gson().toJson("fail")); }finally{ out.flush(); out.close(); } }}

前端代码:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><style>.mt20{ margin-top: 20px;}</style></head><body><div style="text-align: center;"> <div class="mt20"><label>账号:</label><input id="account" type="text"/></div> <div class="mt20"><label>姓名:</label><input id="name" type="text"/></div> <div class="mt20"><label>地址:</label><input id="address" type="text"/></div> <div class="mt20"><button id="addUser" >添加</button></div></div><h3>用户列表:</h3><ul id="userList"></ul><script type="text/javascript" src="js/jquery-1.11.1.min.js"></script><script> $(function() { $.ajax({ url : 'queryAllUser.action', type : 'post', dataType : 'json', success : function(data) { try { for(var i = 0; i < data.length; i++){ $("#userList").append("<li><span style='color:red'>id="+data[i].id+"</span>,account="+data[i].account+",name="+data[i].name+",address="+data[i].address+"</li>"); } } catch (e) {}; } , error : function(e) { alert("sys error"); } }); $("#addUser").on("click", function() { var account = $("#account").val(); var name = $("#name").val(); var address = $("#address").val(); $.ajax({ url : 'addUser.action', type : 'post', dataType : 'json', data : { account : account, name : name, address : address }, success : function(data) { try { $("#userList").append("<li>account="+account+",name="+name+",address="+address+"</li>"); alert("添加成功"); } catch (e) { } }, error : function(e) { alert("sys error"); } }); }); });</script></body></html>

六、测试效果:

struts2运行正常;


测试二级缓存是否OK,查询所有User;

第一次查询:cache未命中,访问数据库:

第二次以及接下来的多次查询,cache命中,没有访问数据库:


@author 风一样的码农

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章