最近一个项目需要从mysql数据库取数据,想着springboot整合mybatis没有独立做过,于是就趁着工作任务时间安排比较宽裕就干脆自己新建一个springboot+mybatis的工程,其中实体类和mapper.xml都已经通过逆向工程生成。
首先贴出项目结构
pom.xml需要引入的依赖
<dependencies>
<!--springboot启动依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mybatis整合springboot的依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--mysql数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--springboot日志-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<!--阿里的json工具-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.9</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
<!--分页PageHelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
</dependencies>
我们都知道spring的IOC是不允许我们自己创建对象的,所以对于自动生成的Mapper接口当然也需要被spring管理起来,这个时候就用到@Mapper注解
@Mapper
public interface DkMapper {
int countByExample(DkExample example);
int deleteByExample(DkExample example);
int insert(Dk record);
int insertSelective(Dk record);
List<Dk> selectByExample(DkExample example);
int updateByExampleSelective(@Param("record") Dk record, @Param("example") DkExample example);
int updateByExample(@Param("record") Dk record, @Param("example") DkExample example);
}
但是如果我们的Mapper接口有很多情况下难道每个接口都要加上注解嘛,当然不会,mybatis还提供了@MapperScan注解用于通过扫描包的方式把包下面的的所有Mapper接口都注入到Spring中,把@MapperScan注解放在SpringBoot的启动类上。
@SpringBootApplication
@Configuration
@ImportResource(locations = {"classpath:generateConfig.xml"})
@MapperScan("com.example.mybatisdemo.mapper")
public class MybatisdemoApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisdemoApplication.class, args);
}
}
因为是逆向工程生成的xxMapper.xml,所以也不用担心Mapper.xml中的名称空间与包名对不上的问题。
我用applicaiton.yml代替了application.xml,因为结构看起来更加清爽,贴出application.yml的配置
spring:
datasource:
url: jdbc:mysql://192.168.1.146:3306/dms_sourcedata
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
pagehelper:
helper-dialect: mysql
reasonable: false
support-methods-arguments: true
params: count=counsql
我看到有很多博主都配置了mybatis的属性,但是我把配置删掉好像也没有什么影响,因为在我看来这些配置的功能都通过注解完成了,小伙伴可以自己试试。
mybatis:
# 配置的实体类包扫描
type-aliases-package: com.example.mybatisdemo.bean
# 这个配置的是xxMapper.xml的路径扫描,resource下面的,这里有一个坑接下来会说到
mapper-locations:
# 这里配置的是mybatis配置文件的位置,但是mybatis和springboot整合以后好像不需要配置文件了,因为都被注解代替了
config-location:
以上内容都配置完后,还差一个数据库连接池,直接创建一个类加上@Configuration表明这是一个配置
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
@Configuration
public class DruidDBConfig {
@Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Bean
@Primary
public DataSource dataSource(){
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(this.dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
return datasource;
}
}
如果不配置这个在启动的时候回报错,但是我看很多博主在做springboot整合mybatis的时候没有写这一步,不知道是因为他们省略了还是可以不用配置,我的是因为项目报错后我配置完就成功运行了。
上述内容都配置完后就可以在service层调用Mapper来对数据库进行操作了~
然而并不行。。。在单元测试的时候项目一直报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
我知道这个问题是因为Mapper接口和对应的xml文件没有装配成功,但是该扫描的包和该配置的文件都配置了,照着网上的各路博客检查了一遍代码,整整困扰了我一整个上午,最后还是在同事的帮助下才发现是因为xml文件没有被扫描到,应该是因为我的mapper.xml在src/main/java下面,但是maven项目在编译的时候默认是不会打包java包下面的xml文件到target里面的,所以要在pom.xml中配置<resource>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
这样就可以跑起来了~