spring cloud学习第二步:spring boot配置mybatis

原创
2018/01/23 17:08
阅读数 3.7K

因为本人使用的是Mybatis作为持久层框架,所以本文主要介绍spring boot集成Mybatis如何

1.首先因为所需要的jar包,mybatis-spring-boot-starter & mysql-connector-java 两个引用

完整的pom.xml,引入mybatis依赖jar包

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>com.aulbrother</groupId>
    <artifactId>first</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>first</name>
    <description>第一步</description>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.9.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <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>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.3.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

 

2.配置application.yml,配置数据源和mybatis配置

server:
  port: 8080
  servlet-path: /first
spring:
 # 数据源配置
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8
    username: root
    password: 123456
#mybaits配置
mybatis:
  mapper-locations: classpath:com/aulbrother/first/modules/**/mapper/*.xml #根据mapper.xml所在的目录设置
  type-aliases-package: com.aulbrother.first.modules.**.po
  configuration:
    cacheEnabled: false
    lazyLoadingEnabled: false
    multipleResultSetsEnabled: true
    useColumnLabel: true
    useGeneratedKeys: true
    autoMappingBehavior: PARTIAL
    defaultExecutorType: SIMPLE
    defaultStatementTimeout: 25
    safeRowBoundsEnabled: false
    mapUnderscoreToCamelCase: true
    localCacheScope: SESSION
    jdbcTypeForNull: OTHER
    lazyLoadTriggerMethods: equals,clone,hashCode,toString

3.在Spring boot main类增加MapperScan注解

@SpringBootApplication
@MapperScan(basePackages = {"com.aulbrother.first.modules"})
public class FirstApplication{

    public static void main(String[] args){
        SpringApplication.run(FirstApplication.class, args);
        System.out.println("Succ start app #####################################################################!!");
    }
}

4.然后依次创建Controller,Service,Dao,Mapper.xml

 

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部