目录

  • 1、 数据源配置
  • 1.1、%CATALINA_HOME%/conf/server.xml 配置全局资源
  • 1.2、%CATALINA_HOME%/conf/server.xml 配置具体工程
  • 2、构建Spring Boot工程
  • 2.1、pom.xml配置
  • 2.2、 src/application.yml系统参数配置
  • 2.3、src/mybatis-config.xml MyBatis相关配置
  • 2.4、启动类
  • 2.4.1、启动类
  • 2.4.2、项目打成war包,需要继承org.springframework.boot.web.servlet.support.SpringBootServletInitializer
  • 2.5、实体类
  • 2.6、Mapper类
  • 2.7、Service
  • 2.7.1、接口
  • 2.7.2、实现类
  • 3、项目目录结构

1、 数据源配置

1.1、%CATALINA_HOME%/conf/server.xml 配置全局资源

server.xml文件找到GlobalNamingResources节点,加入如下代码

<GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>

    <Resource auth="Container"
      driverClassName="com.mysql.cj.jdbc.Driver"
      maxIdle="10"
      maxTotal="10"
      maxWaitMillis="10000"
      url="jdbc:mysql://127.0.0.1:3306/vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8"
      username="root"
      password="123456"
      type="javax.sql.DataSource"
      name="sharedGlobalDataSource" />
  </GlobalNamingResources>

1.2、%CATALINA_HOME%/conf/server.xml 配置具体工程

server.xml文件找到Host节点,加入如下代码

<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".txt"/>

        <Context docBase="spring-boot-jndi" path="/spring-boot-jndi" reloadable="true" source="org.eclipse.jst.jee.server:spring-boot-jndi">
          <ResourceLink name="jdbc/DefaultDS" global="sharedGlobalDataSource" type="javax.sql.DataSource" />
        </Context>
      </Host>

2、构建Spring Boot工程

2.1、pom.xml配置
<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.7.RELEASE</version>
    <relativePath />
  </parent>
  <groupId>com.cnblogs.javalouvre</groupId>
  <artifactId>spring-boot-jndi</artifactId>
  <version>0.0.1</version>

  <dependencies>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <exclusions>
      	<!-- 如果使用外部Tomcat,则排除内置式Tomcat -->
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.4.1</version>
      <exclusions>
      	<!-- 使用JNDI,则使用默认使用DBCP连接池,故排除Hikari连接池 -->
        <exclusion>
          <groupId>com.zaxxer</groupId>
          <artifactId>HikariCP</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

  <!-- 放入Tomcat容器中,打成war包 -->
  <packaging>war</packaging>
</project>

2.2、 src/application.yml系统参数配置

spring:
  datasource:
    jndi-name: java:comp/env/jdbc/DefaultDS

mybatis-plus:
  config-location: classpath:mybatis-config.xml
  mapper-locations: classpath:mapper/**/*.xml
  global-config:
    banner: false

2.3、src/mybatis-config.xml MyBatis相关配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <settings>
    <!-- 全局映射器启用缓存 -->
    <setting name="cacheEnabled" value="true" />
    <!-- 允许 JDBC 支持自动生成主键 -->
    <setting name="useGeneratedKeys" value="true" />
    <!-- 配置默认的执行器 -->
    <setting name="defaultExecutorType" value="REUSE" />
    <!-- 指定 MyBatis 所用日志的具体实现 -->
    <setting name="logImpl" value="SLF4J" />
  </settings>

  <typeAliases>
    <!-- 配置类型别名包 -->
    <package name="com.cnblogs.javalouvre.entity" />
  </typeAliases>
</configuration>

2.4、启动类

2.4.1、启动类
package com.cnblogs.javalouvre;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}
2.4.2、项目打成war包,需要继承org.springframework.boot.web.servlet.support.SpringBootServletInitializer
package com.cnblogs.javalouvre;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(App.class);
    }

}

2.5、实体类

package com.cnblogs.javalouvre.entity;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

@TableName(value = "t_knowledge")
public class Knowledge {

    @TableId(type = IdType.AUTO)
    private Integer id;
    private String  name;
    private String  memo;
    private String  video;

    public Knowledge() {
    }

    public Knowledge(String name, String memo, String video) {
        this.name = name;
        this.memo = memo;
        this.video = video;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMemo() {
        return memo;
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }

    public String getVideo() {
        return video;
    }

    public void setVideo(String video) {
        this.video = video;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("id", getId())
                .append("name", getName())
                .append("memo", getMemo())
                .append("video", getVideo())
                .toString();
    }

}

2.6、Mapper类

package com.cnblogs.javalouvre.mapper;

import org.apache.ibatis.annotations.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cnblogs.javalouvre.entity.Knowledge;

@Mapper
public interface KnowledgeMapper extends BaseMapper<Knowledge> {

}

2.7、Service

2.7.1、接口
package com.cnblogs.javalouvre.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.cnblogs.javalouvre.entity.Knowledge;

public interface IKnowledgeService extends IService<Knowledge> {

}
2.7.2、实现类
package com.cnblogs.javalouvre.service.impl;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnblogs.javalouvre.entity.Knowledge;
import com.cnblogs.javalouvre.mapper.KnowledgeMapper;
import com.cnblogs.javalouvre.service.IKnowledgeService;

@Service
public class KnowledgeServiceImpl extends ServiceImpl<KnowledgeMapper, Knowledge> implements IKnowledgeService {

}

3、项目目录结构

│  pom.xml
│
├─src
│  ├─main
│  │  ├─java
│  │  │  └─com
│  │  │      └─cnblogs
│  │  │          └─javalouvre
│  │  │              │  App.java
│  │  │              │  ServletInitializer.java
│  │  │              │
│  │  │              ├─entity
│  │  │              │      Knowledge.java
│  │  │              │
│  │  │              ├─mapper
│  │  │              │      KnowledgeMapper.java
│  │  │              │
│  │  │              ├─service
│  │  │              │  │  IKnowledgeService.java
│  │  │              │  │
│  │  │              │  └─impl
│  │  │              │          KnowledgeServiceImpl.java
│  │  │              │
│  │  │              └─web
│  │  │                      IndexController.java
│  │  │
│  │  ├─resources
│  │  │  │  application.yml
│  │  │  │  mybatis-config.xml
│  │  │  │
│  │  │  └─mapper
│  └─test
│      ├─java
│      └─resources

----------------------------------------------------------------------------------------------------------