Spring boot整合Mybatis
宣告:本文轉載自:https://blog.csdn.net/litianxiang_kaola/article/details/79481422
這裡介紹兩種整合SpringBoot和Mybatis的模式,分別是“全註解版” 和 “註解xml合併版”。
前期準備
開發環境
- 開發工具:IDEA
- JDK:1.8
- 技術:SpringBoot、Maven、Mybatis
建立專案
專案結構
Maven依賴
<?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.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</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/> <!-- lookup parent from repository --> </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-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
全註解版
SpringBoot配置檔案
這裡使用yml格式的配置檔案,將application.properties改名為application.yml。
#配置資料來源 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/dianping?useUnicode=true&characterEncoding=utf8 username: root password: 123 driver-class-name: com.mysql.jdbc.Driver
SpringBoot會自動載入application.yml相關配置,資料來源就會自動注入到sqlSessionFactory中,sqlSessionFactory會自動注入到Mapper中。
實體類
public class Happiness { private Long id; private String city; private Integer num; //getters、setters、toString }
對映類
@Mapper public interface HappinessDao { @Select("SELECT * FROM happiness WHERE city = #{city}") Happiness findHappinessByCity(@Param("city") String city); @Insert("INSERT INTO happiness(city, num) VALUES(#{city}, #{num})") int insertHappiness(@Param("city") String city, @Param("num") Integer num); }
Service類
事務管理只需要在方法上加個註解:@Transactional
@Service public class HappinessService { @Autowired private HappinessDao happinessDao; public Happiness selectService(String city){ return happinessDao.findHappinessByCity(city); } @Transactional public void insertService(){ happinessDao.insertHappiness("西安", 9421); int a = 1 / 0; //模擬故障 happinessDao.insertHappiness("長安", 1294); } }
Controller類
@RestController @RequestMapping("/demo") public class HappinessController { @Autowired private HappinessService happinessService; @RequestMapping("/query") public Happiness testQuery(){ return happinessService.selectService("北京"); } @RequestMapping("/insert") public Happiness testInsert(){ happinessService.insertService(); return happinessService.selectService("西安"); } }
測試
http://localhost:8080/demo/query
http://localhost:8080/demo/insert
註解xml合併版
專案結構
SpringBoot配置檔案
#配置資料來源 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/dianping username: root password: 123 driver-class-name: com.mysql.jdbc.Driver #指定mybatis對映檔案的地址 mybatis: mapper-locations: classpath:mapper/*.xml
對映類
@Mapper public interface HappinessDao { Happiness findHappinessByCity(String city); int insertHappiness(HashMap<String, Object> map); }
對映檔案
<mapper namespace="com.example.demo.dao.HappinessDao"> <select id="findHappinessByCity" parameterType="String" resultType="com.example.demo.domain.Happiness"> SELECT * FROM happiness WHERE city = #{city} </select> <insert id="insertHappiness" parameterType="HashMap" useGeneratedKeys="true" keyProperty="id"> INSERT INTO happiness(city, num) VALUES(#{city}, #{num}) </insert> </mapper>
Service類
事務管理只需要在方法上加個註解:@Transactional
@Service public class HappinessService { @Autowired private HappinessDao happinessDao; public Happiness selectService(String city){ return happinessDao.findHappinessByCity(city); } @Transactional public void insertService(){ HashMap<String, Object> map = new HashMap<String, Object>(); map.put("city", "西安"); map.put("num", 9421); happinessDao.insertHappiness(map); int a = 1 / 0; //模擬故障 happinessDao.insertHappiness(map); } }
Controller類
@RestController @RequestMapping("/demo") public class HappinessController { @Autowired private HappinessService happinessService; @RequestMapping("/query") public Happiness testQuery(){ return happinessService.selectService("北京"); } @RequestMapping("/insert") public Happiness testInsert(){ happinessService.insertService(); return happinessService.selectService("西安"); } }
SpringBoot整合Mybatis列印sql語句
如果使用的是application.properties檔案,加入如下配置
logging.level.com.example.demo.mapper=debug
logging.level,後面的路徑指的是mybatis對應的方法介面所在的包。並不是mapper.xml所在的包。
如果使用的是application.yml檔案,加入如下配置:
logging: level: com.example.demo.mapper: debug
在mybatis中@MapperScan和@Mapper的區別
在使用Mybatis時需要把對映類*.mapper註冊到Bean工廠中,這時候我們有兩種方式可以注入,一種是在每個*.mapper類上加上@Mapper註解,另一種就是在Application啟動類上加@MapperScan註解來掃描類
(1)方式一:使用@Mapper註解
為了讓DemoMapper能夠讓別的類進行引用,我們可以在DemMapper類上新增@Mapper註解:
直接在Mapper類上面添加註解@Mapper,這種方式要求每一個mapper類都需要新增此註解,比較麻煩。
(2)方式二:使用@MapperScan註解
通過使用@MapperScan可以指定要掃描的Mapper類的包的路徑,比如:
使用@MapperScan註解多個包