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

數據庫數據:
在這裏插入圖片描述

測試:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章