pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <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.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!-- 阿里巴巴连接池Druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.5</version>
        </dependency>

        <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>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

 

application.yml

spring:
 datasource:
  driver-class-name: com.mysql.cj.jdbc.Driver
  username: root
  password: root
  url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT%2B8

 jpa:
  hibernate:
   ddl-auto: update
  show-sql: true

mybatis:
# mapper.xml文件中resultMap的type或者parameterType会使用自定义的pojo type
-aliases-package: com.vast.entity mapper-locations: classpath:mapper/*.xml
config-location: classpath:mapper/mybatis-config.xml configuration: cache-enabled: true

# mybatis核心配置文件中settings中配置,指定 MyBatis 应如何自动映射列到字段或属性。 NONE 表示取消自动映射;PARTIAL 只会自动映射没有定义嵌套结果集映射的结果集。 FULL 会自动映射任意复杂的结果集(无论是否嵌套)。默认是partial,这是一种全局设置 auto-mapping-behavior: FULL
# executor-type值为SIMPLE、REUSE,可通过insert、update、delete方法的返回值判断sql是否执行成功,返回非0表示执行sql成功的条数,返回0表示sql执行失败
# executor-type值为BATCH,insert、update、delete方法返回值一直会是负数-2147482646,在该模式下insert、update、delete返回值将无任何意义,不能作为判断sql执行成功的判断依据
default-executor-type: REUSE map-underscore-to-camel-case: true lazy-loading-enabled: true
# 依层次分别延迟加载,但是不管POJO这个属性是否被调用都会加载进来 aggressive-lazy-loading: false
# 将log集成到控制台 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#指定某个包发生日志操作就在后台打印,不设置无法打印预编译SQL
logging:
  level:
    com:
      vast: debug
 

mybatis-config.xml (打印预编译SQL语句)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!-- 打印SQL语句到控制台 -->
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
  <typeAliases> </typeAliases>
</configuration>

 

Dao

package com.vast.dao;

import com.vast.entity.Account;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface IAccountMybatisDao {

    public int saveAccount(Account account);

    public List<Account> queryAccountInfoById(Integer id);
}

 

资源文件夹下建立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="com.vast.dao.IAccountMybatisDao"> <insert id="saveAccount" parameterType="account"> insert into account(id, name, money) values (#{id}, #{name}, #{money}) </insert> <select id="queryAccountInfoById" resultType="account"> select * from Account </select> </mapper>

 

Dao注入被调用类

    @Autowired
    private IAccountMybatisDao accountMybatisDao;

 

posted on 2019-12-09 14:21  风又奈何  阅读(431)  评论(0编辑  收藏  举报