SpringBoot下整合Mybatis 入门Demo

1.添加依赖

创建好Spring Starter Project项目后,找到项目的pom.xml文件,添加以下三个依赖:JDBC API、MySQL Driver、MyBatis Framework。

在这里插入图片描述
JDBC API依赖:

<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
</dependency>

MySQL Driver依赖:

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

MyBatis依赖:(以2.1.3版本为例)

<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.3</version>
</dependency>

2.配置数据库连接池

在src/main/resources目录下的application.properties文件添加以下配置。
需要注意的是url中localhost:3306可以省略不写,?前面是要连接的数据库的名字。logging.level.com.av=debug:打印com.av包下的日志,debug为日志级别。

#spring datasource
spring.datasource.url=jdbc:mysql://localhost:3306/dbgoods?serverTimezone=GMT%2B8&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root

#spring logging  
logging.level.com.av=debug

3.测试数据库连接

在src/test/java目录下建一个测试类,这个类必须在主启动类或其子包下,具体测试代码如下:

@SpringBootTest
public class DatasourceTest {
	@Autowired
	private DataSource dataSource;//HikariDataSource

	@Test
	void testConection() throws SQLException {
		System.out.println(dataSource.getClass().getName());
		System.out.println("=====================");
		System.out.println(dataSource.getConnection());
	}
}

通过测试,我们知道spring是通过DataSource的实现类HikariDataSource进行连接的。

4.简易配置MyBatis(暂时可不配置)

在application.properties文件中进行基本配置(可选,暂时可以不配置)。springboot会自动加载mybatis.configuration.*的相关配置。
例如:1.设置全局的sql执行超时时间(单位s)
2.将带有下划线的表字段映射为驼峰格式的实体类属性。

mybatis.configuration.default-statement-timeout=30
mybatis.configuration.map-underscore-to-camel-case=true

5.Demo的实现(删除数据)

具体步骤:
5.1 在主启动类的子包之下,创建GoodsDao接口。
@Param注解在新版本可以不要,但是最好还是加上。

@Mapper
public interface GoodsDao {
	int deleteByIds(@Param("ids")Integer...ids);
	
	@Delete("delete from tb_goods where id=#{id}")
	int deleteById(Integer i);
}

5.2 在src/main/resources目录下创建mapper/goods目录,然后在其目录中添加GoodsMapper.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.av.pj.dao.GoodsDao">
  	<delete id="deleteByIds">
  		delete from tb_goods
  		where id in           <!-- 拼接成    (1,2,3,4)  这种形式 -->
  		<foreach collection="ids" 
  				 open="(" close=")" separator=","	
  				 item="id">
  			#{id}
  		</foreach>
  	</delete>
  </mapper>

5.3 application.properties文件下添加映射文件路径的配置,路径为:mapper文件夹下第二级的所有xml文件。

# spring mybatis
mybatis.mapper-locations=classpath:/mapper/*/*.xml

5.4 测试结果
在src/test/java下创建一个测试类,测试代码如下

@SpringBootTest
public class GoodsDaoTest {
	@Autowired
	private GoodsDao goodsDao;
	@Test
	void deleteById() {
		int row = goodsDao.deleteById(1);
		System.out.println("row:"+row);
		//com.sun.proxy.$Proxy56
		System.out.println(goodsDao.getClass().getName());
	}
	@Test
	void deleteByIds() {//删除id为3,4,5的
		int rows = goodsDao.deleteByIds(3,4,5);
		System.out.println("rows:"+rows);
	}
}

附:mybatis官网:https://mybatis.org/mybatis-3/zh/index.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值