Erlo

Eclipse完成Maven + Spring Boot + Mybatis + jsp

2019-03-22 16:01:46 发布   556 浏览  
页面报错/反馈
收藏 点赞

Spring Boot 完成WEB项目开发

开发工具:eclipse

框架:Maven;Spring Boot;Mybatis

界面:jsp;javascript;css

 

前言:

  在SpringBoot的学习中,遇到很多问题,也在网上找过许多文档,但大多数都是使用IDE开发工具完成的,所以在此记录使用Eclipse开发工具完成的SpringBootWeb项目。

  感谢在学习中看到过的文章的作者们,让我学习少走许多弯路。

一、  Eclipse关联Maven

  1. 首先下载Maven,将Maven放置在除开C盘的任意盘中。
  2. 修改Maven的settings.xml文件,该文件的目录在*/Maven/conf
  3. 在settings.xml中配置本地仓库。如图:

 

    apache-maven-3.2.1是我的Maven文件夹,解压后懒得改了。。。

    maven_repository就是我的本地仓库,以后从maven新下载的包都会放到这个本地仓库中。

    settings.xml已经有loaclRepository标签,不过被注释了,只需要找到修改完并打开注释即可

    4. 修改settings.xml中配置阿里云链接。如图:

 

    同样,找到mirrors标签,按图中配置修改即可。这个配置是为了提高查找源码或下载包时的速度,原配置中是连接国外的服务器,相对而言十分慢。

    5. 在eclipse中加入Maven的关联

    在window-preferences-maven中添加Maven的关联

 

    修改settings.xml的关联。查看本地仓库是否正确。

 

    以上5步就已经将eclipse和maven关联起来了。

二、  使用Maven创建Spring Boot Web 项目

  1. 创建Maven项目

    1.1、    点击File – New – Other,选中Maven

 

 

    1.2、    然后点击Next,进入Maven Project,默认选项,点击Next

    1.3、    选中web项目,点击Next如图:

 

 

    1.4、    填写项目基本数据

 

 

      这里的工作空间已经拥有了MyMavenSpringBootWeb项目,所以出现报错且不能创建。

    2.构建SpringBoot的目录结构

    创建出来的SpringBoot目录可能会有所缺失,所以我们将其补全

 

    在main包下面添加resources和webapp包,分别用于放置配置文件和web界面

    3. 修改pom.xml,引入SpringBoot的相关配置

<?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>cn.com</groupId>

  <artifactId>MyMavenSpringBootWeb</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>war</packaging>

 

  <name>MyMavenSpringBootWeb Maven Webapp</name>

  <#gth# FIXME change it to the project's website -->

  <url>http://www.example.com</url>

 

  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <maven.compiler.source>1.7</maven.compiler.source>

    <maven.compiler.target>1.7</maven.compiler.target>

  </properties>

 

 

  <#gth# Spring Boot父依赖 -->

  <parent>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-starter-parent</artifactId>

   <version>1.5.9.RELEASE</version>

  </parent>

 

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>4.11</version>

      <scope>test</scope>

    </dependency>

   

   <#gth# Spring Boot web依赖 -->

   <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-web</artifactId>

   </dependency>

 

   <#gth# Spring Boot 热部署 -->

   <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-devtools</artifactId>

   </dependency>

 

    <#gth# 引入thymelaf 则不需要引入web依赖,若不需要thymelaf则需要添加spring-boot-starter-web -->

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-thymeleaf</artifactId>

    </dependency>

     

    <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-test</artifactId>

    </dependency>

   

    <#gth# mybatis依赖 -->

    <dependency>

        <groupId>org.mybatis.spring.boot</groupId>

        <artifactId>mybatis-spring-boot-starter</artifactId>

        <version>1.3.1</version>

    </dependency>

 

   <#gth# tomcat的支持. -->

   <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-tomcat</artifactId>

      <scope>provided</scope>

   </dependency>

 

   <#gth# 访问JSP页面必须配置 -->

   <dependency>

      <groupId>org.apache.tomcat.embed</groupId>

      <artifactId>tomcat-embed-jasper</artifactId>

      <scope>provided</scope>

   </dependency>

  

   <#gth# jspservlet容器的支持 -->

   <dependency>

      <groupId>javax.servlet</groupId>

      <artifactId>javax.servlet-api</artifactId>

   </dependency>

  

   <#gth# 解析jsp页面,跳转所需配置 -->

   <dependency>

      <groupId>javax.servlet</groupId>

      <artifactId>jstl</artifactId>

   </dependency>

  </dependencies>

 

 

  <build>

    <finalName>MyMavenSpringBootWeb</finalName>

<pluginManagement>

<#gth# lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->

      <plugins>

      <#gth#springframework插件配置 -->

      <plugin>

         <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-maven-plugin </artifactId>

      </plugin>

     

        <plugin>

          <artifactId>maven-clean-plugin</artifactId>

          <version>3.1.0</version>

        </plugin>

        <#gth# see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->

        <plugin>

          <artifactId>maven-resources-plugin</artifactId>

          <version>3.0.2</version>

        </plugin>

        <plugin>

          <artifactId>maven-compiler-plugin</artifactId>

          <version>3.8.0</version>

        </plugin>

        <plugin>

          <artifactId>maven-surefire-plugin</artifactId>

          <version>2.22.1</version>

        </plugin>

        <plugin>

          <artifactId>maven-war-plugin</artifactId>

          <version>3.2.2</version>

        </plugin>

        <plugin>

          <artifactId>maven-install-plugin</artifactId>

          <version>2.5.2</version>

        </plugin>

        <plugin>

          <artifactId>maven-deploy-plugin</artifactId>

          <version>2.8.2</version>

        </plugin>

      </plugins>

    </pluginManagement>

  </build>

</project>

 

 

    Pom配置文件配置好后,选中项目右键-Maven-Update Project下载jar包

    想要正确的访问到jsp界面,我们需要将项目的打包方式改war,即<packaging>标签

    4. 配置properties文件

#数据库配置

spring.datasource.url=jdbc:mysql://localhost:3306/my_local_sql

spring.datasource.username=root

spring.datasource.password=root

#spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.datasource.max-active=20

spring.datasource.max-idle=8

spring.datasource.max-maxWait=100

spring.datasource.min-idle=8

spring.datasource.initial-size=10

spring.session.store-type=none

#mybatis.mapper-locations=classpath:/mybatis/*Mapper.xml

 

#jsp访问路径

spring.mvc.view.suffix=.jsp

spring.mvc.view.prefix=/WEB-INF/jsp/

 

#关闭默认模板引擎

spring.thymeleaf.cache=false

spring.thymeleaf.enabled=false

 

 

三、  编写代码

  1. 编写启动类

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.boot.web.support.SpringBootServletInitializer;

 

// 该注解的作用是:排除自动注入数据源的配置(取消数据库配置),一般使用在客户端(消费者)服务中

// 消费者是微服务的概念;消费者指的是客服端,服务者指的是服务端

//@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

// 当作为服务端时,需要去掉排除,我们使用的myBatis需要注入数据源的配置,而数据源的配置就是该工程的application.properties

@SpringBootApplication

@MapperScan(basePackages = {"cn.com.service","cn.com.mapper"})

public class MyFirstApplication extends SpringBootServletInitializer{

   public static void main(String[] args) throws Exception {

      SpringApplication.run(MyFirstApplication.class, args);

   }

  

    @Override

    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {

        // 注意这里要指向原先用main方法执行的Application启动类

        return builder.sources(MyFirstApplication.class);

    }

}

    启动类命名均以*Application格式,文件同时放在根目录中,如图:


    到这一步,Spring Boot Wed项目基本上已经配置好了。然后加入service、访问数据看的mapper、控制界面的controller和实体类的entity

    2. 编写Mapper接口

import java.util.List;

 

import org.springframework.stereotype.Repository;

 

import cn.com.entity.MyFirstEntity;

 

@Repository

public interface MyFirstMapper {

 

   public List<MyFirstEntity> crateEntity() ;

}

    这里的Mapper需要用@Repository标签来声明

   3. 编写serivce接口和实现类

import java.util.List;

 

import cn.com.entity.MyFirstEntity;

 

public interface MyFirstService {

 

   public List<MyFirstEntity> crateEntity() ;

}

 

import java.util.ArrayList;

import java.util.List;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

 

import cn.com.entity.MyFirstEntity;

import cn.com.mapper.MyFirstMapper;

import cn.com.service.MyFirstService;

 

@Service("firstService1")

public class MyFirstServiceImpl implements MyFirstService {

     

      @Autowired

      private MyFirstMapper myFirstMapper ;

 

      @Override

      public List<MyFirstEntity> crateEntity() {

//         List<MyFirstEntity> list = myFirstDao.crateEntity() ;

           MyFirstEntity firstEntity = new MyFirstEntity() ;

           firstEntity.setId("1") ;

           firstEntity.setName("One") ;

          

           MyFirstEntity secondEntity = new MyFirstEntity() ;

           secondEntity.setId("2") ;

           secondEntity.setName("Second") ;

          

           List<MyFirstEntity> list = new ArrayList<MyFirstEntity>() ;

           list.add(firstEntity) ;

           list.add(secondEntity) ;

          

           return list ;

      }

}

    @Service声明service类,firstService1是给该类的一个别名。

    @Autowired为自动装载的标签,装载类的名字默认为被装载类型的首字母小写,如文中的myFirstMapper

    4. Entity实体类的编写

public class MyFirstEntity {

  

   private String id;

   private String name;

  

  

   public String getId() {

      return id;

   }

   public void setId(String id) {

      this.id = id;

   }

   public String getName() {

      return name;

   }

   public void setName(String name) {

      this.name = name;

   }

}

 

    5. 编写Controller

import java.util.List;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

 

import cn.com.entity.MyFirstEntity;

import cn.com.service.MyFirstService;

 

@Controller

public class MyFirstController {

  

   @Autowired

   private MyFirstService firstService1 ;

  

   @RequestMapping("/hello")

   public String start(){

     

      List<MyFirstEntity> list = firstService1.crateEntity() ;

      int size = list.size() ;

     

      System.out.println("后台测试成功!" + size);

     

        return "hello";

   }

}

    @Controller  Spring默认返回view对象,在使用html界面时完全没有问题

    当界面换成使用jsp时,需要做些配置,首先是pom.xml的配置。

    加入spring-boot-starter-web依赖,SpringBoot有默认的模板引擎,若使用jsp需要禁用默认的模板引擎,所以需要该依赖;

    加入spring-boot-starter-tomcat依赖,本次案例是基于外部tomcat完成,

    所以加入tomcat依赖,注意这里的依赖需要使用<scope>provided</scope>标签;

    加入tomcat-embed-jasper依赖,访问jsp不需要的依赖;

    加入jstl依赖,解析jsp页面,跳转所需配置;

    在application.properties配置文件中加入spring.thymeleaf.cache=false和spring.thymeleaf.enabled=false

    起到禁用默认模板引擎的作用;

    如果controller需要分会实体,如Json则使用@ResponseBoby标签,才能使页面具体展示,否则会

    报 Error resolving template "hello", template might not exist or might not be accessible by any of the configured Template Resolvers的错误

    @RestContreller标签内含了@Contreller和@ResponseBoby标签。所以一般使用该标签来代替其余两标签

    @RequestMapping能让程序在url中找到对应的controller,而该标签还能在文件名中进行定义。

    如我在MyFirstController上加@RequestMapping(“/test”),那么访问路径就是localhost:8080/MyMavenSpringBootWeb/test/hello。

    6. 编写jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

   <head>

      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

      <title>Insert title here</title>

   </head>

   <body>

      Hello Spring Boot Project

   </body>

</html>

    jsp默认放在webapp-WEB-INF文件夹下,这里编写jsp时需要注意下application.preperties中的配置,如此次的项目jsp文件在/WEB-INF/jsp/的文件夹下。

四、  运行

    接下来的运行就如普通的web开发一样,将项目加入到tomcat运行

 

 

 

 

    出现上图就代表启动成功了,典型的SpringBoot启动啊。

    接下来就是直接进行访问了。上图

 

 

五、  后语

    翻阅了许多资料,发现不少博客里都有不需要项目名就能访问jsp界面的。但这些例子大多都是用IDE开的工具完成。

    IDE和Eclipse还是有很多不同,这里说说个人是怎么完成不需要项目名直接访问界面的。

    首先Eclipse项目部署tomcat后,会在原项目文件下生成一个Server文件夹。打开该文件夹可以看到server.xml。

    我的方法就是直接修改这个文件,找到Context标签,将该标签里的属性path值清空即可。

 

    至于怎么将端口去掉也能完成访问就需要后续研究了。。。

登录查看全部

参与评论

评论留言

还没有评论留言,赶紧来抢楼吧~~

手机查看

返回顶部

给这篇文章打个标签吧~

棒极了 糟糕透顶 好文章 PHP JAVA JS 小程序 Python SEO MySql 确认