SpringBoot配置JNDI数据源

共 5509字,需浏览 12分钟

 ·

2021-01-17 09:18

点击上方蓝色字体,选择“标星公众号”

优质文章,第一时间送达

  作者 |  Bruce.Chang.Lee

来源 |  urlify.cn/jMFn2m

76套java从入门到精通实战课程分享

1、 数据源配置

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

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

  
    
    "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"/>

    "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" />
  

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

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

      "webapps" autoDeploy="true" name="localhost" unpackWARs="true">

        
        

        
        "org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".txt"/>

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

2、构建Spring Boot工程

2.1、pom.xml配置
"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">
  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    2.3.7.RELEASE
    
  

  com.cnblogs.javalouvre
  spring-boot-jndi
  0.0.1

  
    
      org.apache.commons
      commons-lang3
    

    
      javax.servlet
      javax.servlet-api
      provided
    


    
      org.springframework.boot
      spring-boot-starter-web
      
       
        
          org.springframework.boot
          spring-boot-starter-tomcat
        

      

    


    
      com.baomidou
      mybatis-plus-boot-starter
      3.4.1
      
       
        
          com.zaxxer
          HikariCP
        

      

    

  


  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      

    

  


  
  war

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相关配置

"1.0" encoding="UTF-8" ?>
"-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

  
    
    "cacheEnabled" value="true" />
    
    "useGeneratedKeys" value="true" />
    
    "defaultExecutorType" value="REUSE" />
    
    "logImpl" value="SLF4J" />
  


  
    
    "com.cnblogs.javalouvre.entity" />
  


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 {

}

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 {

}
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 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




粉丝福利:Java从入门到入土学习路线图

👇👇👇

👆长按上方微信二维码 2 秒


感谢点赞支持下哈 

浏览 95
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报