springboot2.0系列四(整合mybatis)

springboot整合使用mybatis

4.2.1、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>

 

 

4.2.2、配置文件引入

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

 

 

4.2.3、Mapper代码

package com.example.demo.service;

  

  import org.apache.ibatis.annotations.Insert;

  import org.apache.ibatis.annotations.Mapper;

  import org.apache.ibatis.annotations.Param;

  import org.apache.ibatis.annotations.Select;

  

  import java.util.Map;

  @Mapper

  public interface UserMapper {

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

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

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

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

}

 

4.2.3、Controller代码

package com.example.demo.controller;

  

  import com.example.demo.service.UserMapper;

  

  import org.springframework.beans.factory.annotation.Autowired;

  import org.springframework.web.bind.annotation.RequestMapping;

  import org.springframework.web.bind.annotation.RestController;

  

  import java.util.Map;

  

  @RestController

  public class UsersController {

    @Autowired

    UserMapper userMapper;

  

    @RequestMapping("/insertUser")

    public void insertUser(String name,Integer age) {

  

        userMapper.insert(name,age);

  

    }

    @RequestMapping("/user")

    public Map findUser(String name) {

  

      Map<String,Object> result= userMapper.findByName(name);

      return result;

  

    }

}

 

 

4.2.4、启动方式

@SpringBootApplication

public class MybatisApp {

 

      public static void main(String[] args) {

            SpringApplication.run(MybatisApp.class, args);

      }

 

}

效果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值