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 ...
随机推荐
- Leetcode(5)-最长回文子串(包含动态规划以及Manacher算法)
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &quo ...
- oslab oranges 一个操作系统的实现 final
见 github https://github.com/TouwaErioH/subjects/tree/master/oslab-oranges
- Unknown command '\b'. 关于Mysql导入外部数据库脚本报错的解决
来自网络转载 还是字符集的问题 使用source导入外部sql文件: mysql> source F:\php\bookorama.sql;--------------source F:---- ...
- SPOJ - LCS2 Longest Common Substring II(后缀自动机)题解
题意: 求\(n\)个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,然后枚举所有串.对于每个串,求出这个串在\(i\)节点的最大匹配为\(temp[i]\)(当前串在这个节点最多取多少), ...
- Debian8.1 安装samba与windows共享文件,在系统重启后samba服务无法自动启动
Debian8.1安装配置完成并成功与window共享文件后,系统重启后再次访问时出现如下问题 (图)的解决方法 手动重启samba sudo /etc/init.d/samba start 从win ...
- Ubuntu 18.04 + pip3 install virtualenvwrapper 找不到virtualenvwrapper.sh
Reference Ubuntu 18.04 只自带python3.6.5, 因此不想装python2了, 但通过apt install 装virtualenvwrapper时发现必须得装python ...
- reCAPTCHA OCR 详解 , 验验证, OCR(光学自动识别)
WEB安全专题 reCAPTCHA的诞生及意义 CMU(卡耐基梅隆大学)设计了一个名叫reCAPTCHA的强大系统,让电脑去向人类求助.具体做法是:将OCR(光学自动识别)软件无法识别的文字扫 ...
- js 深入原理讲解系列-currying function
js 深入原理讲解系列-currying function 能看懂这一题你就掌握了 js 科里函数的核心原理 不要专业的术语,说人话,讲明白! Q: 实现 sum 函数使得以下表达式的值正确 cons ...
- The Weekly Web Dev Challenge: Emoji Ratings
The Weekly Web Dev Challenge: Emoji Ratings /* DESCRIPTION: You job is to enable users to give a rat ...
- how to create a style element in js (many ways)
how to create a style element in js (many ways) create style in js Constructed StyleSheets CSSStyleS ...