本文在參考了如何優雅的在weblogic上部署spring-boot的基礎下使用springboot整合Mybatis部署在weblogic服務器上。html
開發工具:Eclipse
weblogic版本:10.3.6.0
weblogic-jdk:1.8.0_91java
<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>com.ws</groupId> <artifactId>weblogic-test</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <spring.boot.version>1.5.4.RELEASE</spring.boot.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.2.0</version> </dependency> <!--使用durid數據源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.25</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-messaging</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-legacy</artifactId> <version>1.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <attachClasses>true</attachClasses> <archive> <manifestEntries> <Weblogic-Application-Version>${project.version}</Weblogic-Application-Version> </manifestEntries> <manifest> <addClasspath>true</addClasspath> <addClasspath>lib/</addClasspath> </manifest> </archive> <webResources> <resource> <directory>${project.basedir}/src/main/resources/static</directory> </resource> <resource> <directory>${project.basedir}/src/main/webapp</directory> </resource> </webResources> <warName>${project.artifactId}</warName> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring.boot.version}</version> <configuration> <classifier>BOOT</classifier> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>
2.建立springboot入口類,配置類和測試類git
@SpringBootApplication @MapperScan("com.ws.mapper") public class WeblogicTestApplication extends SpringBootServletInitializer implements WebApplicationInitializer{ public static void main(String[] args) { SpringApplication.run(WeblogicTestApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(WeblogicTestApplication.class); }
配置文件github
server.port=8888 logging.level.=INFO server.tomcat.uri-encoding=UTF-8 spring.http.encoding.force=true spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true server.connection-timeout=5000 spring.resources.static-locations=classpath:static/,file:static/ #spring.devtools.restart.enabled: true mybatis.configuration.mapUnderscoreToCamelCase=true mybatis.type-aliases-package=com.df.bean spring.dataSource.primaryDataSource.type=com.alibaba.druid.pool.DruidDataSource spring.dataSource.primaryDataSource.url=jdbc:oracle:thin:@10.2.1.242:1521/eaidb spring.dataSource.primaryDataSource.username=cheryeai_osbmngt spring.dataSource.primaryDataSource.password=cheryDBeai123 spring.dataSource.primaryDataSource.driverClassName = oracle.jdbc.driver.OracleDriver spring.dataSource.primaryDataSource.initialSize = 5 spring.dataSource.primaryDataSource.minIdle = 5 spring.dataSource.primaryDataSource.maxActive = 15 spring.dataSource.primaryDataSource.maxWait = 60000 spring.dataSource.primaryDataSource.timeBetweenEvictionRunsMillis = 60000 spring.dataSource.primaryDataSource.minEvictableIdleTimeMillis = 300000 spring.dataSource.primaryDataSource.validationQuery = SELECT 1 FROM DUAL spring.dataSource.primaryDataSource.testWhileIdle = true spring.dataSource.primaryDataSource.testOnBorrow = true spring.dataSource.primaryDataSource.testOnReturn = true
配置數據源web
@Component @ConfigurationProperties(prefix = "spring.dataSource.primaryDataSource") public class DuridBeans { private String type; private String url; private String username; private String password; private String driverClassName; private Integer initialSize; private Integer minIdle; private Integer maxActive; private Integer maxWait; private Integer timeBetweenEvictionRunsMillis; private Integer minEvictableIdleTimeMillis; private String validationQuery; private Boolean testWhileIdle; private Boolean testOnBorrow; private Boolean testOnReturn; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public Integer getInitialSize() { return initialSize; } public void setInitialSize(Integer initialSize) { this.initialSize = initialSize; } public Integer getMinIdle() { return minIdle; } public void setMinIdle(Integer minIdle) { this.minIdle = minIdle; } public Integer getMaxActive() { return maxActive; } public void setMaxActive(Integer maxActive) { this.maxActive = maxActive; } public Integer getMaxWait() { return maxWait; } public void setMaxWait(Integer maxWait) { this.maxWait = maxWait; } public Integer getTimeBetweenEvictionRunsMillis() { return timeBetweenEvictionRunsMillis; } public void setTimeBetweenEvictionRunsMillis(Integer timeBetweenEvictionRunsMillis) { this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; } public Integer getMinEvictableIdleTimeMillis() { return minEvictableIdleTimeMillis; } public void setMinEvictableIdleTimeMillis(Integer minEvictableIdleTimeMillis) { this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; } public String getValidationQuery() { return validationQuery; } public void setValidationQuery(String validationQuery) { this.validationQuery = validationQuery; } public Boolean getTestWhileIdle() { return testWhileIdle; } public void setTestWhileIdle(Boolean testWhileIdle) { this.testWhileIdle = testWhileIdle; } public Boolean getTestOnBorrow() { return testOnBorrow; } public void setTestOnBorrow(Boolean testOnBorrow) { this.testOnBorrow = testOnBorrow; } public Boolean getTestOnReturn() { return testOnReturn; } public void setTestOnReturn(Boolean testOnReturn) { this.testOnReturn = testOnReturn; } }
@Configuration public class DuridSource { @Autowired private DuridBeans druidPrimaryDataSourceConfigProperties; @Bean(name="dataSource") @Primary public DataSource primaryDataSource (){ DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(this.druidPrimaryDataSourceConfigProperties.getUrl()); datasource.setUsername(this.druidPrimaryDataSourceConfigProperties.getUsername()); datasource.setPassword(this.druidPrimaryDataSourceConfigProperties.getPassword()); datasource.setDriverClassName(this.druidPrimaryDataSourceConfigProperties.getDriverClassName()); datasource.setInitialSize(this.druidPrimaryDataSourceConfigProperties.getInitialSize()); datasource.setMinIdle(this.druidPrimaryDataSourceConfigProperties.getMinIdle()); datasource.setMaxActive(this.druidPrimaryDataSourceConfigProperties.getMaxActive()); datasource.setMaxWait(this.druidPrimaryDataSourceConfigProperties.getMaxWait()); datasource.setTimeBetweenEvictionRunsMillis(this.druidPrimaryDataSourceConfigProperties.getTimeBetweenEvictionRunsMillis()); datasource.setMinEvictableIdleTimeMillis(this.druidPrimaryDataSourceConfigProperties.getMinEvictableIdleTimeMillis()); datasource.setValidationQuery(this.druidPrimaryDataSourceConfigProperties.getValidationQuery()); datasource.setTestWhileIdle(this.druidPrimaryDataSourceConfigProperties.getTestWhileIdle()); datasource.setTestOnBorrow(this.druidPrimaryDataSourceConfigProperties.getTestOnBorrow()); datasource.setTestOnReturn(this.druidPrimaryDataSourceConfigProperties.getTestOnReturn()); return datasource; } }
@Configuration public class MybatisConfig { @Autowired private DataSource dataSource; //Durid數據源 @Bean public SqlSessionFactoryBean createSqlSessionFactoryBean() throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); org.apache.ibatis.session.Configuration config=new org.apache.ibatis.session.Configuration(); config.setMapUnderscoreToCamelCase(true); //設置駝峯命名 sqlSessionFactoryBean.setConfiguration(config); sqlSessionFactoryBean.setTypeAliasesPackage("com.ws.bean"); Interceptor[] plugins = new Interceptor[]{pageHelper()}; sqlSessionFactoryBean.setPlugins(plugins); return sqlSessionFactoryBean; } /** * Mybatis分頁插件 */ @Bean public PageHelper pageHelper() { PageHelper pageHelper = new PageHelper(); Properties p = new Properties(); p.setProperty("offsetAsPageNum", "true"); p.setProperty("rowBoundsWithCount", "true"); p.setProperty("reasonable", "true"); pageHelper.setProperties(p); return pageHelper; }
public interface UserMapper { @Select({"SELECT * FROM USER_TEST"}) List<User> getUserInfo(); } @Service public class UserService { @Autowired private UserMapper userMapper; public List<User> getUserInfoSer(){ return userMapper.getUserInfo(); } } @RestController public class UserController { @Autowired private UserService userService; @RequestMapping(value="/test" ,method= RequestMethod.GET) public String testGetUserInfo() { User user=userService.getUserInfoSer().get(0); return "我是"+user.getUserName()+"今年"+user.getUserAge()+"歲了!"; } }
說明:關於Durid數據源的配置這裏就不詳細說明了,springboot默認使用tomcat的數據源,在使用tomcat數據源部署的時候報錯(NO supported Datasource type found),而後就給換成durid數據源。spring
3.建立web.xml和weblogic.xmlsql
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>com.ws.WeblogicTestApplication</param-value> </context-param> <listener> <listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextAttribute</param-name> <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <wls:weblogic-web-app xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.4/weblogic-web-app.xsd"> <wls:context-root>/testweblogic</wls:context-root> <wls:container-descriptor> <wls:prefer-application-packages> <wls:package-name>org.slf4j.*</wls:package-name> <wls:package-name>org.springframework.*</wls:package-name> </wls:prefer-application-packages> </wls:container-descriptor> </wls:weblogic-web-app>
說明:關於在eclipse中建立web.xml能夠參考解決新建maven工程沒有web.xml的問題
4.打包部署
先在pom.xml中加入<packaging>war</packaging>。在eclipse中能夠右擊pom.xml->Run As->Maven instal 將項目打成war包apache
5.部署測試
在weblogic上部署web應用能夠參考weblogic部署web項目(war包)。
而後在瀏覽器地址欄訪問項目的測試路徑。
segmentfault
6.總結
到這裏咱們已經成功將 springboot和mybatis整合並部署到weblogic上。api