spring boot集成mybatis
2020-12-13 04:46
标签:strong sch The stdout nbsp epo color doc err idea: file-->new-->project-->Spring Initializr一路next下去。 pom.xml application.yml 接口ISelectIdMapper.java resource下userMapper.xml 接口IQueryIdByName.java 实现类QueryIdByNameImpl.java 添加扫mapper 踩坑记: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 1.新建一个spring boot web工程
2.最终目录
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
package com.example.mybtest.mapper;
import org.springframework.stereotype.Repository;
//@Mapper
@Repository
public interface ISelectIdMapper {
String selectId(String name);
}
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
package com.example.mybtest;
/**
* @Title:
* @Auther: test
* @Date: 2019/6/24 17:26
* @Version: 1.0
* @Description:
*/
public interface IQueryIdByName {
String queryId(String Name);
}
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
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.启动测试