【SSM整合】-项目搭建
来源:互联网 发布:sqlserver 物化视图 编辑:程序博客网 时间:2024/06/04 17:56
先放出我的目录结构
各配置文件已经在其他博文里提到了,这里是整合项目剩余的部分,简单做个小例子测试下
事先创建好了三个页面
addStudent.jsp
<%@ page language="java" import="java.util.*" 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"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'addStudent.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="addStudent.do" method="post"> 姓名:<input type="text" name="name"/> 性别:<label for="sex1">男</label><input id="sex1" type="radio" name="sex" value="1"/> <label for="sex2">女</label><input id="sex2" type="radio" name="sex" value="2"/> <button type="submit">添加</button> </form> </body></html>
showStudent.jsp
<%@ page language="java" import="java.util.*" 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"><html><head><base href="<%=basePath%>"><title>My JSP 'showStudent.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!-- <link rel="stylesheet" type="text/css" href="styles.css"> --></head><body> <c:forEach items="${user }" var="item"> 姓名:【${item.name }】 性别:【${item.sex }】 <a href="listbyid.do?id=${item.id }">修改</a> <a href="delete.do?id=${item.id }">删除</a><br/> </c:forEach> <br></body></html>
updateStudent.jsp
<%@ page language="java" import="java.util.*" 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"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'updateStudent.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="reedit.do" method="post"> <input type="hidden" name="id" value="${s.id }"/> 姓名: <input type="text" name="name" value="${s.name }"/> 性别: <label for="sex1">男</label><input id="sex1" type="radio" name="sex" value="1" <c:if test="${s.sex==1 }">checked='checked'</c:if>/> <label for="sex2">女</label><input id="sex2" type="radio" name="sex" value="2" <c:if test="${s.sex==2 }">checked='checked'</c:if>/> <button type="submit">修改</button> </form> </body></html>
其实,addStudent和updateStudent完全可以用一个页面由于太懒就用了俩
整个业务过程从form提交表单到Controller,然后Controller调用service层去实现dao的sql查询,最终返回到controller实现页面跳转和数据传递
数据库链接配置
Controller层
package cn.fserve.controller;import java.util.List;import javax.annotation.Resource;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import cn.fserve.model.ModelStudent;import cn.fserve.service.impl.ServiceStudentImpl;import cn.fserve.util.AutoIdBySys;@Controllerpublic class ControllerStudent { @Resource private ServiceStudentImpl service; private final static Logger logger = LoggerFactory.getLogger(ControllerStudent.class); @RequestMapping(value = "showStudent.do") public ModelAndView selectAllStudent() { List<ModelStudent> list = service.selectAllStudent(); ModelAndView model = new ModelAndView(); model.addObject("user", list); model.setViewName("showStudent"); return model; }// @RequestMapping(value = "add.do")// public String add() {// return "addStudent";// } @RequestMapping(value = "addStudent.do") public String addStudent(ModelStudent student) { if(student.getName()==null||student.getSex()==0){ return "addStudent"; }else{ student.setId(AutoIdBySys.getAutoId()); service.addStudent(student); return "redirect:showStudent.do"; } } @RequestMapping(value = "listbyid.do") public ModelAndView selectById(@RequestParam("id") String id) { ModelStudent s = service.selectById(id); ModelAndView model = new ModelAndView(); model.addObject("s", s); model.setViewName("updateStudent"); return model; } @RequestMapping(value = "reedit.do") public String updateStudent(ModelStudent student) { service.updateStudent(student); return "redirect:showStudent.do"; } @RequestMapping(value = "delete.do") public String deleteStudent(@RequestParam("id")String id){ service.deleteStudent(id); return "redirect:showStudent.do"; }}
Dao
package cn.fserve.dao;import java.util.List;import org.springframework.stereotype.Repository;import cn.fserve.model.ModelStudent;@Repositorypublic interface DaoStudent { public List<ModelStudent> selectAllStudent(); public ModelStudent selectById(String id); public int addStudent(ModelStudent student); public int updateStudent(ModelStudent student); public int deleteStudent(String id);}
DaoMapper
<?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="cn.fserve.dao.DaoStudent"> <select id="selectAllStudent" resultType="cn.fserve.model.ModelStudent"> select * from student </select> <select id="selectById" parameterType="java.lang.String" resultType="cn.fserve.model.ModelStudent"> select * from student where id=#{id} </select> <insert id="addStudent" parameterType="cn.fserve.model.ModelStudent"> insert into student(id,name,sex) values(#{id},#{name},#{sex}) </insert> <update id="updateStudent" parameterType="cn.fserve.model.ModelStudent"> update student <set> <if test="name!=null"> name=#{name}, </if> <if test="sex!=null"> sex=#{sex} </if> </set> where id=#{id} </update> <delete id="deleteStudent" parameterType="java.lang.String"> delete from student where id=#{id} </delete></mapper>
Service
package cn.fserve.service;import java.util.List;import cn.fserve.model.ModelStudent;public interface ServiceStudent { public List<ModelStudent> selectAllStudent(); public ModelStudent selectById(String id); public int addStudent(ModelStudent student); public int updateStudent(ModelStudent student); public int deleteStudent(String id);}
ServiceImpl
package cn.fserve.service.impl;import java.util.List;import javax.annotation.Resource;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.test.context.ContextConfiguration;import org.springframework.transaction.annotation.Transactional;import org.springframework.web.servlet.ModelAndView;import cn.fserve.dao.DaoStudent;import cn.fserve.model.ModelStudent;import cn.fserve.service.ServiceStudent;//@ContextConfiguration({"classpath:spring-mybatis.xml"})@Servicepublic class ServiceStudentImpl implements ServiceStudent { @Resource private DaoStudent dao; private final static Logger logger = LoggerFactory.getLogger(ServiceStudent.class); @Override public List<ModelStudent> selectAllStudent() { return dao.selectAllStudent(); } @Override public ModelStudent selectById(String id) { return dao.selectById(id); } @Override public int addStudent(ModelStudent student) { return dao.addStudent(student); } @Override public int updateStudent(ModelStudent student) { return dao.updateStudent(student); } @Override public int deleteStudent(String id) { return dao.deleteStudent(id); }}
这里我调用了service的接口,实际上如果想要简单点,完全可以不要那个接口直接在service里实现dao的方法
util自己写的生成id的方法,是日期+随机数+本地ip构成
package cn.fserve.util;import java.net.InetAddress;import java.text.SimpleDateFormat;import java.util.Date;public class AutoIdBySys { public static String getAutoId() { String date = getDateSystem(); String num = getRandomNum(); String ip = getLocalIp(); return date+num+ip; } private static String getDateSystem() { Date n = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String date = sdf.format(n); return date; } private static String getRandomNum() { Double random = Math.random(); String str = random.toString().substring(2, 8); return str; } private static String getLocalIp() { InetAddress ia = null; String localip; String newLocalIp = null; try { ia = InetAddress.getLocalHost(); localip = ia.getHostAddress(); newLocalIp = localip.replace(".", ""); } catch (Exception e) { e.printStackTrace(); } return newLocalIp; }}
测试:仅做添加测试
console打出来的log
到此就全部完成了,欢迎叨扰
1 0
- 【SSM整合】-项目搭建
- SSM框架搭建项目整合学习笔记
- SSM项目整合环境的搭建
- SSM整合--新人快速搭建项目环境
- idea maven项目来搭建ssm整合
- SSM整合搭建
- SSM框架搭建整合
- Maven+SSM整合搭建
- maven+eclipse搭建项目工程ssm整合(一)
- maven+eclipse搭建项目工程ssm整合(二)
- maven+eclipse搭建项目工程ssm整合(三)
- 基于 maven 的 ssm 项目搭建三:整合 spring mvc
- 基于 maven 的 ssm 项目搭建四:整合 log4j
- 基于 maven 的 ssm 项目搭建五:整合 spring
- 五、maven项目搭建 ssm框架之spring整合redis
- SSM项目整合笔记-Eclipse搭建Maven开发环境
- spring、springmvc、mybatis整合搭建ssm项目(Eclipse+maven)
- 【java】SSM框架搭建(二)——项目整合
- 过河问题
- 真的不适合
- 《TCP/IP详解 卷1:协议》 读书笔记 第三章 IP:网际协议
- 结构体
- innodb purge--二级索引
- 【SSM整合】-项目搭建
- CrapApi介绍
- android-日期和
- mysql主从配置过程记录
- [PAT-乙级]1012.数字分类
- C#---对字符串的移除,替换,提取,切除空格,查索引
- linux入侵日志记录清除
- 关于容器
- 【AQS框架扩展】Oracle官方教程之Fork/Join