您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關springboot+springmvc+mybatis項目整合的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
特點:
1. 創建獨立的Spring應用程序
2. 嵌入的Tomcat,無需部署WAR文件
3. 簡化Maven配置
4. 自動配置Spring
5. 提供生產就緒型功能,如指標,健康檢查和外部配置
6. 絕對沒有代碼生成和對XML沒有要求配置
(以上大部分內容摘自百度百科)
ok,關于springboot的講解就到這里大家想了解的更詳細可以自行百度。
搭建springboot項目我推薦大家用idea或者sts(spring tool suite spring公司自己研發的一款編輯器),我現在用的是idea,所以接下來我是用idea搭建項目的
一、創建項目
填寫完Group和Atrifact后點擊下一步,這里我選擇的是jar,因為官方文檔推薦的是打包成JAR,所以這里就不過多解釋了。
這里勾選上web
這里勾選上MySQL、JDBC和Mybatis點擊下一步
這里輸入項目名和項目路徑后點擊完成
這是新建完成后的項目結構
二、添加pom.xml依賴
因為springboot不推薦使用jsp做頁面,如果想要使用就必需的添加jsp的依賴
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.chaoqi</groupId> <artifactId>springboot_demo2</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot_demo2</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--添加jsp依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
三、springboot整合springmvc
因為在創建項目的時候我們選擇了mybatis和jdbc所以在這里也要把他們兩也給配置進去
編輯application.properties
# 頁面默認前綴目錄 spring.mvc.view.prefix=/WEB-INF/jsp/ # 響應頁面默認后綴 spring.mvc.view.suffix=.jsp #開發配置 spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 spring.datasource.username = root spring.datasource.password = 123456 # mybatis接口文件位置 mybatis.mapper-locations: classpath:mapper/*.xml mybatis.type-aliases-package: com.chaoqi.springboot_demo2.domain
如果大家習慣用application.yml那也可以用,但是用application.yml在第一次啟動項目的時候一定要maven clean一下,不然會報錯。
server: port: 8080 spring: mvc: view: prefix: /WEB-INF/jsp/ suffix: .jsp datasource: url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&useUnicode=true&useSSL=false username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver mybatis: mapper-locations: classpath:mapping/*.xml type-aliases-package: com.chaoqi.springboot_demo2.domain
編輯完application.properties之后再src/mian下創建webapp目錄,結構如下
新建IndexController
package com.chaoqi.springboot_test.web; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class IndexController { private static final String INDEX = "index"; @RequestMapping("/show") public String getIndex() { return INDEX; } }
運行main函數
訪問頁面,成功
四、springboot整合mybatis
創建數據庫表
-- ---------------------------- -- Table structure for music_info -- ---------------------------- DROP TABLE IF EXISTS `music_info`; CREATE TABLE `music_info` ( `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主鍵id', `singer_name` varchar(100) NOT NULL COMMENT '歌手名', `music_size` varchar(100) NOT NULL COMMENT '歌曲大小', `music_name` varchar(100) NOT NULL COMMENT '歌曲名', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of music_info -- ---------------------------- INSERT INTO `music_info` VALUES ('1', '小三', '3.2M', '起風了'); INSERT INTO `music_info` VALUES ('2', '劉德華', '3.0M', '忘情水'); INSERT INTO `music_info` VALUES ('3', '豬點點', '5.0M', '會寫程序的小豬');
創建pojo
package com.chaoqi.springboot_test.dao.domain; public class MusicInfo { // 主鍵id private Integer id; // 歌手名 private String singerName; // 歌曲大小 private String musicSize; // 歌曲名 private String musicName; /** * 獲取 主鍵id music_info.id * * @return 主鍵id */ public Integer getId() { return id; } /** * 設置 主鍵id music_info.id * * @param id 主鍵id */ public void setId(Integer id) { this.id = id; } /** * 獲取 歌手名 music_info.singer_name * * @return 歌手名 */ public String getSingerName() { return singerName; } /** * 設置 歌手名 music_info.singer_name * * @param singerName 歌手名 */ public void setSingerName(String singerName) { this.singerName = singerName == null ? null : singerName.trim(); } /** * 獲取 歌曲大小 music_info.music_size * * @return 歌曲大小 */ public String getMusicSize() { return musicSize; } /** * 設置 歌曲大小 music_info.music_size * * @param musicSize 歌曲大小 */ public void setMusicSize(String musicSize) { this.musicSize = musicSize == null ? null : musicSize.trim(); } /** * 獲取 歌曲名 music_info.music_name * * @return 歌曲名 */ public String getMusicName() { return musicName; } /** * 設置 歌曲名 music_info.music_name * * @param musicName 歌曲名 */ public void setMusicName(String musicName) { this.musicName = musicName == null ? null : musicName.trim(); } @Override public String toString() { return "MusicInfo{" + "id=" + id + ", singerName='" + singerName + '\'' + ", musicSize='" + musicSize + '\'' + ", musicName='" + musicName + '\'' + '}'; } }
創建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.chaoqi.springboot_test.dao.mapper.MusicInfoMapper"> <resultMap id="BaseResultMap" type="com.chaoqi.springboot_test.dao.domain.MusicInfo"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="singer_name" jdbcType="VARCHAR" property="singerName" /> <result column="music_size" jdbcType="VARCHAR" property="musicSize" /> <result column="music_name" jdbcType="VARCHAR" property="musicName" /> </resultMap> </mapper>
創建mapper
package com.chaoqi.springboot_test.dao.mapper; import com.chaoqi.springboot_test.dao.domain.MusicInfo; import org.apache.ibatis.annotations.ResultMap; import org.apache.ibatis.annotations.Select; import java.util.List; public interface MusicInfoMapper { @ResultMap("BaseResultMap") @Select("select * from music_info") List<MusicInfo> selectAll(MusicInfo musicInfo); }
service接口
package com.chaoqi.springboot_test.service; import com.chaoqi.springboot_test.dao.domain.MusicInfo; import java.util.List; public interface MusicInfoService { public List<MusicInfo> getMusicInfo(MusicInfo musicInfo); }
service實現類
package com.chaoqi.springboot_test.service.impl; import com.chaoqi.springboot_test.dao.domain.MusicInfo; import com.chaoqi.springboot_test.dao.mapper.MusicInfoMapper; import com.chaoqi.springboot_test.service.MusicInfoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class MusicInfoServiceImpl implements MusicInfoService { @Autowired private MusicInfoMapper musicInfoMapper; @Override public List<MusicInfo> getMusicInfo(MusicInfo musicInfo) { List<MusicInfo> musicInfos = musicInfoMapper.selectAll(null); return musicInfos; } }
創建完成后的結構如下
編輯indexController
package com.chaoqi.springboot_test.web; import com.chaoqi.springboot_test.dao.domain.MusicInfo; import com.chaoqi.springboot_test.service.MusicInfoService; 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; @Controller public class IndexController { private static final String INDEX = "index"; @Autowired private MusicInfoService musicInfoService; @RequestMapping("/show") public String getIndex() { return INDEX; } @RequestMapping("/music") @ResponseBody public List<MusicInfo> getMusicInfo(MusicInfo musicInfo) { List<MusicInfo> musicInfoList = musicInfoService.getMusicInfo(null); return musicInfoList; } }
給SpringbootTestApplication類加上注解@MapperScan("com.chaoqi.springboot_test.dao.mapper")
package com.chaoqi.springboot_test; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @MapperScan("com.chaoqi.springboot_test.dao.mapper") public class SpringbootTestApplication { public static void main(String[] args) { SpringApplication.run(SpringbootTestApplication.class, args); } }
運行項目,成功,springboot+springmvc+mybatis整合完成(源碼下載地址)
關于“springboot+springmvc+mybatis項目整合的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。