踩坑提示:web.xml中需要修改的地方,看里面的注释即可
contextConfigLocation
com.trs.xxx.network.SpringbootApplication
org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener
appServlet
org.springframework.web.servlet.DispatcherServlet
contextAttribute
org.springframework.web.context.WebApplicationContext.ROOT
1
appServlet
/
weblogic下载安装教材:https://blog.csdn.net/qq_36868342/article/details/79967606
正式开始:
一:首先加入连接weblogic的包:去网上下载wlfullclient.jar,作为第三方jar包添加到项目中。
springboot添加第三方包:https://blog.csdn.net/yanzhenjingfan/article/details/90517877
二:在application.properties加入jndi-name,添加mybatis的xml映射文件路径
#JNDI-NAME
spring.datasource.jndi-name=jndi/cmsdb
#mybatis映射文件
mybatis.mapper-locations=classpath:mapping/**/*.xml
三:pom.xml文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.1.RELEASE
ccb
xxx
0.0.1-SNAPSHOT
xxx
war
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-tomcat
provided
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.0
mysql
mysql-connector-java
com.weblogic
weblogic
1.0.1
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
true
true
org.springframework.boot
spring-boot-maven-plugin
四:配置weblogic的jndi数据源
package com.trs.xxx.network.configuration;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jndi.JndiObjectFactoryBean;
/**
*
* @author : 雁阵惊凡
* @version :2019年5月20日 下午2:57:00
*
* TODO :配置weblogic的jndi数据源
*
*/
@Configuration
public class DataSourceConfig {
@Bean(name = "dataSource")
public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, "t3://localhost:7001");
//weblogic账号
properties.put(Context.SECURITY_PRINCIPAL, "weblogic");
//weblogic密码
properties.put(Context.SECURITY_CREDENTIALS, "weblogic@123");//
bean.setJndiEnvironment(properties);
bean.setResourceRef(true);
bean.setJndiName("jndi/cmsdb");
bean.setProxyInterface(DataSource.class);
bean.setLookupOnStartup(false);
bean.afterPropertiesSet();
return (DataSource) bean.getObject();
}
}
五:mybatis获取jndi数据源
package com.trs.xxx.network.configuration;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
/**
*
* @author : 雁阵惊凡
* @version :2019年5月20日 下午3:01:19
*
* TODO :mybatis获取jndi数据源
*
*/
@Configuration
@MapperScan(basePackages = "com.trs.xxx.network.mapper", sqlSessionFactoryRef = "sqlSessionFactory")
public class DSConfig {
//获取dataSourcec
@Autowired
@Qualifier("dataSource")
private DataSource dataSource;
//获取application.propertis配置文件中的mybatis.mapper-locations
@Value("${mybatis.mapper-locations}")
private String mapperLocations;
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
factoryBean.setMapperLocations(resolver.getResources(mapperLocations));
return factoryBean.getObject();
}
@Bean
public DataSourceTransactionManager transactionManager() throws Exception {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public SqlSessionTemplate sqlSessionTemplate() throws Exception {
return new SqlSessionTemplate(sqlSessionFactory());
}
}
六:运行weblogic,然后在浏览器中看是否可以登陆了:http://localhost:7001/console
D:\Oracle\Middleware\Oracle_Home\user_projects\domains\base_domain
七:成功开启weblogic后,写一个测试,运行testConn方法打印看是否拿到datasource
注意,直接运行springboot中的main函数,会报错。我们还是写一个test,看是否拿到jndi数据源
package com.trs.xxx.network;
import java.sql.Connection;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.jndi.JndiObjectFactoryBean;
import org.springframework.test.context.junit4.SpringRunner;
import com.trs.ccb.network.entity.integratedMgr.NWNode;
import com.trs.ccb.network.service.integratedMgr.NWNodeService;
@SpringBootTest
@RunWith(SpringRunner.class)
public class MybatisApplicationTests {
@Test
public void testConn() throws Exception {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, "t3://localhost:7001");
properties.put(Context.SECURITY_PRINCIPAL, "weblogic");
properties.put(Context.SECURITY_CREDENTIALS, "weblogic@123");
bean.setJndiEnvironment(properties);
bean.setResourceRef(true);
bean.setJndiName("jndi/cmsdb");
bean.setProxyInterface(DataSource.class);
bean.setLookupOnStartup(false);
bean.afterPropertiesSet();
DataSource dataSource = (DataSource) bean.getObject();
//打印出jndi数据源
System.err.println(dataSource);
}
}
上面红色打印出来的,表示已经拿到dataSource对象了。
八:springboot启动文件
package com.trs.xxx.network;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.WebApplicationInitializer;
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class SpringbootApplication extends SpringBootServletInitializer implements WebApplicationInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringbootApplication.class);
}
}
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}),加入这个注解,是为了排除运行程序时,springboot回去读取spring.datasource.xxx=xx的配置。我们这里连接的是jndi,不需要加入任何其他的数据库连接配置,不加这个配置,有可能会报错。
九:pom.xml中
注意:打包时记得把测试案例注释掉,有可能会导致打包不成功。
eclipse怎么打包应该都会吧,用IDEA的童鞋自行百度,先maven clean一下,再maven install,在target目录下就生成war包了,步骤:
1:eclipse中右键项目,Run As ----> maven clean
2:eclipse中右键项目,Run As ----> maven install
部署测试,不同版本的部署方式可能有些细微差别,大同小异,应该可以搞定的,很简单
在weblogic上部署web应用可以参考 https://www.cnblogs.com/DFX339/p/8515200.html
十:浏览器访问测试: