黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專(zhuān)注Java教育14年 全國(guó)咨詢(xún)/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁(yè) hot資訊 SpringBoot整合Mybatis

SpringBoot整合Mybatis

更新時(shí)間:2021-06-24 16:27:26 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1236次

之前搭傳統(tǒng)的ssm框架,配置文件很多,看了幾天文檔才把那些xml的邏輯關(guān)系搞得七七八八,搭起來(lái)也是很麻煩,那時(shí)我完全按網(wǎng)上那個(gè)demo的版本要求(jdk和tomcat),所以最后是各種問(wèn)題沒(méi)成功跑起來(lái)。今天嘗試用springboot框架來(lái)整合,不敢相信才失敗幾次就成功了!!

現(xiàn)在來(lái)記錄一下過(guò)程:

添加POM依賴(lài):

<dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-thymeleaf</artifactId>  
    </dependency> 
        
    <dependency>  
            <groupId>mysql</groupId>  
            <artifactId>mysql-connector-java</artifactId>  
    </dependency>  
        
    <dependency>  
            <groupId>org.mybatis.spring.boot</groupId>  
            <artifactId>mybatis-spring-boot-starter</artifactId>  
            <version>1.3.0</version>  
    </dependency>

關(guān)于dao層的依賴(lài)就是后面兩個(gè)mysql的connector和mybatis的starter(這個(gè)可是好東西)

開(kāi)始寫(xiě)代碼:

這里我們有個(gè)簡(jiǎn)單是數(shù)據(jù)表:

springboot整合mybatis

我們的目的是用get發(fā)一個(gè)帶有id值的請(qǐng)求,服務(wù)器根據(jù)id值返回這個(gè)圖書(shū)管理員的全部信息并用json的方式直接顯示在頁(yè)面上,

Controller:

@RestController
public class LibrarianController {
    
    @Autowired
    private LibrarianService librarianService;
    
    @GetMapping("/getLibrarian")
    public Librarian getALibrarianInfo(int id) {
        //System.out.println("test :id: "+id);
        return librarianService.selectLibrarian(id);
    }
}

RestController是responsebody+Controller兩個(gè)注解的合體,一般就拿來(lái)直接傳json數(shù)據(jù)。為什么可以直接傳個(gè)對(duì)象過(guò)去呢?這是因?yàn)閟pringboot內(nèi)置了jackson模塊,可以在maven的依賴(lài)下看到這方面的jar包(之前我寫(xiě)是按網(wǎng)上的教程用gson來(lái)處理的,比起來(lái)這個(gè)簡(jiǎn)直無(wú)敵)

然后看到我們注入的sevice,下面是service

Service:

public interface LibrarianService {
    Librarian selectLibrarian(int id);
}

就是個(gè)接口

ServiceImpl:

@Service
public class LibrarianServiceImpl implements LibrarianService{
    
    @Autowired
    private LibrarianMapper librarianMapper;
    
    @Override
    public Librarian selectLibrarian(int id) {
        // TODO Auto-generated method stub
        return librarianMapper.selectLibrarian(id);
    }
}

這里記得要加 Service備注,才會(huì)被spring生成bean然后注入到controller那里去。

然后看到這里注入了個(gè)mapper

Dao:

package com.example.dao;


import org.apache.ibatis.annotations.Mapper;

import com.example.entity.Librarian;

@Mapper
public interface LibrarianMapper {
    Librarian selectLibrarian(int id);
}

這里加的 Mapper是MyBatis的備注,目的是為了讓spring能夠根據(jù)xml和這個(gè)接口動(dòng)態(tài)生成這個(gè)接口的實(shí)現(xiàn)。如果是加 Repository,就是spring生成一個(gè)bean,自動(dòng)注入service的相關(guān)引用中。

然后是Mapper的xml文件(我的MyBatis的相關(guān)sql用的是xml)

mapper.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.example.dao.LibrarianMapper">

    <!-- 可根據(jù)自己的需求,是否要使用 -->
    <resultMap type="Librarian" id="LibrarianMap">

        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="userName" property="useName" jdbcType="VARCHAR" />
        <result column="password" property="password" jdbcType="VARCHAR" />
        <result column="age" property="age" jdbcType="INTEGER" />
        <result column="position" property="position" jdbcType="VARCHAR" />
        
    </resultMap>
    
    

    <select id="selectLibrarian" parameterType="INTEGER" resultMap="LibrarianMap">
        select *
        from t_librarian 
        where 1=1
            and id = #{id,jdbcType=INTEGER}
    </select>
    
    

</mapper>

第三行的namespace很重要噢,它是指定這個(gè)xml所對(duì)應(yīng)的是哪個(gè)dao(mapper)接口

resultMap是做個(gè)pojo和數(shù)據(jù)庫(kù)表的對(duì)應(yīng)(這里只寫(xiě)個(gè)Librarian不用寫(xiě)全路徑是因?yàn)樽隽嗽O(shè)置,下面會(huì)說(shuō))

select標(biāo)簽的id對(duì)應(yīng)的就是mapper接口中的方法名,parameterType就是傳進(jìn)來(lái)的參數(shù)類(lèi)型

簡(jiǎn)單的配置與設(shè)置:

好現(xiàn)在講講設(shè)置,這里會(huì)想到,那些Controller啊, Service啊還有MyBatis的注解 Mapper什么的spring怎么知道在哪呢?不用像mvc那樣搞個(gè)掃描設(shè)置嗎?

是的要的,這些我們?cè)趩?dòng)類(lèi)做了簡(jiǎn)單的設(shè)置:

package com.example.main;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;



/**
 * 指定所掃的包,會(huì)自動(dòng)掃描指定包下的全部標(biāo)有@Component的類(lèi),并注冊(cè)成bean,
 * 當(dāng)然包括@Component下的子注解@Service,@Repository,@Controller。
 * @author 85060
 *
 */
@SpringBootApplication(scanBasePackages={"com.example.*"})
@MapperScan("com.example.dao")
public class SpringBootDemoApplication {

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

關(guān)于這個(gè)SpringBootApplication,它其實(shí)是好幾個(gè)注解的合體,所以可以直接在這寫(xiě)掃包的設(shè)置,然后那個(gè) MapperScan是MyBatis提供的設(shè)置注解。

然后還有點(diǎn)配置文件:

spring.thymeleaf.cache=false        
spring.devtools.restart.enabled=true   
spring.devtools.restart.additional-paths=src/main/java  

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db_library
spring.datasource.username=root
spring.datasource.password=4008

mybatis.type-aliases-package=com.example.entity
mybatis.mapperLocations=classpath:mappers/*.xml

主要有關(guān)的是第四行開(kāi)始的,最后兩行一個(gè)是設(shè)置基本包(包別名)也就是為什么在mapper.xml中可以只寫(xiě)一個(gè)Librarian的原因。

最后一行的很重要,是告訴系統(tǒng)在哪里去找mapper.xml文件。

上一個(gè)最后的效果圖:

springboot整合mybatis

再放一個(gè)項(xiàng)目結(jié)構(gòu)圖:

springboot整合mybatis

以上就是動(dòng)力節(jié)點(diǎn)小編介紹的"springboot整合mybatis",希望對(duì)大家有幫助,想了解更多可查看SpringBoot教程技術(shù)文檔,如有疑問(wèn),請(qǐng)?jiān)诰€咨詢(xún),有專(zhuān)業(yè)老師隨時(shí)為您服務(wù)。

提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 日韩中文字幕视频在线观看 | 久久精品一区二区三区不卡牛牛 | 韩国理伦伦片在线观看 | 国产成年人网站 | 国产白丝在线观看 | 国产一区国产二区国产三区 | 国产中文久久精品 | 午夜18免费观看 | 亚洲国产精品视频 | 97色伦图片在线观看 | 97人人爽 | 欧美视频导航 | 日本aⅴ网站 | 18一20岁一级毛片 | 久久免费视频一区 | 亚洲女人国产香蕉久久精品 | 爱爱毛片 | 亚洲午夜高清 | 国产精品无卡无在线播放 | 性欧美巨大的视频 | 亚洲精选在线 | 日韩不卡视频在线 | 未成18年禁止观看的免费 | 国产一级片在线播放 | 日韩免费视频观看 | 亚洲精品第四页中文字幕 | 成年网站视频在线观看 | 看a级毛片 | 日韩亚洲第一页 | 岛国在线最新 | 日本天堂在线 | 免费永久观看美女视频网站网址 | 日b视频在线观看 | 午夜视频在线免费观看 | 黄色一级免费看 | 麻豆xfplay国产在线观看 | 99riav国产在线观看 | 又爽又大又光又色的午夜视频 | 黄频大全 | 日韩欧美精品中文字幕 | 日韩欧美国产成人 |