历程之Spring Boot 2:MyBatis访问数据库

最近几年接触的项目中,数据库访问中间件都不见Hibernate的踪影了,基本都是MyBatis。MyBatis相对于Hibernate来说的确方便很多,最关键的就是灵活,在绝大多数的项目当中,如果使用Hibernate将一对多或者多对一,主外键关系在数据库和代码中限制死,那么在后期的开发和测试过程中绝对是非常麻烦的事情。所以在使用MyBatis时候,我们一般将这列的数据关系限制在业务中,这样至少数据库层面上可以方便调整应对各种极端测试案例。

不细多说,搭建一个简单的Spring Boot项目,并使用MyBatis来访问数据库。首先要看的是pom.xml文件,这里能看到引入了哪些工具包。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.8.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.henry</groupId>
	<artifactId>springboot-common</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-common</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

代码中依赖的mybatis-spring-boot-starter,maven会自动下载依赖的MaBatis包。

自动生成的启动类不需要更改,看下我新建的类列表:

图中的MyConfiguration类是我为了体验@Configuration注解方式的注入添加的,在PersonServiceImpl中添加@Service的效果是一样的。

Spring Boot MyBatis整合最常见的错误就是Service调用Mapper的时候,发现Spring容器中没有注入。

@Mapper
public interface PersonMapper {

    @Select("select id id, name name, create_time createTime from t_person")
    List<Person> getAll();

}

图中的@Mapper注解可以解决这个问题。或者在启动类中使用@MapperScan主动扫描也是可行的。还是就是application.properties里配置的数据库参数可以写错:

spring.datasource.username=test
spring.datasource.password=test
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

整体来说,很快就整个好了,提现了Spring Boot在搭建项目的便利。

需要看完整代码可以访问:GitHub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值