一.基于springboot的ssm的增删改查。配置文件不用写,要理解。
说明:这里大家把下面 链接中的案例非常熟练,原理都搞明白,基本上就可以了。(35分到手)
(1)SpringBoot Mybatis-注解方式Springboot Mybatis-注解方式https://how2j.cn/k/springboot/springboot-mybatis/1649.html
- application.properties
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp spring.datasource.url=jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=admin spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- 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.how2java</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot</name> <description>springboot</description> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- servlet依赖. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- tomcat的支持.--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- 这个需要为 true 热部署才有效 --> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
- Category
package com.how2java.springboot.pojo; public class Category { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
- CategoryMapper
增加一个包:com.how2java.springboot.mapper,然后创建接口CategoryMapper。 使用注解@Mapper 表示这是一个Mybatis Mapper接口。 使用@Select注解表示调用findAll方法会去执行对应的sql语句。 package com.how2java.springboot.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import com.how2java.springboot.pojo.Category; @Mapper public interface CategoryMapper { @Select("select * from category_ ") List<Category> findAll(); }
- CategoryController
增加一个包:com.how2java.springboot.web,然后创建CategoryController 类。 1. 接受listCategory映射 2. 然后获取所有的分类数据 3. 接着放入Model中 4. 跳转到listCategory.jsp中 package com.how2java.springboot.web; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.how2java.springboot.mapper.CategoryMapper; import com.how2java.springboot.pojo.Category; @Controller public class CategoryController { @Autowired CategoryMapper categoryMapper; @RequestMapping("/listCategory") public String listCategory(Model m) throws Exception { List<Category> cs=categoryMapper.findAll(); m.addAttribute("cs", cs); return "listCategory"; } }
- listCategory.jsp
用jstl遍历从CategoryController 传递过来的集合:cs. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <table align='center' border='1' cellspacing='0'> <tr> <td>id</td> <td>name</td> </tr> <c:forEach items="${cs}" var="c" varStatus="st"> <tr> <td>${c.id}</td> <td>${c.name}</td> </tr> </c:forEach> </table>
(2)基于Springboot Mybatis的CRUD和分页(增删改查和分页) Springboot Mybatishttps://how2j.cn/k/springboot/springboot-mybatis-crud-pagination/1651.html
- pom.xml(增加对PageHelper的支持)
<!-- pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency>
- PageHelperConfig
注解@Configuration 表示PageHelperConfig 这个类是用来做配置的。 注解@Bean 表示启动PageHelper这个拦截器。 新增加一个包 com.how2java.springboot.config, 然后添加一个类PageHelperConfig ,其中进行PageHelper相关配置。 package com.how2java.springboot.config; import java.util.Properties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.github.pagehelper.PageHelper; @Configuration public class PageHelperConfig { @Bean public PageHelper pageHelper() { PageHelper pageHelper = new PageHelper(); Properties p = new Properties(); p.setProperty("offsetAsPageNum", "true"); p.setProperty("rowBoundsWithCount", "true"); p.setProperty("reasonable", "true"); pageHelper.setProperties(p); return pageHelper; } }
- CategoryMapper
修改CategoryMapper,增加CRUD方法的支持。 其实就是调用不同的SQL语句。 package com.how2java.springboot.mapper; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import com.how2java.springboot.pojo.Category; @Mapper public interface CategoryMapper { @Select("select * from category_ ") List<Category> findAll(); @Insert(" insert into category_ ( name ) values (#{name}) ") public int save(Category category); @Delete(" delete from category_ where id= #{id} ") public void delete(int id); @Select("select * from category_ where id= #{id} ") public Category get(int id); @Update("update category_ set name=#{name} where id=#{id} ") public int update(Category category); }
- CategoryController
package com.how2java.springboot.web; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.how2java.springboot.mapper.CategoryMapper; import com.how2java.springboot.pojo.Category; @Controller public class CategoryController { @Autowired CategoryMapper categoryMapper; @RequestMapping("/addCategory") public String listCategory(Category c) throws Exception { categoryMapper.save(c); return "redirect:listCategory"; } @RequestMapping("/deleteCategory") public String deleteCategory(Category c) throws Exception { categoryMapper.delete(c.getId()); return "redirect:listCategory"; } @RequestMapping("/updateCategory") public String updateCategory(Category c) throws Exception { categoryMapper.update(c); return "redirect:listCategory"; } @RequestMapping("/editCategory") public String listCategory(int id,Model m) throws Exception { Category c= categoryMapper.get(id); m.addAttribute("c", c); return "editCategory"; } @RequestMapping("/listCategory") public String listCategory(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception { PageHelper.startPage(start,size,"id desc"); List<Category> cs=categoryMapper.findAll(); PageInfo<Category> page = new PageInfo<>(cs); m.addAttribute("page", page); return "listCategory"; } }
- listCategory.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <div align="center"> </div> <div style="width:500px;margin:20px auto;text-align: center"> <table align='center' border='1' cellspacing='0'> <tr> <td>id</td> <td>name</td> <td>编辑</td> <td>删除</td> </tr> <c:forEach items="${page.list}" var="c" varStatus="st"> <tr> <td>${c.id}</td> <td>${c.name}</td> <td><a href="editCategory?id=${c.id}">编辑</a></td> <td><a href="deleteCategory?id=${c.id}">删除</a></td> </tr> </c:forEach> </table> <br> <div> <a href="?start=1">[首 页]</a> <a href="?start=${page.pageNum-1}">[上一页]</a> <a href="?start=${page.pageNum+1}">[下一页]</a> <a href="?start=${page.pages}">[末 页]</a> </div> <br> <form action="addCategory" method="post"> name: <input name="name"> <br> <button type="submit">提交</button> </form> </div>
- editCategory.jsp(修改分类的页面)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <div style="margin:0px auto; width:500px"> <form action="updateCategory" method="post"> name: <input name="name" value="${c.name}"> <br> <input name="id" type="hidden" value="${c.id}"> <button type="submit">提交</button> </form> </div>
2、maven,spring,mybytis,springmvc,springboot,ssm的基本原理。需要了解程序的运行流程。大家按照how2j网站来复习就行了。
(1)maven,掌握什么是maven,使用maven有什么好处
Mavenhttps://how2j.cn/k/maven/maven-introduction/1328.html
- 什么是Maven? Maven 是专门用于构建和管理Java相关项目的工具。
- Maven 主要用处
相同的项目结构: 使用Maven管理的Java 项目都有着相同的项目结构 1. 有一个pom.xml 用于维护当前项目都用了哪些jar包 2. 所有的java代码都放在 src/main/java 下面 3. 所有的测试代码都放在src/test/java 下面 统一维护jar包: maven风格的项目,首先把所有的jar包都放在"仓库“ 里,然后哪个项目需要用到这个jar包,只需要给出jar包的名称和版本号就行了。 这样jar包就实现了共享 例如:在pom.xml里,表示用到了mysql 的jar包,版本号是xxx。
(2)spring,了解什么是IOC,依赖注入,AOP
IOC/DI
Spring是一个基于IOC和AOP的结构J2EE系统的框架
IOC 反转控制 是Spring的基础,Inversion Of Control
简单说就是创建对象由以前的程序员自己new 构造方法来调用,变成了交由Spring创建对象
DI 依赖注入 Dependency Inject. 简单地说就是拿到的对象的属性,已经被注入好相关值了,直接使用即可。
(1)准备pojo Category,用来演示IOC和DI
(2)applicationContext.xml:在src目录下新建applicationContext.xml文件
applicationContext.xml是Spring的核心配置文件,通过关键字c即可获取Category对象,该对象获取的时候,即被注入了字符串"category 1“到name属性中
(3)TestSpring:测试代码,演示通过spring获取Category对象,以及该对象被注入的name属性。
如图所示,可以打印出通过Spring拿到的Category对象的name属性
package com.how2java.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.how2java.pojo.Category;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
Category c = (Category) context.getBean("c");
System.out.println(c.getName());
}
}
AOP
AOP 即 Aspect Oriented Program 面向切面编程
首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。
所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务
所谓的周边功能,比如性能统计,日志,事务管理等等
周边功能在Spring的面向切面编程AOP思想里,即被定义为切面
在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发
然后把切面功能和核心业务功能 "编织" 在一起,这就叫AOP
(3)mybatis,是什么框架,运行原理
平时我们都用JDBC访问数据库,除了需要自己写SQL之外,还必须操作Connection, Statement, ResultSet 这些其实只是手段的辅助类。 不仅如此,访问不同的表,还会写很多雷同的代码,显得繁琐和枯燥。
那么用了Mybatis之后,只需要自己提供SQL语句,其他的工作,诸如建立连接,Statement, JDBC相关异常处理等等都交给Mybatis去做了,那些重复性的工作Mybatis也给做掉了,我们只需要关注在增删改查等操作层面上,而把技术细节都封装在了我们看不见的地方。
- 准备实体类Category,用于映射表category_
- 配置文件mybatis-config.xml
- 配置文件Category.xml
- 测试类TestMybatis
(4)springmvc,运行原理图
- 配置web.xml
- 创建springmvc-servlet.xml
- 控制类 IndexController
- 准备index.jsp
(5)springboot,是什么运行原理,如何启动,有什么优点
(6)ssm,运行原理图