springboot配置多模块项目

来源:互联网 发布:淘宝足球竞猜 编辑:程序博客网 时间:2024/05/20 23:05

用idea构建多模块spring boot项目,步骤如下:

1.创建父项目,删除src,target目录,只保留pom.xml。

2.配置父模块pom。

<?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>com.xxxx.xxxxx</groupId>    <artifactId>parent</artifactId>    <version>1.0-SNAPSHOT</version>    <packaging>pom</packaging>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.3.3.RELEASE</version>    </parent>    <modules>        <module>model</module>        <module>dao</module>        <module>service</module>        <module>controller</module>    </modules>    <!--依赖关系-->    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-jdbc</artifactId>        </dependency>        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis-spring</artifactId>            <version>1.2.2</version>        </dependency>        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis</artifactId>            <version>3.2.8</version>        </dependency>        <dependency>            <groupId>org.apache.tomcat</groupId>            <artifactId>tomcat-jdbc</artifactId>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>        </dependency>    </dependencies>    <!--设置maven仓库-->    <repositories>        <repository>            <id>spring-releases</id>            <url>https://repo.spring.io/libs-release</url>        </repository>    </repositories>    <pluginRepositories>        <pluginRepository>            <id>spring-releases</id>            <url>https://repo.spring.io/libs-release</url>        </pluginRepository>    </pluginRepositories></project>

3.创建子模块

右键create module。

4.修改子模块配置,打成jar包,引入需要的依赖。

controller需要引入service和model包;service需要引入dao和model包;dao需要引入model包。

这不是绝对的,具体情况具体分析。

5.创建启动类

在controller包里面建立有@SpringBootApplication注解的类,@ComponentScan指明需要扫描的包。

@SpringBootApplication@ComponentScan("com.xxxx.xxxx.project")public class Application {    public static void main(String[] args){        SpringApplication.run(Application.class, args);    }}

6.配置Mybatis

在dao包的resources文件夹中建立application.properties文件,用于配置数据连接信息。

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/bendispring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driver
设置dao文件路径和对应的xml文件路径。

@Configurationpublic class MyBatisConfig {    @Bean    @ConfigurationProperties(prefix = "spring.datasource")    public DataSource dataSource(){        return new org.apache.tomcat.jdbc.pool.DataSource();    }    @Bean(name = "sqlSessionFactory")    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();        sqlSessionFactoryBean.setDataSource(dataSource());        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mybatis/*.xml"));        return sqlSessionFactoryBean.getObject();    }
@Configuration@MapperScan("com.xxxx.xxxx.project.dao.mapping")@AutoConfigureAfter(MyBatisConfig.class)public class MyBatisMapperScannerConfig {    public MapperScannerConfigurer mapperScannerConfigurer(){        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");        mapperScannerConfigurer.setBasePackage("com.xxxx.xxxx.project.dao.mapping");        return mapperScannerConfigurer;    }}

至此运行启动类的主函数,项目就可以跑起来了,如果遇到问题,还需要具体分析。




阅读全文
1 0