今天就跟大家聊聊有關(guān)使用Spring Boot如何對(duì)Mybatis進(jìn)行整合,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
資中網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、自適應(yīng)網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。成都創(chuàng)新互聯(lián)公司2013年開(kāi)創(chuàng)至今到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。依賴配置
結(jié)合前面的內(nèi)容,這里我們要嵌入數(shù)據(jù)庫(kù)的操作,這里以操作MySQL為例整合Mybatis,首先需要在原來(lái)的基礎(chǔ)上添加以下依賴
org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1
當(dāng)然啦,只依賴mybatis是不夠的還需要依賴jdbc驅(qū)動(dòng)以及返回json數(shù)據(jù)的json庫(kù)(格式化數(shù)據(jù))
org.apache.tomcat tomcat-jdbc mysql mysql-connector-java 5.1.21 com.alibaba fastjson 1.1.43
應(yīng)用配置
接著需要在application.properties中添加數(shù)據(jù)庫(kù)配置
#JDBC配置 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/weibo?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver
這里連接的數(shù)據(jù)庫(kù)名叫weibo,其中表結(jié)構(gòu)如下:
接下來(lái)就需要來(lái)具體的初始化MyBatis配置以及數(shù)據(jù)表的操作了,先看一下工程結(jié)構(gòu)
編寫(xiě)配置類
數(shù)據(jù)庫(kù)相關(guān)的DataSource,SqlSeesion配置,其中DataSourse的配置可以理解為解讀application.properties中的jdbc相關(guān)配置然后初始化JDBC驅(qū)動(dòng)的,SqlSeesion配置主要是針對(duì)Mybatis使用事務(wù)操作時(shí)的配置信息。
package com.example.demo.config; import javax.sql.DataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; @EnableAutoConfiguration @ComponentScan @MapperScan("com.example.demo.mapper") public class JdbcConfig { // DataSource配置 @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return new org.apache.tomcat.jdbc.pool.DataSource(); } // 提供SqlSeesion(數(shù)據(jù)庫(kù)事務(wù)操作相關(guān)的配置) @Bean public SqlSessionFactory sqlSessionFactoryBean() throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml")); return sqlSessionFactoryBean.getObject(); } @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); } }
添加Pojo類
該類主要接收數(shù)據(jù)表中的數(shù)據(jù),所以屬性基本上跟數(shù)據(jù)表的屬性一致
package com.example.demo.bean; public class Diary { private int id; private String title; private String content; private String pubTime; private int userId; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getPubTime() { return pubTime; } public void setPubTime(String pubTime) { this.pubTime = pubTime; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } @Override public String toString() { return "Diary [id=" + id + ", title=" + title + ", content=" + content + ", pubTime=" + pubTime + ", userId=" + userId + "]"; } }
添加數(shù)據(jù)表操作接口
該類描述了操作數(shù)據(jù)表的過(guò)程,SprintBoot在運(yùn)行中會(huì)根據(jù)類上的@Mapper注解找到它,因此不能落下這個(gè)注解
package com.example.demo.mapper; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import com.example.demo.bean.Diary; @Mapper public interface DiaryMapper { @Select("select * from diary where _id = #{id}") public Diary getDiaryById(@Param("id")Integer id); }
使用
package com.example.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.alibaba.fastjson.JSON; import com.example.demo.bean.Diary; import com.example.demo.mapper.DiaryMapper; @RestController public class DiaryMappingController { @Autowired DiaryMapper diaryMapper; @RequestMapping("/diary") public String getDiary(Integer id){ Diary d = diaryMapper.getDiaryById(id); String json = JSON.toJSONString(d); return json; } }
最后運(yùn)行SpringBoot項(xiàng)目,然后在瀏覽器上輸入網(wǎng)址:
http://localhost:8080/diary?id=2
這樣即可看到結(jié)果
看完上述內(nèi)容,你們對(duì)使用Spring Boot如何對(duì)Mybatis進(jìn)行整合有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。