热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

bootjndi数据源spring_SpringBoot配置JNDI数据源

1、数据源配置1.1、%CATALINA_HOME%confserver.xml配置全局资源server.xml文件找到GlobalNamingResources节点,

1、 数据源配置

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

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

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节点,加入如下代码

2、构建Spring Boot工程

2.1、pom.xml配置

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

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



推荐阅读
author-avatar
夫功的美丽家园
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有