一.用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>
原文地址:https://www.cnblogs.com/aibabel/p/11728914.html
时间: 2024-10-02 00:49:56