rest

    xiaoxiao2025-04-06  22

    app_base.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <!-- 创建数据源 --> <bean id="data" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/j122?characterEncoding=utf-8&amp;allowMultiQueries=true&amp;useSSL=false"></property> <property name="username" value="root"></property> <property name="password" value="99233"></property> <property name="maxTotal" value="100"></property> <property name="maxIdle" value="20"></property> <property name="maxWaitMillis" value="2000"></property> </bean> <!-- 创建会话工厂 --> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="data"></property> <property name="configLocation" value="classpath:mybatis.cfg.xml"></property> <property name="mapperLocations"> <list> <value>classpath:orm/*.xml</value> </list> </property> </bean> <!-- 配置会话 --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"> <constructor-arg ref="sessionFactory"></constructor-arg> </bean> <!-- 配置事务管理器 --> <bean id="trans" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="data"></property> </bean> <!-- 允许使用注解配置事务 --> <tx:annotation-driven transaction-manager="trans"/> <!-- 扫描指定包中的spring注解类 --> <context:component-scan base-package="com.lovo.service"></context:component-scan> </beans>

    app_mvc.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <mvc:annotation-driven></mvc:annotation-driven> <!-- 静态资源交给默认处理器处理 --> <mvc:default-servlet-handler/> <context:component-scan base-package="com.lovo.action"></context:component-scan> </beans>

    mybatis.cfg.xml同ssm

    orm同sam

    web.xml同ssm

    action

    package com.lovo.action; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.codehaus.jackson.map.ObjectMapper; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.lovo.bean.CutPageBean; import com.lovo.bean.MessageBean; import com.lovo.service.IMessageService; @Controller @RequestMapping("/message") public class MessageAction { @Resource private IMessageService service; //method=RequestMethod.GET 表示该方法只支持GET请求 @RequestMapping(value="/findByItem",method=RequestMethod.GET) public void findByItem(int pageNum,String userName,String tel,HttpServletResponse response)throws Exception{ CutPageBean<MessageBean> cutPageBean = service.findByItem(pageNum, userName, tel); ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(response.getWriter(), cutPageBean); } @RequestMapping(value="/add",method=RequestMethod.POST) public void add(MessageBean bean,HttpServletResponse response) throws Exception{ service.add(bean); response.getWriter().print("添加成功"); } @RequestMapping(value="/del/{id}",method=RequestMethod.DELETE) public void del(@PathVariable("id") int id,HttpServletResponse response)throws Exception{ service.del(id); response.getWriter().print("删除成功"); } @RequestMapping(value="/findById",method=RequestMethod.GET) public void findById(int id, HttpServletResponse response)throws Exception { MessageBean bean = service.findById(id); ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(response.getWriter(), bean); } @RequestMapping(value="/update/{id}",method=RequestMethod.PUT) public void update(@PathVariable("id") int id,String tel,HttpServletResponse response)throws Exception{ service.update(id, tel); response.getWriter().print("修改成功"); } }

    html

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="/rest/js/jquery-1.12.1.js"></script> <script type="text/javascript"> $(function () { ajax(1); }); function ajax(pageNum) { $.get("/rest/message/findByItem","pageNum="+pageNum+"&"+$("#f1").serialize(),function(dataObj){ //alert(JSON.stringify(dataObj)); var str = ""; for(var i=0;i<dataObj.list.length;i++){ var obj = dataObj.list[i]; str += "<tr><td>"+obj.userName+ "</td><td>"+obj.tel+"</td><td>"+ "&nbsp;&nbsp;&nbsp;<a href='javascript:del("+obj.id+")'>删除</a>"+ "&nbsp;&nbsp;&nbsp;<a href='/rest/update.html?id="+obj.id+"'>修改</a></td></tr>"; } $("#data").html(str); var cutStr = ""; for (var i = 1; i <= dataObj.totalPage; i++) { cutStr += "<a href='javascript:ajax("+i+")'>"+i+"</a>"; } $("#cutPageDiv").html(cutStr); },"json"); } function del(id){ $.ajax({ type:"DELETE", url:"/rest/message/del/"+id, success:function(info){ if (info == "删除成功") { ajax(1); }else{ alert("删除异常"); } } }); } </script> </head> <body> <table border="1" cellspacing="0" width="60%"> <thead> <tr> <td>姓名</td> <td>电话</td> <td>操作</td> </tr> </thead> <tbody id="data"></tbody> </table> <div id="cutPageDiv"></div> <form action="" id="f1"> 姓名:<input type="text" name="userName"><br> 电话:<input type="text" name="tel"><br> <input type="button" value="查询" onclick="ajax(1)"> </form> <a href="/rest/add.html">添加用户</a> </body> </html>
    转载请注明原文地址: https://ju.6miu.com/read-1297785.html
    最新回复(0)