公司整了个WSO2产品,WSO2中使用的SpringBoot使用的版本为1.2.0.Release!
我这边写的代码要整合进去,我的代码是运行在SpringBoot 2.4.0版本上的。这样问题出现了,Springboot启动类继承的SpringBootServletInitializer 不一致,导致我的高版本不能放进WSO2项目框架中。
/**
* @author Rone
* @date 2019/07/02
*/
@EnableTransactionManagement(proxyTargetClass = true)
@EnableAutoConfiguration(exclude = {MybatisAutoConfiguration.class,DataSourceAutoConfiguration.class})
@SpringBootApplication
public class DeployTestApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DeployTestApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(DeployTestApplication.class, args);
}
}
只能降版本喽。
首先降pom文件中的版本降下来
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.2.0.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
然后更新mvn库。
现在要重新整合mybatis
喽,因为SpringBoot2.0以上版本都帮我们整合好了mybatis,只需要在*.yml
配置数据库信息以及mybatis日志信息就完事了,现在降版本必须手动整合mybatis
。
首先看项目结构
,这个项目结构如果出错,后面的配置都将是徒劳!!
一定要将启动类与controller、service、dao层在同一级目录
然后将mybatis的依赖加进pom文件中,最好我这个一样,因为我这个是整合成功后的(亲测完美运行)
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
然后在mybatis包中新建这两个类(mybatis配置类)里面都已经写好了注释,务必要配置好*.xml文件的位置
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
public class MyBatisConfig {
@Autowired
DataSource dataSource;
@Bean(name="sqlSessionFactory")
@ConditionalOnMissingBean //当容器里没有指定的Bean的情况下创建该对象
public SqlSessionFactory sqlSessionFactory() {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
try {
System.out.println("sqlSessionFactory try begin###############################################");
// 设置数据源
sqlSessionFactoryBean.setDataSource(dataSource);
// 设置mybatis的主配置文件
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource mybatisConfigXml = resolver.getResource("classpath:mybatisConfig/mapper-config.xml");
sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);
//设置mybatis扫描的mapper.xml文件的路径(非常重要,否则找不到mapper.xml文件)
Resource[] mapperResources = resolver.getResources("classpath:mappers/*.xml");
sqlSessionFactoryBean.setMapperLocations(mapperResources);
// 设置别名包,便于在mapper.xml文件中ParemeType和resultType不要写完整的包
sqlSessionFactoryBean.setTypeAliasesPackage("net/aas/penrose/deploy/Entity");
return sqlSessionFactoryBean.getObject();
}catch (Exception e) {
System.out.println("sqlSessionFactory catch###############################################");
// TODO: handle exception
e.printStackTrace();
throw new RuntimeException();
}
}
还有下面这一个,注意扫描的mapper包,就是dao包的路径改成自己的
package net.aas.penrose.deploy.mybatis;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@AutoConfigureAfter(MyBatisConfig.class)
public class MapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
mapperScannerConfigurer.setBasePackage("net/aas/penrose/deploy/dao");
return mapperScannerConfigurer;
}
}
接下来配置DataSource,由于不是springBoot帮我们自动注入,则需要手动注入datasource,现在application.yml文件就没有任何用处了
,因为不使用自动注入。`我们需要建立一个db.properties用来配置mysql信息
在这里将你的mysql数据库信息改进去
jdbc.url=jdbc:mysql://192.168.1.241:3306/LENO_DEVICE?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=UTC
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123456
最后建立一个dataSoruce配置类,这里如果读取配置文件路径正确的话,会给显示出jdbcUrl等信息的,db.properties的路径要写对
package net.aas.penrose.deploy.springConfig;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource(value = {"classpath:db.properties"})//也可以使用@ConfigurationProperties直接注入属性
public class DataSourcesConfig {
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.driverClassName}")
private String jdbcDriverClassName;
@Value("${jdbc.username}")
private String jdbcUsername;
@Value("${jdbc.password}")
private String jdbcPassword;
/**
* 配置数据库
* @return
*/
@Bean(name = "dataSource")
public DataSource dataSource(){
System.out.println("dataSource###############################################");
DruidDataSource datasource = new DruidDataSource();
// 数据库驱动
datasource.setDriverClassName(jdbcDriverClassName);
// 相应驱动的jdbcUrl
datasource.setUrl(jdbcUrl);
// 数据库的用户名
datasource.setUsername(jdbcUsername);
// 数据库的密码
datasource.setPassword(jdbcPassword);
// 每个分区最大的连接数
datasource.setMaxActive(20);
// 每个分区最小的连接数
datasource.setMinIdle(5);
return datasource;
}
}
接下来测试一下
新建testContrller
package net.aas.penrose.deploy.controller;
import net.aas.penrose.deploy.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/ML")
public class TestCont {
@Autowired
private DemoService demoService;
@RequestMapping(value = "/test",produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
@ResponseBody
public Object test(){
return demoService.getTest();
}
}
Serviece接口
package net.aas.penrose.deploy.service
public interface DemoService {
String getTest();
}
service实现类
package net.aas.penrose.deploy.service;
import net.aas.penrose.deploy.dao.DemoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class demoServiceImp implements DemoService {
@Autowired
private DemoMapper demoMappers;
@Override
public String getTest() {
return demoMappers.getTest();
}
}
Dao包中创建demoMapper
package net.aas.penrose.deploy.dao;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public interface DemoMapper {
String getTest();
}
编写xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//net.aas.penrose.deploy.mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="net.aas.penrose.deploy.dao.DemoMapper">
<select id="getTest" resultType="string">
SELECT '1' from dual
</select>
</mapper>
测试成功
最后注意一点就是注解,在springBoot启动类上一定不要加@MapperScan(这里会走自动注入那一套)
还有在启动类上添加
@EnableAutoConfiguration(exclude = {MybatisAutoConfiguration.class,DataSourceAutoConfiguration.class})
是为了卸载自动注入.
在贴一次启动类吧。。。。
package net.aas.penrose.deploy;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
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.context.web.SpringBootServletInitializer;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @author Rone
* @date 2019/07/02
*/
@EnableTransactionManagement(proxyTargetClass = true)
@EnableAutoConfiguration(exclude = {MybatisAutoConfiguration.class,DataSourceAutoConfiguration.class})
@SpringBootApplication
public class DeployTestApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DeployTestApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(DeployTestApplication.class, args);
}
}