Springboot多模块构建

一、新建项目

新建项目,删除src目录,然后新建模块。
pom.xml



    4.0.0

    
    com.yth
    springboot-multi
    0.0.1-SNAPSHOT
    pom

    springboot-multi
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    
    
    
        dao
        web
        entity
        service
    

    
    

        
            
                com.yth
                web
                0.0.1-SNAPSHOT
            
            
                com.yth
                dao
                0.0.1-SNAPSHOT
            
            
                com.yth
                entity
                0.0.1-SNAPSHOT
            
            
                com.yth
                service
                0.0.1-SNAPSHOT
            

            
            
                org.springframework.boot
                spring-boot-starter-data-redis
            

            
            
                postgresql
                postgresql
                9.1-901-1.jdbc4
            
            
            
                org.mybatis.spring.boot
                mybatis-spring-boot-starter
                1.3.2
            

            
                org.mybatis.generator
                mybatis-generator-core
                1.3.2
            

            
            
                com.alibaba
                druid-spring-boot-starter
                1.1.0
            

        
    

    
    
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter
        
    

    

二、dao层,管理数据库访问

使用mybatis-generator自动生成相关文件
pom.xml



    4.0.0

    com.yth
    dao
    0.0.1-SNAPSHOT
    jar

    dao
    Demo project for Spring Boot

    
    
        com.yth
        springboot-multi
        0.0.1-SNAPSHOT
    

    

        
        
            postgresql
            postgresql
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
        
        
            org.mybatis.generator
            mybatis-generator-core
        
        
        
            com.alibaba
            druid-spring-boot-starter
        
        
        
            com.yth
            entity
        

    

    
    
        
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.2
                
                    true
                    true
                
            
        
    

配置文件application-dao.yml必须以application-开头命名

#公共配置与profiles选择无关 mapperLocations指的路径是src/main/resources
mybatis:
  typeAliasesPackage: com.yth.entity
  mapperLocations: classpath:mapper/*.xml
  configuration:
          map-underscore-to-camel-case: true #启用驼峰映射

#开发配置
spring:
  profiles: dev
  datasource:
    #这是postgresql的,mysql对应改一改
    url: jdbc:postgresql://127.0.0.1:5432/test03
    username: postgres
    password: *****  
    driver-class-name:  org.postgresql.Driver

    # 使用druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    #默认为stat,即开启sql监控。这里加了个wall,表示同时开启sql防火墙
    druid:
      filters: stat,wall
    #spring监控,hello.controller是我的控制层包名,也可以是服务层,用逗号分隔多个监控内容
      aop-patterns: com.yth.web.*
    #监控页面登录用户名
      StatViewServlet.loginUsername: admin
    #监控页面登录密码
      StatViewServlet.loginPassword: 123

自动生成的配置文件generatorConfig.xml





    
    
    
        
        
        
            
            
        

        
        
        

        
        
            
        

        
        
            
            
        

        
        
            
        
        
        
            
        

        
        

接下来点击如下图,即可自动生成相关文件。


Springboot多模块构建_第1张图片
mybatis自动生成.PNG

mybatis的自动化配置和自动化创建, 没有支持到多模块中,因次需要使用java的配置方式配置。

@Configuration
public class MybatisConfig {
    @Value("${mybatis.mapperLocations}")
    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();
    }
}

@Configuration
@AutoConfigureAfter(MybatisConfig.class)
@MapperScan("com.yth.dao")
public class MybatisMapperScannerConfig {
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage("com.yth.dao");
        return mapperScannerConfigurer;
    }
}

然后就OK啦。Mapper接口和Mapper.xml和实体类都是自动生成。

service模块

pom.xml



    4.0.0
    com.yth
    service
    0.0.1-SNAPSHOT
    jar
    service
    Demo project for Spring Boot
    
        com.yth
        springboot-multi
        0.0.1-SNAPSHOT
    
    
        
            com.yth
            entity
        
        
            com.yth
            dao
        
    

具体的接口和实现就不贴出来了,没啥可注意的。

entity模块

pom.xml



    4.0.0
    com.yth
    entity
    0.0.1-SNAPSHOT
    jar
    entity
    Demo project for Spring Boot
    
        com.yth
        springboot-multi
        0.0.1-SNAPSHOT
    
    

    

web模块,主模块

pom.xml



    4.0.0
    com.yth
    web
    0.0.1-SNAPSHOT
    jar

    web
    Demo project for Spring Boot
    
        com.yth
        springboot-multi
        0.0.1-SNAPSHOT
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            com.yth
            dao
        
        
            com.yth
            entity
        
        
            com.yth
            service
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


如果需要打包成jar的话build标签替换一下。然后执行mvc clean package命令就可以打包成功了。
之后运行的话 java -jar web.jar就可以了


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                    com.yth.web.WebApplication
                    ZIP
                
                
                    
                        
                            repackage
                        
                    
                
            
        
    

配置文件application.yml

server:
  port: 8080
  servlet:
    context-path: /yth
spring:
       profiles:
            include: dao  #必须把其他模块的配置文件添加进来哦
            active: dev

启动类

@SpringBootApplication
@ComponentScan(basePackages = {"com.yth"}) //自动扫描
public class WebApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class, args);
    }
}

测试Controller

@RestController
public class HelloController {
    @Autowired
    private UserService userService;
@RequestMapping("/hello") //使用@GetMapping不起作用,不知道为啥>.<。
    public String hello(){
        return "hello";
    }

    @RequestMapping("/user/{id}")
    public UserInfo getUserInfoById(@PathVariable("id")int id){
        return userService.getUserById(id);
    }
}

你可能感兴趣的:(Springboot多模块构建)