SpringBoot学习笔记5-SpringBoot 集成 MyBatis

【Android免费音乐下载app】【佳语音乐下载】建议最少2.0.3版本。最新版本:
https://gitlab.com/gaopinqiang/checkversion/raw/master/Music_Download.apk

Spring boot 集成 MyBatis的步骤如下:
1、在pom.xml中配置相关jar依赖;

<!--加载mybatis整合springboot -->
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.1</version>
</dependency>

<!-- MySQL的jdbc驱动包 -->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>

2、在Springboot的核心配置文件 application.properties 中配置MyBatis的Mapper.xml文件所在位置:
如果xml在resources目录下,且包名和java中的mapper接口的包名一样,不配置也可以

mybatis.mapper-locations=classpath:com/example/springboot/mapper/*.xml

3、在Springboot的核心配置文件application.properties中配置数据源:
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver #这个地方新版本会提示过期需要使用com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/study?useUnicode=true&characterEncoding=utf8&useSSL=false #这个地方也会报错,需要加 &serverTimezone=GMT%2b8 属性

4、在MyBatis的Mapper接口中添加@Mapper注解 或者
在运行的主类上添加 @MapperScan(“com.example.springboot.mapper”) 注解包扫描

==========================================
在Idea中实际演示一遍,有些地方的配置还需要注意下。
1、在pom.xml中配置相关jar依赖;具体的依赖参考上面

2、在Springboot的核心配置文件 application.properties 中配置MyBatis的Mapper.xml文件所在位置:

	#如果mapper.xml的路径和java代码中的mapper接口路径不一致,就需要配置
	# 例如:java代码路径:com.example.mybatis.mapper,xml在资源(resources)文件的路径:com.example.mybatis.xml。
	#编译的时候会自动在/target/classes/com/example/mybatis/xml/下存放对应的xml
	mybatis.mapper-locations=classpath:com/example/mybatis/xml/*.xml
#如果mapper的xml文件和mapper接口文件放在一个文件夹下,就不需要配置mybatis.mapper-locations 了,但是需要在pom.xml的build中配置resources,否则编译的时候xml拷贝不到target中
	<!--如果mapper.xml文件不是放在resources文件夹下,没有配置下面这个,编译的时候xml就不会在target中生成,所以都配置下-->
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
			</resource>

			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.*</include>
				</includes>
			</resource>

			<resource>
				<directory>src/main/webapp</directory>
				<targetPath>META-INF/resources</targetPath>
				<includes>
					<include>**/*.*</include>
				</includes>
			</resource>
		</resources>

3、在Springboot的核心配置文件application.properties中配置数据源:

#需要使用com.mysql.cj.jdbc.Driver,否则会报一个错误
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#必须要加 &serverTimezone=GMT%2b8 ,否则会连接不上数据库(You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property))
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/study?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&serverTimezone=GMT%2b8
spring.datasource.username = root
spring.datasource.password = root

4、编写对应的 Mapper 、 Service 、 Controller
创建com.example.mybatis.mapper包,创建StudentMapper.java

package com.example.mybatis.mapper;

import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Map;

@Mapper //这里设置这个注解,在application中就不用@MapperScan扫描了。(推荐使用全局的MapperScan扫描)
public interface StudentMapper {
	List<Map<String, Object>> getStudent(String id);
}
	

创建com.example.mybatis.service包,创建StudentService接口

package com.example.mybatis.service;

import java.util.List;
import java.util.Map;

public interface StudentService {
	List<Map<String, Object>> getStudent(String id);
}

创建com.example.mybatis.service.impl包,创建StudentServiceImpl.java

package com.example.mybatis.service.impl;

import com.example.mybatis.mapper.StudentMapper;
import com.example.mybatis.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service //使用注解标记
public class StudentServiceImpl implements StudentService {

	@Autowired //自动注入Mapper
	private StudentMapper studentMapper;

	@Override
	public List<Map<String, Object>> getStudent(String id) {
		return studentMapper.getStudent("123");
	}
}

创建com.example.mybatis.controller包,创建StudentController.java

package com.example.mybatis.controller;

import com.example.mybatis.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class StudentController {
	@Autowired
	private StudentService studentService;

	@RequestMapping("/getStudent")
	public @ResponseBody String getStudent(){
		List<Map<String, Object>> student = studentService.getStudent("100");
		System.out.println("查询数据库");
		return student.toString();
	}

}

在resources目录下创建包com.example.mybatis.xml(注意这里和java中的mapper路径是不一样的,这里是演示故意设置成不一样),创建StudentMapper.xml

这个StudentMapper.xml名称可以修改,但是namespace中对应的com.example.mybatis.mapper.StudentMapper一定要和StudentMapper接口对应上

<?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.mybatis.mapper.StudentMapper"><!--对应的是Mapper接口-->
	<select id="getStudent" resultType="java.util.HashMap">
		select 
			*
		from
			t_person
	</select>

</mapper>

Application代码,未做配置(我们在Mapper的接口上加了@Mapper注解,所以不用配置xml扫描)

package com.example.mybatis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

application.properties完整配置:

#配置服务器端口
server.port=8080

#如果mapper.xml的路径和java代码中的mapper接口路径不一致,就需要配置
# 例如:java代码路径:com.example.mybatis.mapper,xml在资源(resources)文件的路径:com.example.mybatis.xml。
#编译的时候会自动在/target/classes/com/example/mybatis/xml/下存放对应的xml
mybatis.mapper-locations=classpath:com/example/mybatis/xml/*.xml

#如果mapper的xml文件和接口文件放在一个文件夹下,就不需要配置mybatis.mapper-locations 了,但是需要在pom.xml的build中配置resources,否则编译的时候xml拷贝不到target中



#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.datasource.url = jdbc:mysql://127.0.0.1:3306/wmos_database?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&serverTimezone=GMT%2b8
#需要使用com.mysql.cj.jdbc.Driver,否则会报一个错误

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#必须要加 &serverTimezone=GMT%2b8 ,否则会连接不上数据库(You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property))
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/study?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&serverTimezone=GMT%2b8
spring.datasource.username = root
spring.datasource.password = root

数据库数据:
在这里插入图片描述

测试:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值