一、公司架構爲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()); }