您好,登錄后才能下訂單哦!
小編給大家分享一下springboot多模塊如何將mybatis項(xiàng)目拆分出去,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
前言:
以前我們?cè)诮?xiàng)目的時(shí)候, 要么將所有的package建在一個(gè)項(xiàng)目里面, 在處理引用的時(shí)候, 真的很方便. 不用擔(dān)心, 有些東西配置不到或者讀取不到.
或者, 將package獨(dú)立出去, 到一個(gè)項(xiàng)目中或者子項(xiàng)目中. 這時(shí)候, 項(xiàng)目中的引用處理, 還是有些麻煩的. 不過好處更多, 不再表述.
在 idea 里面, 推薦使用 多模塊 建項(xiàng)目, 而不再是 eclipse 里面的那種方式. 那這里, 就試著將一個(gè)springboot 的項(xiàng)目拆分到子模塊中去, 看看效果如何.
項(xiàng)目拆分:
1. 目錄變化
2. 父項(xiàng)目
父pom.xml理論上來說, 應(yīng)該是對(duì)子項(xiàng)目中的引用進(jìn)行一個(gè)約束, 主要是版本約束.
所以父 pom.xm 中, 應(yīng)該使用dependencyManagement 來約束 子項(xiàng)目中 jar 包版本.
然后對(duì)于一些子項(xiàng)目都用得著的引用, 可以提到 父項(xiàng)目中去.
<?xml version="1.0" encoding="UTF-8"?> <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>cn.elvinle</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>parent</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <modules> <module>pojo</module> <module>web</module> <module>dao</module> <module>service</module> <module>simpl</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <druid.version>1.1.3</druid.version> <mybatis.boot.starter.version>1.3.1</mybatis.boot.starter.version> <mysql.connector.java.version>5.1.44</mysql.connector.java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.boot.starter.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.connector.java.version}</version> </dependency> </dependencies> </dependencyManagement> <!--申明依賴關(guān)系--> <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> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3. 子項(xiàng)目 - pojo
3.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>cn.elvinle</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.elvinle</groupId> <artifactId>pojo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>pojo</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.2 db實(shí)體
package cn.elvinle.pojo; /** * @author: elvin */ public class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
對(duì)于pojo和dao中的內(nèi)容, 可以使用mybatis逆向工程生成, 可以省去很多麻煩.
4. 子項(xiàng)目 - dao
4.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>cn.elvinle</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.elvinle</groupId> <artifactId>dao</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>dao</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!-- 內(nèi)部引用 --> <dependency> <groupId>cn.elvinle</groupId> <artifactId>pojo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- 外部引用 --> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
4.2 mapper
UserMapper.java:
public interface UserMapper { public List<User> getAll(); }
UserMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="cn.elvinle.dao.mapper.UserMapper" > <select id="getAll" resultType="cn.elvinle.pojo.User"> select * from user </select> </mapper>
5. 子項(xiàng)目 - service
5.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>cn.elvinle</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.elvinle</groupId> <artifactId>service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>cn.elvinle</groupId> <artifactId>pojo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
5.2 UserService.java
public interface UserService { public List<User> getAll(); }
6. 子項(xiàng)目 - simpl
6.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>cn.elvinle</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.elvinle</groupId> <artifactId>simpl</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>simpl</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>cn.elvinle</groupId> <artifactId>pojo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>cn.elvinle</groupId> <artifactId>service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>cn.elvinle</groupId> <artifactId>dao</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
6.2 UserSImpl
@Service public class UserSImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> getAll() { System.out.println("UserSImpl getAll"); return userMapper.getAll(); } }
7. 子項(xiàng)目 - web
關(guān)鍵的地方來了.
7.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>cn.elvinle</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.elvinle</groupId> <artifactId>web</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>web</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>cn.elvinle</groupId> <artifactId>simpl</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
7.2 application.yml
server: context-path: /parent port: 8080 mybatis: config-location: /mapper/*.xml spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.jdbc.Driver
7.3 控制器
@RestController @RequestMapping("api") public class ApiController { @Autowired private UserService userService; @RequestMapping("index") public List<User> index(){ List<User> all = userService.getAll(); return all; } }
到目前為止, 沒什么特別的, 都是正常修改, 接下來, 會(huì)出現(xiàn)與不分模塊不同的地方.
7.4 入口處修改
@ComponentScan({"cn.elvinle"}) @SpringBootApplication public class WebApplication { public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); } }
這時(shí)候, 如果直接運(yùn)行起程序, 是不會(huì)成功訪問的. 原因在于, mybatis的自動(dòng)化配置和自動(dòng)化創(chuàng)建, 沒有支持到多模塊中.
這時(shí)候, 需要我們手動(dòng)進(jìn)行配置和創(chuàng)建.
7.5 Mybatis java 配置
<!-- mybatis 的工廠 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/> <!-- 自動(dòng)掃描mapping.xml文件 --> <!--<property name="mapperLocations" value="classpath:mapper/*.xml" />--> </bean> <!-- DAO接口所在包名,Spring會(huì)自動(dòng)查找其下的類 --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.elvinle.bookshop.mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>
可以對(duì)照著上面的配置文件, 進(jìn)行java配置
MybatisConfig:
@Configuration public class MybatisConfig { @Value("${mybatis.config-location}") private String mapperLocationPattern; @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource(){return new com.alibaba.druid.pool.DruidDataSource(); } @Bean(name="sqlSessionFactory") public SqlSessionFactory sqlSessionFactory() throws Exception{ SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactoryBean.setMapperLocations(resolver.getResources(mapperLocationPattern)); return sqlSessionFactoryBean.getObject(); } }
MybatisMapperScannerConfig:
@Configuration @AutoConfigureAfter(MybatisConfig.class) @MapperScan("cn.elvinle.dao.mapper") public class MybatisMapperScannerConfig { public MapperScannerConfigurer mapperScannerConfigurer(){ MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory"); mapperScannerConfigurer.setBasePackage("cn.elvinle.dao.mapper"); return mapperScannerConfigurer; } }
OK, 到這里, 就可以把程序跑起來了, 看一下結(jié)果:
以上是“springboot多模塊如何將mybatis項(xiàng)目拆分出去”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。