目录

介绍

整合

1. pom.xml

2. web.xml

3. 新建包、目录、文件

4. applicationContext.xml

5. dispatcherServlet.xml

6. jdbc.properties

7. mybatis.xml

8. 写java代码

9. 写jsp代码

结束


介绍

来自 动力节点王鹤

功能:

  1. 添加学生
  2. 查询学生

mysql数据库设计

附创建语句

DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(80) DEFAULT NULL,`age` int(11) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

整合

1. pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.bjpowernode</groupId><artifactId>ch07</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!--servlet--><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><!--jsp--><dependency><groupId>javax.servlet.jsp</groupId><artifactId>javax.servlet.jsp-api</artifactId><version>2.3.1</version><scope>provided</scope></dependency><!--springmvc--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.5.RELEASE</version></dependency><!--事务相关--><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.5.RELEASE</version></dependency><!--jackson--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.9.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.0</version></dependency><!--mybatis--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.6</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.1</version></dependency><!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.9</version></dependency><!-- druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.12</version></dependency></dependencies><build><!--将src下以及resources目录下的properties、xml文件编译后写出到target目录--><resources><resource><directory>src/main/java</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>false</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>false</filtering></resource></resources></build>
</project>

2. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!--配置springmvc DispatcherServlet中央调度器--><servlet><servlet-name>springMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:conf/dispatcherServlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping><!--注册spring监听器--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:conf/applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--注册字符集过滤器 强制utf-8编码--><filter><filter-name>characterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param><init-param><param-name>forceRequestEncoding</param-name><param-value>true</param-value></init-param><init-param><param-name>forceResponseEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>characterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

3. 新建包、目录、文件

新建4个包 分别是controller,dao,domain,service

新建resources/config目录 以及该目录下的applicationContext.xml、dispatcherServlet.xml、jdbc.properties、mybatis.xml文件,分别表示spirng配置文件、spirngMVC配置文件、数据库连接信息文件、mybatis配置文件

新建src/main/webapp/WEB-INF/jsp目录 以及该目录下的result.jsp文件 该文件不希望被直接访问到

在src/main/webapp/目录中 新建index.jsp、addStudent.jsp、listStudent.jsp,分别表示导航首页、添加学生页,查询学生页。

4. applicationContext.xml

主要是配置service和dao

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--获取数据库连接信息--><context:property-placeholder location="classpath:conf/jdbc.properties" /><!--使用druid数据源--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"init-method="init" destroy-method="close"><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><!--创建SqlSessionFactory--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="configLocation" value="classpath:conf/mybatis.xml" /></bean><!--声明mybatis扫描器,创建dao对象--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /><property name="basePackage" value="com.bjpowernode.dao" /></bean><!--声明 service的注解@Service所在包的位置--><context:component-scan base-package="com.bjpowernode.service" /><!--事务配置(注解或aspectj)-->
</beans>

5. dispatcherServlet.xml

配置spirngMVC,主要是组件controller、视图view的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--组件扫描器--><context:component-scan base-package="com.bjpowernode.controller" /><!--视图解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean><!--注解驱动--><mvc:annotation-driven /></beans>

6. jdbc.properties

数据库连接信息 改成自己的

jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=333

7. mybatis.xml

配置mybatis

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!--开启mybatis日志--><settings><setting name="logImpl" value="STDOUT_LOGGING"/></settings><!--配置别名--><typeAliases><package name="com.bjpowernode.domain"/></typeAliases><!--声明mapper文件--><mappers><package name="com.bjpowernode.dao"/></mappers>
</configuration>

8. 写java代码

文件内容如下:

StudentController

package com.bjpowernode.controller;import com.bjpowernode.domain.Student;
import com.bjpowernode.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;import javax.annotation.Resource;
import java.util.List;@Controller
@RequestMapping("/student")
public class StudentController {@Resourceprivate StudentService service;//添加学生@RequestMapping("/addStudent.do")public ModelAndView addStudent(Student student){ModelAndView mv =new ModelAndView();String tips = "注册失败";//调用service处理int nums = service.addStudent(student);if(nums > 0){tips = "学生【"+student.getName()+"】注册成功";}//添加数据mv.addObject("tips", tips);//指定结果页面(逻辑名称)mv.setViewName("result");return mv;}//查询 返回json@RequestMapping("/queryStudent.do")@ResponseBodypublic List<Student> queryStudent(Student student){//省略参数检查以及数据处理List<Student> students = service.findStudents();return students;}
}

StudentDao

package com.bjpowernode.dao;import com.bjpowernode.domain.Student;import java.util.List;public interface StudentDao {int insertStudent(Student student);List<Student> selectStudents();
}

StudentDao.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjpowernode.dao.StudentDao"><select id="selectStudents" resultType="Student">select id,name,age from student order by id desc</select><insert id="insertStudent">insert into student(name,age) values(#{name},#{age})</insert>
</mapper>

Student.java

package com.bjpowernode.domain;public class Student {private Integer id;private String name;private Integer age;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}
}

StudentServiceImpl

package com.bjpowernode.service.impl;import com.bjpowernode.dao.StudentDao;
import com.bjpowernode.domain.Student;
import com.bjpowernode.service.StudentService;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.List;@Service
public class StudentServiceImpl implements StudentService {@Resourceprivate StudentDao studentDao;@Overridepublic int addStudent(Student student) {int nums = studentDao.insertStudent(student);return nums;}@Overridepublic List<Student> findStudents() {return studentDao.selectStudents();}
}

StudentService

package com.bjpowernode.service;import com.bjpowernode.domain.Student;import java.util.List;public interface StudentService {int addStudent(Student student);List<Student> findStudents();
}

9. 写jsp代码

result.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
结果:${tips}
</body>
</html>

addStudent.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%String basePath = request.getScheme()+"://" +request.getServerName() + ":" + request.getServerPort() +request.getContextPath() + "/";
%>
<html>
<head><title>Title</title><base href="<%=basePath%>">
</head>
<body><div align="center"><form action="student/addStudent.do" method="post"><table><tr><td>姓名</td><td><input type="text" name="name"></td></tr><tr><td>年龄</td><td><input type="text" name="age"></td></tr><tr><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td><input type="submit" value="提交"></td></tr></table></form></div>
</body>
</html>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%String basePath = request.getScheme()+"://" +request.getServerName() + ":" + request.getServerPort() +request.getContextPath() + "/";
%>
<html>
<head><title>Title</title><base href="<%=basePath%>"></head>
<body>
<table align="center"><tr><td><a href="addStudent.jsp">注册学生</a></td></tr><tr><td><a href="listStudent.jsp">浏览学生</a></td></tr>
</table>
</body>
</html>

listStudent.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%String basePath = request.getScheme()+"://" +request.getServerName() + ":" + request.getServerPort() +request.getContextPath() + "/";
%>
<html>
<head><title>查询学生 使用ajax</title><base href="<%=basePath%>"><script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script><script type="text/javascript">$(function(){$.ajax({url:"student/queryStudent.do",dataType:"json",success:function(data){$.each(data, function(i,n){$("#info").append("<tr>").append("<td>"+n.id+"</td>").append("<td>"+n.name+"</td>").append("<td>"+n.age+"</td>").append("</tr>")})}})})</script>
</head>
<body><table align="center"><thead><tr><td>学号</td><td>姓名</td><td>年龄</td></tr></thead><tbody id="info"></tbody></table>
</body>
</html>

结束

http://localhost:8080/ch07_war_exploded/

复习下

ssm整合开发 动力节点王鹤版本相关推荐

  1. 整理动力节点王鹤老师ssm整合步骤

    1.SSM整合开发步骤 总体来说就是 SPringMVC接收用户请求-----Spring中的Service对象-----Mybatis处理数据 ssm整合也叫做ssi,整合中有容器 1.第一容器Sp ...

  2. SpringBoot集成Redis笔记-动力节点王鹤

    关于springboot集成redis,我做了笔记,分享给小伙伴们,视频看的动力节点王鹤老师讲的, 动力节点王鹤老师讲解的springboot教程,由浅入深,带你体验Spring Boot的极速开发过 ...

  3. 动力节点王鹤Spring Boot笔记

    多年来,随着新功能的增加,spring变得越来越复杂.只需访问https://spring.io/projects页面,我们就会看到可以在我们的应用程序中使用的所有Spring项目的不同功能. 如果必 ...

  4. Ajax学习笔记-动力节点-王鹤老师

    Ajax学习笔记-动力节点-王鹤老师 视频教程来自:https://www.bilibili.com/video/BV15k4y167XM?spm_id_from=333.999.0.0 第一节 全局 ...

  5. SpringBoot学习笔记总结—动力节点王鹤

    先说说Spring Boot是什么? 多年来,随着新功能的增加,spring变得越来越复杂.只需访问https://spring.io/projects页面,我们就会看到可以在我们的应用程序中使用的所 ...

  6. 阿里开发规范文档_华为阿里等技术专家15年开发经验总结:SSM整合开发实战文档...

    前言 Spring自2002年诞生至今,已有近20年的历史,虽然几经变迁,但始终在继续发展和精进.Spring目前由Pivotal维护和开发. Pivotal是PaaS(平台即服务)的领导者,也是消息 ...

  7. 史上最全SpringBoot学习笔记-动力节点王鹤(2021最新版)

    SpringBoot 资料官方下载地址 动力节点springboot资料 视频观看地址 https://www.bilibili.com/video/BV1XQ4y1m7ex 第一章 JavaConf ...

  8. SSM整合开发学习笔记

    1. 整合配置 1.1 配置项目依赖 <?xml version="1.0" encoding="UTF-8"?><project xmlns ...

  9. SSM 整合开发初见面

    SpringMVC是Spring其中的一个模块,我们直接使用即可: SSM的整合:将Mybatis 和 Spring进行整合: >类的根路径:classpath,也就是classes文件夹 sr ...

最新文章

  1. Windows下Hadoop的环境安装[转]
  2. CSS那些事笔记(一入门)
  3. boost库之tcp server(异步)
  4. mysql库存先进先出_sql 先进先出 库存
  5. 某电商平台开发记要——客服系统
  6. timestamp mysql php_PHP和Mysql的Timestamp互换
  7. ROS | ROS2安装(Ubuntu 16.04版本:通过Debian包安装)
  8. databasemetadata获取表注释_宏基因组测序中短序列的注释
  9. 02-1.CSS边框,边界,布局相关笔记
  10. Spring Boot 动态修改定时任务cron参数
  11. java从控制台读取数据_Java不同版本从控制台读取数据方法及优缺点分析
  12. 计算机操作安全协议,安全协议操作语义模型研究及应用-计算机软件与理论专业论文.docx...
  13. 20175212童皓桢 《Java程序设计》第十周学习总结
  14. UE4(Unreal Engine 4)显示FPS
  15. HJ87 密码强度等级(一把过)
  16. 大学计算机word图文混排,Word 2003从入门到精通第五讲(图文混排)讲稿
  17. change在c语言中的用法,change的过去式和用法例句意思及阅读
  18. dw版权信息栏如何制作_dw网页制作过程?用dw怎么制作网页?
  19. 数据规范化处理方法-Min-max 规范化和 Z-Score 规范化
  20. 4年的数学竞赛学习,终于成就这位牛娃的北大保送之路!

热门文章

  1. 一文快速搞懂Kudu到底是什么
  2. STM32F103中文参考手册(754页)
  3. 总结一波 Redis 面试题
  4. 阿里数据中台OneID核心能力之ID-Mapping
  5. bzoj 3234: [Ahoi2013]立方体
  6. java毕业设计软件源代码]springMVC+mysql实现进销存系统仓库管理系统
  7. StrongShop 开源商城 跨境电商独立站的理想选择
  8. 微信小程序this.setData()
  9. python小项目:MakeCode小游戏——躲砖块
  10. LoadRunner(一)Win10系统下LoadRunner12的安装下载