SpringBoot-整合mybatis

springboot整合使用mybatis

pom文件引入

      <parent>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-parent</artifactId>

            <version>2.0.0.RELEASE</version>

      </parent>

      <dependencies>

            <dependency>

                  <groupId>org.springframework.boot</groupId>

                  <artifactId>spring-boot-starter</artifactId>

            </dependency>

            <!-- 测试 -->

            <dependency>

                  <groupId>org.springframework.boot</groupId>

                  <artifactId>spring-boot-starter-test</artifactId>

                  <scope>test</scope>

            </dependency>

            <dependency>

                  <groupId>org.mybatis.spring.boot</groupId>

                  <artifactId>mybatis-spring-boot-starter</artifactId>

                  <version>1.1.1</version>

            </dependency>

            <!-- mysql 依赖 -->

            <dependency>

                  <groupId>mysql</groupId>

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

            </dependency>

            <!-- springboot-web组件 -->

            <dependency>

                  <groupId>org.springframework.boot</groupId>

                  <artifactId>spring-boot-starter-web</artifactId>

            </dependency>

      </dependencies>

 

 

配置文件引入

spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

 

 

Mapper代码

public interface UserMapper {

      @Select("SELECT * FROM USERS WHERE NAME = #{name}")

      User findByName(@Param("name") String name);

      @Insert("INSERT INTO USERS(NAME, AGE) VALUES(#{name}, #{age})")

      int insert(@Param("name") String name, @Param("age") Integer age);

}

 

启动方式

//@ComponentScan(basePackages = { "com.ouyan.controller" })

@MapperScan("com.ouyan.mapper")

//@EnableAutoConfiguration

@SpringBootApplication

public class MybatisApp {

 

      public static void main(String[] args) {

            SpringApplication.run(MybatisApp.class, args);

      }

 

}

注:@Mapper与@MapperScan关系

参考:https://blog.csdn.net/fwk19840301/article/details/80251461

posted @ 2019-02-18 14:42  虚极静笃  Views(209)  Comments(0Edit  收藏  举报