How to deploy Mybatis on weblogic in spring boot integration

Keywords: Oracle Weblogic Spring Maven xml

introduce

In this paper, we refer to How to deploy spring boot gracefully on weblogic Mybatis is deployed on weblogic Server by using springboot.

Environmental Science

Development tool: Eclipse
weblogic version: 10.3.6.0
weblogic-jdk: 1.8.0_91

Technology realization

  1. Create Maven project and add it to pom file
<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>
         <!--Use durid data source  -->
         <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>

Note: because the ojdbc package cannot be pulled in maven warehouse, we need to import it manually.
2. Create springboot entry class, configuration class and test class

@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);
    }
@Configuration
public class MybatisConfig {
    @Autowired 
    private DataSource dataSource;   //Durid data source
    
    @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);    //Set hump name
    sqlSessionFactoryBean.setConfiguration(config);
    sqlSessionFactoryBean.setTypeAliasesPackage("com.ws.bean"); 
    Interceptor[] plugins =  new Interceptor[]{pageHelper()};
    sqlSessionFactoryBean.setPlugins(plugins);
    return sqlSessionFactoryBean; 
    }
    
    /**
     * Mybatis Paging plugins 
     */
    @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 "I am"+user.getUserName()+"This year"+user.getUserAge()+"Year old!";
    }
}

Note: the configuration of the durid data source is not described in detail here. Spring boot uses the Tomcat data source by default. When I use the Tomcat data source, I report an error (no supported data source type found) when I deploy it, and then I change it to the durid data source. At the beginning, the version of the springboot mybatis integration package I used was 1.1.3. When I deployed, I reported an error and changed the version to 1.1.1.

3. Create web.xml and weblogic.xml

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

Note: for creating web.xml in eclipse, please refer to Solve the problem that there is no web.xml in the new maven project
4. Package and deploy
First, add < packaging > war < / packaging > to pom.xml. In eclipse, you can right-click pom.xml - > run as - > Maven install to make the project a war package

5. Deployment test
To deploy web applications on weblogic, please refer to weblogic deployment web project (war package).
Then access the test path of the project in the browser address bar.

6. summary
So far, we have successfully integrated springboot and mybatis into weblogic.

Posted by chuckjones on Sun, 15 Dec 2019 12:28:55 -0800