使用SpeingMVC做正删改查

大家好:

        痛苦的挨过了一天的苦日子,所以我想跟大家分享一下我的经验体会,不动手永远不会操作!加油!坚持就是胜利

Maven的分模块管理
-benmu-parent
   -benmu-core-->entity,mapper
   -benmu-service-->service
   -benmu-web--> web


   -basic-common
   -basic-mybatis
   -basic-redis

在Maven中主要是前3部分,分别写什么,就一目了然了!

1)首先,在mybatorConfig.xml做配置!

<!DOCTYPE generatorConfiguration

  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!-- classPathEntry:数据库的JDBC驱动的jar包地址 这里的路径一定要跟你项目中的本地路径一直 -->
    <classPathEntry location="D:\Repositories\Maven\mysql\mysql-connector-java\5.1.23\mysql-connector-java-5.1.23.jar" />
    <context id="benmu" targetRuntime="MyBatis3"
        defaultModelType="conditional">
    <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
<!--这是链接数据库的语句-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://117.121.26.70:3305/benmu" userId="kaifa"
            password="99754106633f94d350db34d548d6091a">
        </jdbcConnection>
     <!--  默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer   
             true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal   

         -->   

      <javaTypeResolver >  
         <property name="forceBigDecimals" value="false" />  
      </javaTypeResolver>  
       <!-- targetProject:自动生成代码的位置 想生成在哪就把路径改成哪-->  
        <javaModelGenerator targetPackage="com.benmu.medical.system.test.entity"
            targetProject="benmu-core/src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->  
            <property name="enableSubPackages" value="true" />
             <!-- 从数据库返回的值被清理前后的空格  -->   
             <property name="trimStrings" value="true" />

        </javaModelGenerator>

<!-- 此处是mapper生成的路径-->

        <sqlMapGenerator targetPackage="com.benmu.medical.system.test.mapper"
            targetProject="benmu-core/src/main/java">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
         <!-- 生成dao -->
        <javaClientGenerator targetPackage="com.benmu.medical.system.test.mapper"
            targetProject="benmu-core/src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true" />

        </javaClientGenerator> 

<!--此处是实体类对应的数据库表 t_test:数据库表, Test:实体类-->

<table tableName="t_test" domainObjectName="Test"
            enableInsert="true" enableSelectByPrimaryKey="true"
            enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="true" >
        </table>
    </context>

</generatorConfiguration>


2)右击mybatorConfig.xml ->genetate Mybatis/Ibatis Artifacts 自动生成配置中的 Test.java \TestMapper.java \ TestMapper.xml三个文件

在Service层没什么要说的,但是要注意的是Service 的实现 :在实现类上要添加@Service 这里要是忘添加的话tomcat会报错的

@Service
public class TestServiceImpl implements TestService {
@Autowired   //这里的注解要看清楚了!
private TestMapper TestMapper;  
/*
* 查询所有信息
*/
@Override 
public List<Test> getListAll(){
return TestMapper.getListAll();

}
}

3)TestAController 控制层

package com.benmu.medical.system.core.web;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.benmu.basic.mybatis.PageInfo;
import com.benmu.medical.order.info.entity.OrderInfoEntity;
import com.benmu.medical.system.test.entity.Test;
import com.benmu.medical.test.service.TestAService;


@Controller //控制器相当于action
public class TestAController{
    @Autowired
    private TestAService testService;
    
    /*
     * 不同请求方式(get,post),映射不同的方法
     * value 指定请求路径,method 指定请求方式
     */

    @RequestMapping(value = "/test", method = RequestMethod.GET) //请求映射
    public ModelAndView getListAll(
            PageInfo<Test> pageInfo,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "pageNumber", required = false) Integer pageNumber,

            ModelMap modelMap) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        pageInfo = testService.getList(pageInfo, map);//此处使用到了分页,如果只是显示,请把此句注掉

        List<Test> testList= testService.getListAll();

        modelMap.put("testList", testList);
        modelMap.put("pageInfo", pageInfo);
        return new ModelAndView("/common/test", modelMap);     //返回页面
    }
    /*
     * 添加
     */
    @RequestMapping(value = "/testAdd", method = RequestMethod.POST) //请求映射
    public ModelAndView postAddTest(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "address", required = false) String address,

            ModelMap modelMap) throws Exception {
            Test record = new Test();
            
            record.setName(name);
            record.setAddress(address);
        int number = testService.insert(record);
        if(number>0){
            List<Test> testList= testService.getListAll();
            modelMap.put("testList", testList);
            return new ModelAndView("/common/test", modelMap);
        }else{
            return new ModelAndView("/common/testAdd", modelMap);
        }
    }
    /*
     * 修改
     */
    @RequestMapping(value = "/testUpdate", method = RequestMethod.POST) //请求映射
    public ModelAndView postUpdateTest(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "address", required = false) String address,

            ModelMap modelMap
) throws Exception {
        Test record = new Test();
        record.setId(id);
        record.setName(name);
        record.setAddress(address);
        int number = testService.updateByPrimaryKey(record);
        if(number>0){
            List<Test> testList= testService.getListAll();
            modelMap.put("testList", testList);
            return new ModelAndView("/common/test", modelMap);
        }else{
            return new ModelAndView("/common/testUpdate", modelMap);
        }
    }
    /*
     * 删除
     */
    @RequestMapping(value = "/testDelect", method = RequestMethod.POST) //请求映射
    public ModelAndView postDelectTest(
            @RequestParam(value = "id", required = false) Integer id,
            ModelMap modelMap) throws Exception {
        Test record = new Test();
        record.setId(id);
        int number = testService.deleteByPrimaryKey(id);
        if(number>0){
            List<Test> testList= testService.getListAll();
            modelMap.put("testList", testList);
            return new ModelAndView("/common/test", modelMap);
        }else{
            return new ModelAndView("/common/test", modelMap);
        }
    }
}

4)JSP页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="<%=basePath%>script/jquery-1.8.3.js" type="text/javascript"></script>
</head>
<body>
<p> 这是一个测试的页面  </p>

 <table>
 <tr>
 <td>用户名:</td>
 <td>地  址:</td>
 <td></td>
 </tr>
 <c:forEach items="${pageInfo.rows}" var="te">
 
 <tr>
 <td>
 <input type="text" name="name" id="name" value="${te.name}">
 </td>
 <td>
 <input type="text" name="address" id="address" value="${te.address}">
 </td>
 <td>
 <input type="hidden" name="id" id="id" value="${te.id}">
 </td>
 <td>
 <a href="view/common/testUpdate.jsp?id='${te.id}'&name='${te.name}'&address='${te.address}'">修改</a>
 </td>
 <td>
 <input type="button" value="删除" οnclick="delectA()">
 </td>
 </tr>
 </c:forEach>
 <tr>
 <td colspan="3">
 <a href="view/common/testAdd.jsp">添加</a>
 </td>

 </tr>
 </table>
 
</body>
  <!-- 翻页 -->
     <jsp:include page="/view/common/pagination.jsp" />
  <!-- 分页 -->
<script type="text/javascript">
function delectA(){
    var id = $("#id").val();
    $.post("/benmu/testDelect",
            {"id":id},
            function(data){
    });
}
</script>

</html>

@添加页面

<%@ 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">
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<p> 这是测试的添加页面  </p>
<form action="/benmu/testAdd" method="post" id="add">
 <table>
 <tr>
 <td>用户名:<input type="text" name="name" id="name" ></td>
 <td>地  址:<input type="text" name="address" id="address"></td>
 </tr>
 <tr>
 <td><input type="submit" value="确定"></td>
 <td><input type="reset" value="重置"></td>
 </tr>
 </table>
 </form>
</body>
</html>

@修改页面(涉及一个页面间的传参问题)

<%@ 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">
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<p> 这是一个修改测试的页面  </p>
<form action="/benmu/testUpdate" method="post" id="update">
<%
String id =request.getParameter("id");
String name = request.getParameter("name");
String address = request.getParameter("address");
%>

 <table>
 <tr>
  <td>ID:<input type="text" name="id" id="id" value=<%=id%>></td>
 <td>用户名:<input type="text" name="name" id="name" value=<%=name%>></td>
 <td>地  址:<input type="text" name="address" id="address" value=<%=address%>></td>
 <td colspan="3">
<input type="submit" name="xiugai" value="修改">
 </td>
 </tr>
 </table>
 </form>
</body>
</html>

这是一个简单的增删改查的小项目,整了一天,学到了挺多!继续加油!此项目后来涉及到的分页,牵扯到了代码,直接运行可能会出bug

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值