以前搭傳統的ssm框架,配置文件不少,看了幾天文檔才把那些xml的邏輯關係搞得七七八八,搭起來也是很麻煩,那時我徹底按網上那個demo的版本要求(jdk和tomcat),因此最後是各類問題沒成功跑起來。 今天嘗試用springboot來整合,不敢相信才失敗幾回就成功了!! java
如今來記錄一下過程:mysql
首先springboot基本的創建就不講了,以前的博客裏面有寫。spring
添加POM依賴:sql
<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>
關於dao層的依賴就是後面兩個mysql的connector和mybatis的starter(這個但是好東西)數據庫
開始寫代碼:apache
這裏咱們有個簡單是數據表:json
咱們的目的是 用get發一個帶有 id值的請求,服務器根據id值返回這個圖書管理員的所有信息並用json的方式直接顯示在頁面上,tomcat
Controller:springboot
@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兩個註解的合體,通常就拿來直接傳json數據。 爲何能夠直接傳個對象過去呢?這是由於springboot內置了jackson模塊,能夠在maven的依賴下看到這方面的jar包(以前我寫是按網上的教程用gson來處理的,比起來這個簡直無敵)服務器
而後看到咱們注入的sevice,下面是service
Service:
public interface LibrarianService { Librarian selectLibrarian(int id); }
就是個接口
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備註,纔會被spring生成bean而後注入到controller那裏去。
而後看到這裏注入了個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可以根據xml和這個接口動態生成這個接口的實現。若是是加@Repository,就是spring生成一個bean,自動注入service的相關引用中。
而後是Mapper的xml文件(個人MyBatis的相關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"> <!-- 可根據本身的需求,是否要使用 --> <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很重要噢,它是指定這個xml所對應的是哪一個dao(mapper)接口
resultMap是作個pojo和數據庫表的對應(這裏只寫個Librarian不用寫全路徑是由於作了設置,下面會說)
select標籤的id對應的就是mapper接口中的方法名,parameterType就是傳進來的參數類型
簡單的配置與設置:
好如今講講設置,這裏會想到,那些Controller啊,@Service啊還有MyBatis的註解@Mapper什麼的spring怎麼知道在哪呢?不用像mvc那樣搞個掃描設置嗎?
是的要的,這些咱們在啓動類作了簡單的設置:
package com.example.main; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 指定所掃的包,會自動掃描指定包下的所有標有@Component的類,並註冊成bean, * 固然包括@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); } }
關於這個SpringBootApplication,它實際上是好幾個註解的合體(以前的博客裏有講),因此能夠直接在這寫掃包的設置,而後那個@MapperScan是MyBatis提供的設置註解。
而後還有點配置文件:
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
主要有關的是第四行開始的,最後兩行一個是設置基本包(包別名)也就是爲何在mapper.xml中能夠只寫一個Librarian的緣由。
最後一行的很重要,是告訴系統在哪裏去找mapper.xml文件。
上一個最後的效果圖:
再放一個項目結構圖:
放兩個springboot和mybatis整合講得比較好的博客供參考:
https://blog.csdn.net/gebitan505/article/details/54929287(要是着看到這個就省好多功夫了!)
https://blog.csdn.net/blackwoodcliff/article/details/50776155