一、工程结构:

SpringBoot(二)SpringBoot集成Mybatis

 

二、主要文件配置说明

1、pom.xml

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
        
<!-- 继承mybatis   启动时回默认加载src/main/resources下面的application.yml配置文件,加载数据源 -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>
        
<!-- 数据库连接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.0</version>
</dependency>
</dependencies>


2、Application注解
   
    SpringBootApplication= (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan
    1)、@Configuration:提到@Configuration就要提到他的搭档@Bean。使用这两个注解就可以创建一个简单的spring配置类,可以用来替代相应的xml配置文件。
    2)、@EnableAutoConfiguration:能够自动配置spring的上下文,试图猜测和配置你想要的bean类,通常会自动根据你的类路径和你的bean定义自动配置。
    3)、@ComponentScan:会自动扫描指定包(默认当前包及其子包)下的全部标有@Component的类,并注册成bean,当然包括@Component下的子注解@Service,@Repository,@Controller。


3、默认加载配置文件:src/main/resources/applcation.yml
    spring.profiles.active = dev/pro/test  开发、生产、测试环境  
    mybatis:
      mapperLocations: classpath:mapper/*.xml    #

    spring:
      profiles: dev
      datasource:
      url: jdbc:mysql://localhost:3306/test
      username: root
      password: 123456
      driver-class-name: com.mysql.jdbc.Driver
      # 使用druid数据源

      type: com.alibaba.druid.pool.DruidDataSource

 

三、主要代码层说明

1)dao层:

SpringBoot(二)SpringBoot集成Mybatis

2)service层

SpringBoot(二)SpringBoot集成Mybatis

SpringBoot(二)SpringBoot集成Mybatis

3)web层

SpringBoot(二)SpringBoot集成Mybatis

4)application

SpringBoot(二)SpringBoot集成Mybatis

5)application.yml

SpringBoot(二)SpringBoot集成Mybatis

6)UserMapper.xml

SpringBoot(二)SpringBoot集成Mybatis

7)pom.xml

SpringBoot(二)SpringBoot集成Mybatis

SpringBoot(二)SpringBoot集成Mybatis

 

SpringBoot(二)SpringBoot集成Mybatis

相关文章: