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 ...
随机推荐
- JavaScript中的对象引用和复制
在JavaScript分为两种原始值和引用值类型,原始值之间的复制是值对值得复制,而引用类型则是引用对引用的复制: // 原始值的复制: let num1 = 1; let num2 = num1; ...
- MathJax TeX & LaTeX
MathJax TeX & LaTeX mathcal https://leetcode-cn.com/problems/binary-search/solution/er-fen-cha-z ...
- PyCharm 中文 字符 python 报错 的 完美 解决方案!
PyCharm 中文 字符 python 报错 的 完美 解决方案! #_*_ coding:utf-8_*_ https://www.python.org/dev/peps/pep-0263/ 到p ...
- 使用 js 实现一个简易版的 async 库
使用 js 实现一个简易版的 async 库 具有挑战性的前端面试题 series & parallel 串行,并行 refs https://www.infoq.cn/article/0NU ...
- Flutter Navigator2.0
Example 1 import 'package:dart_printf/dart_printf.dart'; import 'package:flutter/material.dart'; cla ...
- USDN代币多少钱?USDN有什么用?
加密货币走向主流人群的采用有很多障碍,比如监管.交易所黑客事件等,但最明显的障碍还是它们极端的价格波动.这从加密货币的整个历史长度来看都是如此.一个货币要正常运转,比如成为有效的交换媒介.记账单位以及 ...
- 伦尼斯酒庄(Chateau Renice)再次赞助亚洲50大餐厅赛事
连续几年来,伦尼斯酒庄(Chateau Renice)一直是亚洲50大最佳餐厅评选赛(Asia's 50 Best Restaurant Awards)的赞助商.2020年伦尼斯酒庄酒庄(Chatea ...
- Union international inc引进微信线下支付,开启消费无现金时代
长期以来,Union international inc娱乐集团(公司编号:20151533091)因其客户来自全球各国,特别是除了美国之外的中国用户居多,因此公司一直和中国领先的社交软件微信保持着良 ...
- 翻译:《实用的Python编程》01_07_Functions
目录 | 上一节 (1.6 文件) | 下一节 (2.0 处理数据) 1.7 函数 随着程序开始变大,我们会想要有条理地组织这些程序.本节简要介绍函数.库模块以及带有异常的错误处理. 自定义函数 对你 ...
- 都学Python了,C++难道真的用不着了吗?
本文首发 | 公众号:lunvey 人人都在学Python,我还学C++吗? 现在只要提及编程语言,得到的答复都是:学Python,有未来! 大家可能有一个误区,数据分析带火了Python,让人们 ...