之前搭傳統的ssm框架,配置文件很多,看了幾天文檔才把那些xml的邏輯關系搞得七七八八,搭起來也是很麻煩,那時我完全按網上那個demo的版本要求(jdk和tomcat),所以最后是各種問題沒成功跑起來。 今天嘗試用springboot來整合,不敢相信才失敗幾次就成功了!!
現在來記錄一下過程:
首先springboot基本的建立就不講了,之前的博客里面有寫。
添加POM依賴:
<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(這個可是好東西)
開始寫代碼:
這里我們有個簡單是數據表:
我們的目的是 用get發一個帶有 id值的請求,服務器根據id值返回這個圖書管理員的全部信息並用json的方式直接顯示在頁面上,
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兩個注解的合體,一般就拿來直接傳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