1.pom文件配置添加依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
2.工程目录结构
3.数据源配置
## 数据源配置
spring:
datasource:
url: jdbc:mysql:XXXXXXXXXXXXX
username: XXXXXXX
password: XXXXXXXX
driver-class-name: com.mysql.cj.jdbc.Driver
## Mybatis 配置
mybatis:
type-aliases-package: com.demo.bean
mapper-locations: classpath:mappers/*.xml
特别注意配置文件格式,好多问题都是在这里产生的。
4 代码
mapper 类
package com.demo.mapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface XxxxMapper {
List<String> xxxxxx(String tagName);
}
类上面添加注释 @Mapper
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.demo.mapper.XxxxMapper">
<select id="xxxxxx" resultType="string" >
select xxxx from xxx.xxxxxxxxxxx where xx = #{tagName}
</select>
</mapper>
springboot启动类
package com.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.demo.*"})
@MapperScan("com.demo.mapper")
public class MysqlApplication {
public static void main(String[] args) {
SpringApplication.run(MysqlApplication.class);
}
}
service类
package com.demo.service;
import com.demo.mapper.XxxxMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class XxxxxService {
@Autowired
XxxxMapper xxxxMapper;
public String xxxxxx(String tagName){
return xxxxMapper.xxxxxx(tagName).toString();
}
}
controller 类
package com.demo.controller;
import com.demo.service.XxxxxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/tag")
public class XxxxxController {
@Autowired
private XxxxxService xxxxxService;
@RequestMapping("/description")
public String xxxxxx(String tagName){
return xxxxxService.xxxxxx(tagName);
}
}
bean 里面正常写实体类就ok了。
启动后就可以正常调用了
常见问题:
问题一:
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
数据库配置文件已经配置
springboot启动类中@SpringBootApplication(xclude = {DataSourceAutoConfiguration.class})添加了注解,启动依然报错
问题解决:application.yml配置文件中数据源的配置路径不对,建议,如果不确定可以先用application.properties文件配置。比如 url 完整的地址 spring.datasource.url= 用户名地址 spring.datasource.username=。
问题二:
nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
classpath:mappers/*.xml 地址配置不对
问题三:
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
导致该问题的原因很多,主要还是要检查配置是否正确,最后问题发现是url地址不能直接访问,重新配置了一个测试地址,查询ok。