为了整合这个花了,好长时间,因为上几个连在一起,在pom文件中有没注释的配置,导致我找这个问题找了好久。下面开始
1,先new project 时选择spring initializr,新建好一个空项目后,先在pom文件中配置springmvc+mybatis+mysql(有注解的是我新加的,没注解的是项目原有的)
<?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.oda</groupId> <artifactId>e-commerce</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>e-commerce</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.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-test</artifactId> <scope>test</scope> </dependency> <!-- 内嵌Tomcat使用 jsp 必要引入 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- Mybatis引入 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!-- Mysql引入 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- jstl标签引入 --> <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>
2.配置aplication.properties文件
server.port=8080 #SpringMvc配置视图 spring.mvc.view.prefix=/WEB-INF/views/ spring.mvc.view.suffix=.jsp spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=root mybatis.mapper-locations=classpath:sql/*.xml
3新建.UserController
package com.oda.controller; import com.oda.service.UserService; import org.apache.catalina.LifecycleState; import org.apache.catalina.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import java.util.List; @Controller public class UserController { @Resource private UserService userService; @RequestMapping("/welcome") public String index(Model model){ List<User> users = userService.selectUsers(); model.addAttribute("users",users); return "welcome"; } }
4.新建UserService
package com.oda.service; import com.oda.dao.UserDao; import org.apache.catalina.User; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service public class UserService { @Resource private UserDao userDao; public List<User> selectUsers() { return userDao.selectUsers(); } }
5.新建UserDao
package com.oda.dao; import org.apache.catalina.User; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface UserDao { List<User> selectUsers(); }
6.新建User
package com.oda.model; import org.springframework.stereotype.Component; @Component public class User { 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 + '}'; } }
7.新建use.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.oda.dao.UserDao"> <select id="selectUsers" resultType="com.oda.model.User"> select * from user </select> </mapper>
8.新建welcome.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <head> <title></title> </head> <body> <h1>This is a springmvc ${message}</h1> <table border="1px"> <tr> <td>序号</td> <td>姓名</td> <td>年齡</td> </tr> <c:forEach items="${users}" var="u"> <tr> <td>${u.id}</td> <td>${u.name}</td> <td>${u.age}</td> </tr> </c:forEach> </table> </body> </html>
注意要改一下自己的Application类
package com.oda; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.oda.dao") public class ECommerceApplication { public static void main(String[] args) { SpringApplication.run(ECommerceApplication.class, args); } }
这就建好了,启动,访问http://localhost:8080/welcome
结果:
项目结构: