开发工具:IDEA
项目结构:
第一步:pom文件
<?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> <groupId>com.zm</groupId> <artifactId>springmvc</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>springmvc</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <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-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </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.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
第二步:application.properties文件
server.port=8080 server.context-path=/zm/ spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=root spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true #指定mybatis基础配置文件和实体类映射文件的地址 #mybatis.config-location=classpath:mybatis/mybatis-config.xml #mybatis.mapper-locations=classpath:mybatis/mapper/*.xml #设置页面跳转 spring.mvc.view.prefix=/jsp/ spring.mvc.view.suffix=.jsp
第三步:SpringmvcAplication.java
package com.zm.springmvc; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.zm.springmvc.dao") public class SpringmvcApplication { public static void main(String[] args) { SpringApplication.run(SpringmvcApplication.class, args); } }
第四步:UserController.java
package com.zm.springmvc.controller; import com.zm.springmvc.bean.User; import com.zm.springmvc.service.UserService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.List; @Controller public class UserController { @Resource private UserService userService; @GetMapping("/") @ResponseBody public String index() { return "Hello world!"; } @GetMapping("/index") public String one() { return "welcome"; } @RequestMapping("/mvc") public String welcome(Model model) { List<User> users = userService.getList(); model.addAttribute("users", users); System.out.println("user"+users); return "welcome"; } }
第五步:Userservice.java
package com.zm.springmvc.service; import com.zm.springmvc.bean.User; import com.zm.springmvc.dao.UserMapper; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service public class UserService { @Resource private UserMapper userMapper; public List<User> getList() { return userMapper.selectUser(); } }
第六步:UserMapper.java
package com.zm.springmvc.dao; import com.zm.springmvc.bean.User; import org.apache.ibatis.annotations.Select; import java.util.List; public interface UserMapper { @Select("select * from user") List<User> selectUser(); }
第七步:User.java
package com.zm.springmvc.bean; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue private int id; private String name; private int age; public User() { } public User(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
第八步:welcom.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <head> <title>jsp</title> </head> <body> <h3>welcome to jsp page....</h3> <table border="1px"> <tr> <td>序号</td> <td>姓名</td> <td>年齡</td> </tr> <c:forEach items="${users}" var="user"> <tr> <td>${user.id}</td> <td>${user.name}</td> <td>${user.age}</td> </tr> </c:forEach> </table> </body> </html>
然后启动,没有报错,访问页面时候访问不到
地址:http://localhost:8080/zm/mvc
他是找不到error这个视图,应该是在访问http://localhost:8080/zm/mvc的时候报错了,然后调整到error的时候才提示的404,你可以先看下是不是在请求的时候报异常了
谢谢你的评论,嗯嗯,后台有提示,刚自学这个看不太懂
@七月妖妖无: 解决了嘛
#设置页面跳转 spring.mvc.view.prefix=/jsp/ spring.mvc.view.suffix=.jsp
把这儿改一下,因为你的jsp是在webinf下面的
谢谢你的评论,我试过,但是不是这个的问题
spring.mvc.view.prefix=/jsp/ 应该是 spring.mvc.view.prefix=/WEB-INF/jsp/
谢谢你的评论,我试过,但是不是这个的问题
终于找到问题了,因为application.properties文件没有注解jpa的配置
– 七月妖妖无 6年前