前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ssm(springMVC + spring+MyBatis) 小例

ssm(springMVC + spring+MyBatis) 小例

作者头像
微风-- 轻许--
发布2022-04-13 08:46:33
1800
发布2022-04-13 08:46:33
举报
文章被收录于专栏:java 微风

整体环境参见本人另一文:http://blog.csdn.net/jiangyu1013/article/details/51983360

此工程访问入口为index.jsp页面.

工程结构:

1. jap 页面:

index.jsp :

代码语言:javascript
复制
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>去确定页面</title>
</head>
<body>
 <a href="javascript:<jsp:forward page='WEB-INF/views/sure.jsp'/>"></a>
</body>
</html>

sure.jsp :

代码语言:javascript
复制
<%@ 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+"/";%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>确定页面</title>
<script type="text/javascript" src="js/jquery-1.12.0.min.js"></script>
<script type="text/javascript" src="js/jquery.watermark.min.js"></script>
<script language="javascript">

function doSubmit(){
	$.ajax({
		url:"<%=basePath%>sure/to",
		type:"post",
		data:$("#formMark").serialize(),
		success:function(data){
			if(data){
				window.kk="<%=basePath%>sure/to";
			}else{
				alert(data);
			}
		},
		error:function(){alert("有异常!");}
	});
}
</script>
</head>
<body>
<div>
	<div><span>确定页面</span></div>
	<form id="formMark">
		<table>
			<tr>
				<td width="15%">
					你想知道book表有多少条数据吗?
				</td>
			</tr>
		</table>
		<div>
			<input type="button" style="margin-left:15px;" value="想" onclick="doSubmit()"/>
		</div>
	</form>
</div>
</body>
</html>

result.jsp :

代码语言:javascript
复制
<%@ 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+"/";%> --%>
<%-- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> --%>
<!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>结果页面</title>
</head>
<body>
<div>
	<div ><span>结果页面</span></div>
	<table >
		<tr>
			<td>
				book表数据总条数为:${count} 条。
			</td>
		</tr>
	</table>
</div>
</body>
</html>

stillResult.jsp :

代码语言:javascript
复制
<%@ 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+"/";%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!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>结果页面</title>
<base href="<%=basePath%>">
</head>
<body>
<div>
	<div ><span>结果页面</span></div>
	<table >
		<tr>
			<td>
				这是still页面。
			</td>
		</tr>
	</table>
</div><pre name="code" class="html">

</body></html>

2.代码部分:

SureController.java

代码语言:javascript
复制
package com.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.service.SureServiceImpl;

@Controller
@RequestMapping("/sure")
public class SureController {
	
	@Resource
	private SureServiceImpl sureService;
	
	@RequestMapping("/to")
	public String getCount(ModelMap model) throws Exception {
		
		int count = sureService.getCount();
		model.addAttribute("count",count);
		return "result";
	}
	
	@RequestMapping(value="/still")
	public String still(ModelMap model) throws Exception {
		return "stillResult";
	}
}

ISureService.java

代码语言:javascript
复制
package com.service;

public interface ISureService {

	public int getCount();
}

SureServiceImpl.java

代码语言:javascript
复制
package com.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mapper.SureMapper;

@Service("sureService")
public class SureServiceImpl implements ISureService {

	@Resource
	private SureMapper sureMapper;
	
	public int getCount() {		
		return sureMapper.getTheCount();
	}

}

SureMapper.java

代码语言:javascript
复制
package com.mapper;

public interface SureMapper {

	public int getTheCount();
		
}

SureMapper.xml

表名选择任意数据库已经有的表即可。

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.mapper.SureMapper" >

  <select id="getTheCount" resultType="java.lang.Integer">
    select count(1) from BOOK
  </select>
  
</mapper>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016/07/21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档