spring boot集成mybatis

2020-12-13 04:46

阅读:446

标签:strong   sch   The   stdout   nbsp   epo   color   doc   err   

 1.新建一个spring boot web工程

idea: file-->new-->project-->Spring Initializr一路next下去。

2.最终目录

技术图片

pom.xml


         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASEcom.example
    mybtest
    0.0.1-SNAPSHOTmybtestDemo project for Spring Boot1.8org.springframework.boot
            spring-boot-starter
        org.springframework.boot
            spring-boot-starter-test
            testorg.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2mysql
            mysql-connector-java
        org.springframework.boot
            spring-boot-starter-web
        org.springframework.boot
                spring-boot-maven-plugin
            

application.yml

spring:
#  application:
#    name: service-mybatistest2
  datasource:
    # 数据库配置
    url: jdbc:mysql://192.168.1.1:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
    username: test
    password: test
    driverClassName: com.mysql.jdbc.Driver
#server:
#  port: 8081
mybatis:
  configuration:
#    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:mappers/*.xml
#eureka:
#  client:
#    serviceUrl:
#      #指向注册中心
#      defaultZone: http://192.168.111.133:8888/eureka/
#  instance:
#    # 每间隔1s,向服务端发送一次心跳,证明自己依然”存活“
#    lease-renewal-interval-in-seconds: 1
#    # 告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
#    lease-expiration-duration-in-seconds: 2

3.创建mapper

接口ISelectIdMapper.java

package com.example.mybtest.mapper;

import org.springframework.stereotype.Repository;

//@Mapper
@Repository
public interface ISelectIdMapper {
    String selectId(String name);
}

resource下userMapper.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.example.mybtest.mapper.ISelectIdMapper">
select id="selectId" resultType="String">
    select id from t_user
    where  name=#{name}
select>
mapper>

4.创建service

接口IQueryIdByName.java

package com.example.mybtest;
/**
 * @Title:
 * @Auther: test
 * @Date: 2019/6/24 17:26
 * @Version: 1.0
 * @Description:
 */
public interface IQueryIdByName {
    String queryId(String Name);
}

实现类QueryIdByNameImpl.java

package com.example.mybtest;

import com.example.mybtest.mapper.ISelectIdMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Title:
 * @Auther: test
 * @Date: 2019/6/24 17:25
 * @Version: 1.0
 * @Description:
 */
@Service
public class QueryIdByNameImpl implements IQueryIdByName {
    @Autowired
    private ISelectIdMapper selectIdMapper;

    @Override
    public String queryId(String name) {

        return selectIdMapper.selectId(name);
    }
}

5.contoller类helloController.java

package com.example.mybtest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Title:
 * @Auther: test
 * @Date: 2019/6/24 18:37
 * @Version: 1.0
 * @Description:
 */
@RestController
public class helloController {
    @Autowired
    QueryIdByNameImpl queryIdByNameImplService;


    @RequestMapping(value="/queryIdByName")
    public String queryId(String name){
        return queryIdByNameImplService.queryId(name);
    }
    @RequestMapping(value="/selectId")
    public String say(){
        String str="hello world mybatis";
        System.out.println(str);
        return str;
    }
}

6.启动类MybtestApplication

添加扫mapper

package com.example.mybtest;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.example.mybtest.mapper")
@SpringBootApplication
public class MybtestApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybtestApplication.class, args);
    }

}

7.启动测试

技术图片

技术图片

 

踩坑记:Invalid bound statement (not found)--spring boot集成mybatis

spring boot集成mybatis

标签:strong   sch   The   stdout   nbsp   epo   color   doc   err   

原文地址:https://www.cnblogs.com/pu20065226/p/11122437.html


评论


亲,登录后才可以留言!