springboot系列四:springboot整合mybatis jsp
一.用IDEA 创建maven项目
项目目录结构

1.添加pom jar依赖
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent> <groupId>com.aibabelx</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency> <!-- 添加servlet依赖模块 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId> </dependency>
<!-- 添加jstl标签库依赖模块 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--添加tomcat依赖模块.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId> </dependency>
<!-- 使用jsp引擎,springboot内置tomcat没有此依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.13</version> </dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
2.创建javabean
package com.aibabelx.entity;
public class Student {
private String xh;
private String xm;
private double fs;
public String getXh() {
return xh;
}
public void setXh(String xh) {
this.xh = xh;
}
public String getXm() {
return xm;
}
public void setXm(String xm) {
this.xm = xm;
}
public double getFs() {
return fs;
}
public void setFs(double fs) {
this.fs = fs;
}
@Override
public String toString() {
return "Student{" +
"xh='" + xh + '\'' +
", xm='" + xm + '\'' +
", fs=" + fs +
'}';
}
}
3.mapper接口
package com.aibabelx.mapper; import com.aibabelx.entity.Student;
import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper
public interface StudentMapper { public Integer getSum(); public List<Student> getMax(); public List<Student> getSax();
}
4.servie接口
package com.aibabelx.service; import com.aibabelx.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service; import java.util.List; @Service
public interface StudentService { public Integer getSum(); public List<Student> getMax(); public List<Student> getSax();
}
5.service的实现类
package com.aibabelx.service.impl; import com.aibabelx.entity.Student;
import com.aibabelx.mapper.StudentMapper;
import com.aibabelx.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import java.util.List;
@Transactional
public class StudentServiceImpl implements StudentService { @Autowired
public StudentMapper studentMapper;
@Override
public Integer getSum() {
return studentMapper.getSum();
} @Override
public List<Student> getMax() {
return studentMapper.getMax();
} @Override
public List<Student> getSax() {
return studentMapper.getSax();
}
}
6.controller
package com.aibabelx.controller; import com.aibabelx.entity.Student;
import com.aibabelx.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import java.util.List; @Controller
public class IndexController { @Autowired
public StudentService studentService ;
@RequestMapping("/index")
public ModelAndView index(){
Integer n=studentService.getSum();
List<Student> max=studentService.getMax();
List<Student> sax=studentService.getSax(); return new ModelAndView("index.jsp")
.addObject("n",n)
.addObject("max",max)
.addObject("sax",sax);
}
}
7.mapper.xml 文件
<?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.aibabelx.mapper.StudentMapper"> <!-- 通用查询映射结果 -->
<resultMap id="ItemBaseResultMap" type="com.aibabelx.entity.Student">
<id column="xh" property="xh" />
<result column="xm" property="xm" />
<result column="fs" property="fs" /> </resultMap> <select id="getSum" resultType="Integer" >
SELECT COUNT(xh)
FROM Student </select>
<select id="getMax" resultType="Student">
SELECT xh,xm,fs from student WHERE fs =(SELECT MAX(fs) from student) </select>
<select id="getSax" resultType="Student">
SELECT xh,xm,fs from student WHERE fs =(SELECT min(fs) from student) </select> </mapper>
8.配置文件
server.port =9999
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8
#jsp config
spring.mvc.view.prefix=/WEB-INF/views/
#spring.mvc.view.suffix: .jsp # DataSource
spring.datasource.url=jdbc:mysql://localhost/aiplay?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql = true mybatis.mapper-locations=classpath*:mapper/*.xml
mybatis.type-aliases-package=com.aibabelx.entity
9.index.jsp 文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> <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">
</head> <body> ${n} <c:forEach items="${max}" var="student" >
${student.xh}<br> ${student.xm}<br> ${student.fs}<br>
</c:forEach> <c:forEach items="${sax}" var="student" >
${student.xh}<br> ${student.xm}<br> ${student.fs}<br>
</c:forEach> </body>
</html>
springboot系列四:springboot整合mybatis jsp的更多相关文章
- springboot学习四:整合mybatis
在application.properties加入配置 ## Mybatis 配置 mybatis.typeAliasesPackage=org.spring.springboot.domain my ...
- SpringBoot系列(五)Mybatis整合完整详细版
SpringBoot系列(五)Mybatis整合 目录 mybatis简介 项目创建 entity dao service serviceImpl mapper controller 1. Mybat ...
- Springboot 2.0.4 整合Mybatis出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
在使用Springboot 2.0.4 整合Mybatis的时候出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are require ...
- SpringBoot数据访问之整合mybatis注解版
SpringBoot数据访问之整合mybatis注解版 mybatis注解版: 贴心链接:Github 在网页下方,找到快速开始文档 上述链接方便读者查找. 通过快速开始文档,搭建环境: 创建数据库: ...
- (入门SpringBoot)SpringBoot项目数据源以及整合mybatis(二)
1.配置tomcat数据源: # 数据源基本配置spring.datasource.url=jdbc:mysql://localhost:3306/shoptest?useUnicode=true ...
- SpringBoot学习- 3、整合MyBatis
SpringBoot学习足迹 1.下载安装一个Mysql数据库及管理工具,同类工具很多,随便找一个都可以,我在windows下做测试项目习惯使用的是haosql 它内部集成了MySql-Front管理 ...
- SpringBoot数据访问之整合Mybatis配置文件
环境搭建以及前置知识回顾 SpringBoot中有两种start的形式: 官方:spring-boot-starter-* 第三方:*-spring-boot-starter Mybatis属于第三方 ...
- Spring Boot2 系列教程 (十三) | 整合 MyBatis (XML 版)
前言 如题,今天介绍 SpringBoot 与 Mybatis 的整合以及 Mybatis 的使用,之前介绍过了 SpringBoot 整合MyBatis 注解版的使用,上一篇介绍过 MyBatis ...
- Java开发学习(十四)----Spring整合Mybatis及Junit
一.Spring整合Mybatis思路分析 1.1 环境准备 步骤1:准备数据库表 Mybatis是来操作数据库表,所以先创建一个数据库及表 create database spring_db cha ...
随机推荐
- codeforces 1076E Vasya and a Tree 【dfs+树状数组】
题目:戳这里 题意:给定有n个点的一棵树,顶点1为根.m次操作,每次都把以v为根,深度dep以内的子树中所有的顶点(包括v本身)加x.求出最后每个点的值为多少. 解题思路:考虑到每次都只对点及其子树操 ...
- 手工数据结构系列-C语言模拟队列和栈 hdu1702
#include <stdio.h> #include <stdlib.h> //================= DATA STRUCTURE ============== ...
- sqli-libs(7) bool盲注
首先打开网页后,发现页面显示outfile, 对不起,看不懂, 就直接加 ' 发现报错了 后来,查看源码得知 他是两个括号括起来的 我们就可以构造我们的payload了, 首先查询当前数据库的长度 ...
- 如何使用 VuePress 搭建一个 element-ui 风格的文档网站
如何使用 VuePress 搭建一个 element-ui 风格的文档网站 { "devDependencies": { "vuepress": "1 ...
- 使用 js 实现一个简易版的 drag & drop 库
使用 js 实现一个简易版的 drag & drop 库 具有挑战性的前端面试题 H5 DnD js refs https://www.infoq.cn/article/0NUjpxGrqRX ...
- free online markdown editor
free online markdown editor markdown https://blog.csdn.net/xgqfrms/article/details/50129317 In-brows ...
- Make one your own Online Video Recorder by using WebRTC & vanilla javascript
Make one your own Online Video Recorder by using WebRTC & vanilla javascript Online Video Record ...
- disable html input & pointer-events
disable html input & pointer-events css https://developer.mozilla.org/en-US/docs/Web/CSS/pointer ...
- input change only trigger once bug
input change only trigger once bug clear first https://stackoverflow.com/a/11280934/5934465 upload E ...
- 什么是NGK节点?NGK的节点有什么作用?
从2018年EOS竞选超级节点火爆整个区块链圈,节点和超级节点就成为了热议的话题,时至今日,热度依然不减.节点是项目生态发展的重要组成部分,超级节点对项目的运行.安全更是起着至关重要的作用. 什么 ...