SpringBoot整合SSM项目+jsp
1.新建SpringBoot项目
01.jpg
02.jpg
2.pom.xml文件中添加依赖
<!--SpringBoot整合mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.15</version>
</dependency>
<!--SpringBoot整合jsp-->
<!--添加servlet依赖模块-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- 添加jstl标签库依赖模块 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--添加tomcat依赖模块.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- 使⽤jsp引擎,springboot内置tomcat没有此依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--
自动生成当前项⽬的⽗项⽬为spring-boot-starter-parent
标记当前项⽬为⼀个springboot的项⽬
版本为2.2.6
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ym</groupId>
<artifactId>spring_boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring_boot</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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--SpringBoot整合mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.15</version>
</dependency>
<!--SpringBoot整合jsp-->
<!--添加servlet依赖模块-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- 添加jstl标签库依赖模块 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--添加tomcat依赖模块.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- 使⽤jsp引擎,springboot内置tomcat没有此依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.在resource包下添加application.yml文件
server:
port: 8080
# 指定端口
spring:
# 配置数据源
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3307/datatest?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
# 给返回值加上前缀和后缀
mvc:
view:
prefix: /WEB-INF/page/
suffix: .jsp
# 配置mybatis
mybatis:
type-aliases-package: com.ym.pojo
mapper-locations: classpath*:mapper/*Mapper.xml
4.pojo层User
package com.ym.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int uid;
private String name;
private String sex;
private int age;
private String address;
}
5.dao层接口
package com.ym.dao;
import com.ym.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper //接口上有注解
public interface IUserDao {
List<User> getAllUsers();
}
6.resource包下新建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属性 -->
<mapper namespace="com.ym.dao.IUserDao">
<sql id="all">
select * from users
</sql>
<select id="getAllUsers" resultType="user">
<include refid="all" />
</select>
</mapper>
7.service层接口IUserService.java
package com.ym.service;
import com.ym.pojo.User;
import java.util.List;
public interface IUserService {
List<User> getAllUsers();
}
8.service层接口的实现类UserService.java
package com.ym.service.impl;
import com.ym.dao.IUserDao;
import com.ym.pojo.User;
import com.ym.service.IUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserService implements IUserService {
@Resource
private IUserDao userDao;
@Override
public List<User> getAllUsers() {
return userDao.getAllUsers();
}
}
9.controller层的项目的启动主页indexController.java
- 项目启动后,页面直接显示hello , spring boot
package com.ym.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@GetMapping("/")
public String index(){
return "hello , spring boot";
}
}
10.controller层的UserController.java
package com.ym.controller;
import com.ym.pojo.User;
import com.ym.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@Controller
public class UserController {
@Resource
private IUserService userService;
@GetMapping("/getAllUser")
@ResponseBody
public List<User> getAllUser(){
List<User> users = userService.getAllUsers();
return users;
}
@GetMapping("/allUser")
public String queryAllUser(Model model){
List<User> users = userService.getAllUsers();
model.addAttribute("users",users);
return "user";
}
}
11.新建jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>用户</title>
</head>
<body>
<h1>用户列表页</h1>
<table width="80%" align="center" border="1">
<tr>
<th>编号</th>
<th>名称</th>
<th>性别</th>
<th>年龄</th>
<th>地址</th>
</tr>
<c:forEach items="${users}" var="u">
<tr>
<td>${u.uid}</td>
<td>${u.name}</td>
<td>${u.sex}</td>
<td>${u.age}</td>
<td>${u.address}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
12.目录结构
目录位置.jpg
网友评论