最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年04月02日 10:42:38   作者:£晝夜&  
這篇文章主要介紹了使用sts工具、SpringBoot整合mybatis的詳細(xì)步驟,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(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/庫名
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)識
@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ù)庫:

/*創(chuàng)建數(shù)據(jù)庫 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 '城市編號', `provinceId` int(10) unsigned DEFAULT NULL COMMENT '省份編號', `cityName` varchar(20) DEFAULT NULL COMMENT '城市名稱', `description` text COMMENT '城市描述', PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
/*插入測試數(shù)據(jù) */
insert into `city`(`id`,`provinceId`,`cityName`,`description`) values (1,1,'北京','這是北京市的描述信
息,北京這家伙是中國首都,百年帝都,政治經(jīng)濟(jì)文化中心,也將是世界的中心.'),(2,2,'鄭州','這是鄭
州市的描述信息,鄭州這家伙是河南省會,城市中的后起之秀,河南政治經(jīng)濟(jì)文化中心,也是中國的
中心城市.'),(3,3,'ZhengZhou','這是鄭州市的描述信息,鄭州這家伙是河南省會,城市中的后起之秀, 河南政治經(jīng)濟(jì)文化中心,也是中國的中心城市.');

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

在這里插入圖片描述

打開工具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; //城市編號
	private Integer provinceId; //省份編號
	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)目,查看。

在這里插入圖片描述

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

在這里插入圖片描述

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

相關(guān)文章

  • JAVA實(shí)現(xiàn)經(jīng)典掃雷游戲的示例代碼

    JAVA實(shí)現(xiàn)經(jīng)典掃雷游戲的示例代碼

    windows自帶的游戲《掃雷》是陪伴了無數(shù)人的經(jīng)典游戲,本程序參考《掃雷》的規(guī)則進(jìn)行了簡化,用java語言實(shí)現(xiàn),采用了swing技術(shù)進(jìn)行了界面化處理。感興趣的可以學(xué)習(xí)一下
    2022-01-01
  • Spring Boot接口設(shè)計(jì)防篡改、防重放攻擊詳解

    Spring Boot接口設(shè)計(jì)防篡改、防重放攻擊詳解

    這篇文章主要給大家介紹了關(guān)于Spring Boot接口設(shè)計(jì)防篡改、防重放攻擊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Java object wait notify notifyAll代碼解析

    Java object wait notify notifyAll代碼解析

    這篇文章主要介紹了Java object wait notify notifyAll代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Java+opencv3.2.0實(shí)現(xiàn)人臉檢測功能

    Java+opencv3.2.0實(shí)現(xiàn)人臉檢測功能

    這篇文章主要為大家詳細(xì)介紹了Java+opencv3.2.0實(shí)現(xiàn)人臉檢測功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • mybatis foreach批量插入數(shù)據(jù):Oracle與MySQL區(qū)別介紹

    mybatis foreach批量插入數(shù)據(jù):Oracle與MySQL區(qū)別介紹

    這篇文章主要介紹了,需要的朋友可以參考下
    2018-01-01
  • Java利用異常中斷當(dāng)前任務(wù)的技巧分享

    Java利用異常中斷當(dāng)前任務(wù)的技巧分享

    在日常開發(fā)中,我們經(jīng)常遇到調(diào)用別人的代碼來完成某個(gè)任務(wù),但是當(dāng)代碼比較耗時(shí)的時(shí)候,沒法從外部終止該任務(wù),所以本文為大家介紹了如何利用異常中斷當(dāng)前任務(wù),需要的可以參考下
    2023-08-08
  • Java Scanner對象中hasNext()與next()方法的使用

    Java Scanner對象中hasNext()與next()方法的使用

    這篇文章主要介紹了Java Scanner對象中hasNext()與next()方法的使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringBoot異常處理之異常顯示的頁面問題

    SpringBoot異常處理之異常顯示的頁面問題

    這篇文章主要介紹了SpringBoot異常處理異常顯示的頁面的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-09-09
  • IDEA配置JRebel實(shí)現(xiàn)熱部署的方法

    IDEA配置JRebel實(shí)現(xiàn)熱部署的方法

    這篇文章主要介紹了IDEA配置JRebel實(shí)現(xiàn)熱部署的方法,本文給大家介紹的非常想詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • Java基礎(chǔ)語法:邏輯控制

    Java基礎(chǔ)語法:邏輯控制

    下面小編就為大家?guī)硪黄狫ava邏輯控制的基礎(chǔ)文章。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2021-08-08

最新評論

清徐县| 公安县| 襄城县| 南召县| 阳曲县| 时尚| 华亭县| 常德市| 宁远县| 措勤县| 安泽县| 黄冈市| 岳池县| 呈贡县| 眉山市| 平江县| 扬州市| 肥城市| 张家港市| 高州市| 淮阳县| 鸡西市| 呼图壁县| 遵义县| 长丰县| 临潭县| 剑阁县| 乡宁县| 莱芜市| 宁强县| 龙州县| 农安县| 抚宁县| 台安县| 肇庆市| 仲巴县| 东丰县| 公安县| 永川市| 成安县| 石景山区|