一、公司架构为mybatis使用的 springboot+mybatis
二、mybatis 使用的是 mybatis-generator 生成
三、可是其余项目每次使用的时候 都会在生成的 xml中进行添加 sql语句。 致使后续表变动从新生成要一个一个进行对比修改;因此改为统一辈子成的xml文件存放到 公共项目中 打成jar包引入; 其余项目进行引入;项目本身新增的xml中写本身的sql信息;进行分离java
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <!-- 源代码使用的JDK版本 --> <target>1.8</target> <!-- 须要生成的目标class文件的编译版本 --> <encoding>UTF-8</encoding><!-- 字符集编码 --> </configuration> </plugin>
当前是mybatis-generator 生成的目录mysql
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>
basePackages 表示的是公共包的mapper接口地址
mybatisXmlPath 表示的是 当前要获取的xml文件的目录 因为当前是从jar包中读取xml文件,因此麻烦一点使用读取jar包的方式读取xml文件; 读取jar包中的文件使用的是 inputstream 流的方式。最终添加到Mybatis的 MapperLocations中, 注:此处的new InputStreamResource(inputStream,jarEntry.getName());
中的jarEntry.getName 必须的不一样 不然会认为加载的是同一个文件;(暂时不懂,后面看源码补充)web
@Configuration @MapperScan(basePackages = "du.lo.sh.projectjar.mapper",sqlSessionTemplateRef = "adminMySqlSessionTemplate") public class BuildConfiguration { private String mybatisXmlPath = "mybatis/"; @javax.annotation.Resource private DataSource dataSourceRaw; @Bean @Qualifier("adminMysqlDataSource") public DataSource getDataSource(){ return dataSourceRaw; } @Bean(name = "adminMysqlTransactionManager") public DataSourceTransactionManager setTransactionManager(@Qualifier("adminMysqlDataSource") DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } @Bean(name = "adminMysqlSqlSessionFactory") public SqlSessionFactory setSqlSessionFactory (@Qualifier("adminMysqlDataSource") DataSource dataSource) throws Exception{ SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); Resource[] resources = packageMapperLocations(); if (resources != null){ bean.setMapperLocations(resources); } return bean.getObject(); } @Bean(name = "adminMySqlSessionTemplate") public SqlSessionTemplate setSqlSessionTemplate (@Qualifier("adminMysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception{ return new SqlSessionTemplate(sqlSessionFactory); } private Resource[] packageMapperLocations() throws IOException { List<Resource> resourceList = new ArrayList<>(); Enumeration<URL> enumerationUrl = ClassLoader.getSystemResources(mybatisXmlPath); while (enumerationUrl.hasMoreElements()){ URL url = enumerationUrl.nextElement(); if ("jar".equals(url.getProtocol())){ JarFile jarFile = ((JarURLConnection)url.openConnection()).getJarFile(); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()){ JarEntry jarEntry = entries.nextElement(); if (jarEntry.getName().startsWith(mybatisXmlPath) && !mybatisXmlPath.equals(jarEntry.getName()) && jarEntry.getName().endsWith("xml")){ InputStream inputStream = ClassLoader.getSystemResourceAsStream(jarEntry.getName()); Resource resource = new InputStreamResource(inputStream,jarEntry.getName()); resourceList.add(resource); } } } } if (!resourceList.isEmpty()){ Resource[] resources = new Resource[resourceList.size()]; resourceList.toArray(resources); return resources; } return null; } }
resources 下添加 META-INF/spring.factories文件 springboot启动自动扫描; spring.factories 中添加spring
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ du.lo.sh.projectjar.build.BuildConfiguration
须要在 pom.xml 中引入当前项目 配置数据源sql
@Configuration public class TestConfig { @Value("${spring.datasource.lottery.mysql.username}") private String userName; @Value("${spring.datasource.lottery.mysql.password}") private String password; @Value("${spring.datasource.lottery.mysql.url}") private String url; @Value("${spring.datasource.lottery.mysql.driver-class-name}") private String driver; //此处名字是固定的 @Bean(name = "dataSourceRaw") public DataSource setDataSource(){ DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setUsername(userName); druidDataSource.setPassword(password); druidDataSource.setUrl(url); druidDataSource.setDriverClassName(driver); return druidDataSource; } }
和在本地配置的没有区别apache
@Autowired private LotterySystemConfigMapper lotterySystemConfigMapper; @Autowired private LotteryUserTicketMapper lotteryUserTicketMapper; @Autowired private LotteryPrizeConfigMapper lotteryPrizeConfigMapper; @Autowired private LotteryStatisticsMapper lotteryStatisticsMapper; public void test(){ int result = lotteryUserTicketMapper.countByExample(new LotteryUserTicketExample()); int result2 = lotterySystemConfigMapper.countByExample(new LotterySystemConfigExample()); int result3 = lotteryPrizeConfigMapper.countByExample(new LotteryPrizeConfigExample()); int result4 = lotteryStatisticsMapper.countByExample(new LotteryStatisticsExample()); }