欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用sts工具、SpringBoot整合mybatis的詳細(xì)步驟

 更新時(shí)間:2021年04月02日 10:42:38   作者:£晝夜&  
這篇文章主要介紹了使用sts工具、SpringBoot整合mybatis的詳細(xì)步驟,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

SpringBoot 集成 Mybatis 框架

一、1、SpringBoot 集成 Mybatis 的基本步驟

第一步:添加依賴;
第二步:配置數(shù)據(jù)源;
第三步:掃描接口包。

二、詳細(xì)的集成步驟如下:

1.第一步:添加依賴;

添加依賴;除了常規(guī)依賴外,需要加入 Mybatis
代碼如下(示例):

<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ysd.springboot</groupId>
<artifactId>spring-boot-mybatis</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-mybatis</name>
<url>http://maven.apache.org</url>
<!-- Spring Boot 啟動(dòng)父依賴 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<properties>
<!-- 項(xiàng)目設(shè)置:編碼格式 UTF-8 及 springboot 相關(guān)版本 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
<mysql-connector>5.1.39</mysql-connector>
<druid>1.0.18</druid>
</properties>
<dependencies>
<!-- Spring Boot SpringMVC 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Mybatis 依賴 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot}</version>
</dependency>
<!-- MySQL 連接驅(qū)動(dòng)依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector}</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- SpringBoot 插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!-- SpringBoot 項(xiàng)目打包名稱 -->
<finalName>springmybatis</finalName>
</build>
</project>

2.第二步:配置數(shù)據(jù)源;

在 application.properties 配置文件中,配置數(shù)據(jù)源、Mybatis 的配置及映射文件。

代碼如下(示例):

## 數(shù)據(jù)源配置
spring.datasource.url=jdbc:mysql://localhost:3306/庫(kù)名
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
## Mybatis 配置
# 實(shí)體所在包,起別名
mybatis.typeAliasesPackage=org.spring.springboot.domain
# 映射文件所在路徑
mybatis.mapperLocations=classpath:mapper/*.xml

3.第三步:掃描接口包。

在主模塊上注解掃描接口包,使用@MapperScan(“包名”)。

代碼如下(示例):

@SpringBootApplication // Spring Boot 應(yīng)用的標(biāo)識(shí)
@MapperScan("org.spring.springboot.dao") // mapper 接口類掃描包配置
//如果要顯示 Sql 細(xì)節(jié)還需要在 logback 配置<logger name="接口類所在包" level="debug" />
public class Application {
public static void main(String[] args) {
// 程序啟動(dòng)入口
// 啟動(dòng)嵌入式的 Tomcat 并初始化 Spring 環(huán)境及其各 Spring 組件
SpringApplication.run(Application.class,args);
}
}

實(shí)驗(yàn):
參考的數(shù)據(jù)庫(kù):

/*創(chuàng)建數(shù)據(jù)庫(kù) springbootdb*/
CREATE DATABASE /*!32312 IF NOT EXISTS*/`springbootdb` /*!40100 DEFAULT CHARACTER SET utf8
*/;
USE `springbootdb`;
/*創(chuàng)建表 city*/
DROP TABLE IF EXISTS `city`;
CREATE TABLE `city` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '城市編號(hào)', `provinceId` int(10) unsigned DEFAULT NULL COMMENT '省份編號(hào)', `cityName` varchar(20) DEFAULT NULL COMMENT '城市名稱', `description` text COMMENT '城市描述', PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
/*插入測(cè)試數(shù)據(jù) */
insert into `city`(`id`,`provinceId`,`cityName`,`description`) values (1,1,'北京','這是北京市的描述信
息,北京這家伙是中國(guó)首都,百年帝都,政治經(jīng)濟(jì)文化中心,也將是世界的中心.'),(2,2,'鄭州','這是鄭
州市的描述信息,鄭州這家伙是河南省會(huì),城市中的后起之秀,河南政治經(jīng)濟(jì)文化中心,也是中國(guó)的
中心城市.'),(3,3,'ZhengZhou','這是鄭州市的描述信息,鄭州這家伙是河南省會(huì),城市中的后起之秀, 河南政治經(jīng)濟(jì)文化中心,也是中國(guó)的中心城市.');

運(yùn)行結(jié)果:

在這里插入圖片描述

打開(kāi)工具sts,新建一個(gè)項(xiàng)目、具體如下。

在這里插入圖片描述

點(diǎn)擊下一步

在這里插入圖片描述

點(diǎn)擊finish結(jié)束。

在這里插入圖片描述

回看第一步.

1、添加依賴

查看pom.xml中是否有

在這里插入圖片描述

第二步,配置數(shù)據(jù)源

在 application.properties 配置文件中,配置數(shù)據(jù)源、Mybatis 的配置及映射文件。

在這里插入圖片描述

在src/main/resources目錄下新建包Mapper,在官網(wǎng)搜mybatis入門復(fù)制如下代碼。

<?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="org.mybatis.example.BlogMapper">
 <select id="selectBlog" resultType="Blog">
 select * from Blog where id = #{id}
 </select>
</mapper>

上述項(xiàng)目中建entity、dao、controller.代碼如下。
entity

package com.zha.entity;

public class City {
	private Integer id; //城市編號(hào)
	private Integer provinceId; //省份編號(hào)
	private String cityName;//城市名稱
	private String description;//城市描述
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getProvinceId() {
		return provinceId;
	}
	public void setProvinceId(Integer provinceId) {
		this.provinceId = provinceId;
	}
	public String getCityName() {
		return cityName;
	}
	public void setCityName(String cityName) {
		this.cityName = cityName;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	@Override
	public String toString() {
		return "City [id=" + id + ", provinceId=" + provinceId + ", cityName=" + cityName + ", description="
				+ description + "]";
	}
	public City(Integer id, Integer provinceId, String cityName, String description) {
		super();
		this.id = id;
		this.provinceId = provinceId;
		this.cityName = cityName;
		this.description = description;
	}
	public City() {
		super();
	}
	

}

dao

package com.zha.dao;

import java.util.List;

import com.zha.entity.City;

public interface CityDao {
	public List<City> getAll();
}

controller

package com.zha.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zha.dao.CityDao;
@RestController
public class helloController {
	@Autowired
	CityDao cityDao;
	@RequestMapping("/hello")
	public String hello() {
		return ""+cityDao.getAll();
		
	}

}

第三步

在主模塊上注解掃描接口包,使用@MapperScan(“包名”)。

在這里插入圖片描述

然后在src/main/resources目錄下新建包Mapper,在官網(wǎng)搜mybatis入門復(fù)制如下代碼,換成自己的。

<?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.zha.dao.CityDao">
	<select id="getAll" resultType="com.zha.entity.City">
		SELECT
		city.id,
		city.provinceId,
		city.cityName,
		city.description
		FROM
		city
	</select>
</mapper>

運(yùn)行項(xiàng)目,查看。

在這里插入圖片描述

看到這里就說(shuō)明已經(jīng)成功了,在網(wǎng)頁(yè)輸入http://localhost:8080/hello,按回車鍵。

在這里插入圖片描述

到此這篇關(guān)于使用sts工具、SpringBoot整合mybatis的詳細(xì)步驟的文章就介紹到這了,更多相關(guān)sts SpringBoot整合mybatis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot+Vue靜態(tài)資源刷新后無(wú)法訪問(wèn)的問(wèn)題解決方案

    SpringBoot+Vue靜態(tài)資源刷新后無(wú)法訪問(wèn)的問(wèn)題解決方案

    這篇文章主要介紹了SpringBoot+Vue靜態(tài)資源刷新后無(wú)法訪問(wèn)的問(wèn)題解決方案,文中通過(guò)代碼示例和圖文講解的非常詳細(xì),對(duì)大家解決問(wèn)題有一定的幫助,需要的朋友可以參考下
    2024-05-05
  • 基于SpringBoot應(yīng)用監(jiān)控Actuator安全隱患及解決方式

    基于SpringBoot應(yīng)用監(jiān)控Actuator安全隱患及解決方式

    這篇文章主要介紹了SpringBoot應(yīng)用監(jiān)控Actuator安全隱患及解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 詳解java中各類鎖的機(jī)制

    詳解java中各類鎖的機(jī)制

    這篇文章為大家總結(jié)了java中常見(jiàn)的鎖(互斥鎖、讀寫(xiě)鎖、公平鎖與非公平鎖等)的機(jī)制以及如何使用,文中示例代碼講解詳細(xì),需要的可以學(xué)習(xí)一下
    2021-12-12
  • Springboot ApplicationRunner的使用解讀

    Springboot ApplicationRunner的使用解讀

    這篇文章主要介紹了Springboot ApplicationRunner的使用解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • SpringBoot配置多數(shù)據(jù)源的四種方式分享

    SpringBoot配置多數(shù)據(jù)源的四種方式分享

    在日常開(kāi)發(fā)中我們都是以單個(gè)數(shù)據(jù)庫(kù)進(jìn)行開(kāi)發(fā),在小型項(xiàng)目中是完全能夠滿足需求的,但是,當(dāng)我們牽扯到大型項(xiàng)目的時(shí)候,單個(gè)數(shù)據(jù)庫(kù)就難以承受用戶的CRUD操作,那么此時(shí),我們就需要使用多個(gè)數(shù)據(jù)源進(jìn)行讀寫(xiě)分離的操作,本文就給大家介紹SpringBoot配置多數(shù)據(jù)源的方式
    2023-07-07
  • IDEA插件開(kāi)發(fā)之環(huán)境搭建過(guò)程圖文詳解

    IDEA插件開(kāi)發(fā)之環(huán)境搭建過(guò)程圖文詳解

    這篇文章主要介紹了IDEA插件開(kāi)發(fā)之環(huán)境搭建過(guò)程,本文通過(guò)圖文并茂實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Spring Boot中如何使用Convert接口實(shí)現(xiàn)類型轉(zhuǎn)換器

    Spring Boot中如何使用Convert接口實(shí)現(xiàn)類型轉(zhuǎn)換器

    這篇文章主要介紹了Spring Boot中使用Convert接口實(shí)現(xiàn)類型轉(zhuǎn)換器的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • JVM中ClassLoader類加載器的深入理解

    JVM中ClassLoader類加載器的深入理解

    這篇文章主要給大家介紹了關(guān)于JVM中ClassLoader類加載器的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • JAVA中Context的詳細(xì)介紹和實(shí)例分析

    JAVA中Context的詳細(xì)介紹和實(shí)例分析

    這篇文章主要介紹了JAVA中Context的詳細(xì)介紹和實(shí)例分析,Context是維持android各組件能夠正常工作的一個(gè)核心功能類。如果感興趣來(lái)學(xué)習(xí)一下
    2020-07-07
  • ScheduledThreadPoolExecutor巨坑解決

    ScheduledThreadPoolExecutor巨坑解決

    這篇文章主要為大家介紹了使用ScheduledThreadPoolExecutor遇到的巨坑解決示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02

最新評(píng)論