参考URL:SpringBoot 整合视图View jsp/freemarker/leaf : https://cloud.tencent.com/developer/article/1629511
1.在eclipse的help,eclipse marketplace中搜索 spring ,安装第一个spring tool 4(aka...),然后新建项目就会出现spring boot项目:
2.POM内容: 换成aliyun的仓库。
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.study</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </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> <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <!-- jsp jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- jasper --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!-- jsp jstl end --> </dependencies> <repositories> <repository> <id>aliyun-repos</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>aliyun-plugin</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>3. application.properies
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://2222222222:3306/javadb?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true spring.datasource.username=root spring.datasource.password=eeeee mybatis.type-aliases-package=com.study.demo.model mybatis.mapper-locations=classpath*:mapper/*.xml #jsp方式模板 spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp #freemarker方式模板,默认不能兼用。 #spring.freemarker.suffix=.ftl4.UserMapper.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.study.demo.mapper.UserMapper"> <select id="getAll" resultType="User"> select * from t_user </select> </mapper>5.application.java
package com.study.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.study.demo.mapper") public class SpringbootApplication { public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); System.out.println("hello spring boot."); } }6.UserController.java
package com.study.demo.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.study.demo.model.User; import com.study.demo.service.UserService; @Controller @RequestMapping("/user") public class UserController { @Autowired UserService userService; @ResponseBody @RequestMapping("/listdata") public List<User> getAllData(){ return userService.getAll(); } @RequestMapping("/list") public String listPage(Model model) { List<User> userList = userService.getAll(); model.addAttribute("list",userList); return "userlist"; } @RequestMapping("/listmarker") public String listMarker(Model model) { List<User> userList = userService.getAll(); model.addAttribute("list",userList); return "user"; } }7.UserService.java
package com.study.demo.service; import java.util.List; import com.study.demo.model.User; public interface UserService { List<User> getAll(); }8.UserServiceImpl.java
package com.study.demo.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.study.demo.mapper.UserMapper; import com.study.demo.model.User; import com.study.demo.service.UserService; @Service("userService") public class UserServiceImpl implements UserService { @Autowired UserMapper userMapper; @Override public List<User> getAll() { return userMapper.getAll(); } }9.User.java
package com.study.demo.model; import java.io.Serializable; public class User implements Serializable{ int userid; String username; String password; String nickname; public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } }10.UserMapper.java
package com.study.demo.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.study.demo.model.User; @Mapper public interface UserMapper { List<User> getAll(); }11.建jsp页面
12.发布到服务器项目:freemarker原生支持。
1:POM.xml
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.xxxx.bootedu.BooteduApplication</mainClass> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/webapp</directory> <targetPath>META-INF/resources</targetPath> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build>2项目上 run as ... maven clean maven install ,生成 jar文件。
3拷贝 jar文件到 tomcat 的 webapp 下,
4运行: java -jar xxx.jar
5访问即可。
13.排队项目X错误: https://blog.csdn.net/u012373281/article/details/89597853
14. ALT键 + / 在pom.xml中可以调出选择界面。
15. 上传文件时将上传目录放到项目外的方法:application.properties 及 @Configuration 继承自:WebMvcConfigurer
https://blog.csdn.net/u011144425/article/details/79225864