1. 程式人生 > >Spring Boot 實用MyBatis做資料庫操作

Spring Boot 實用MyBatis做資料庫操作

前言:

本專案基於maven構建,使用mybatis-spring-boot作為spring-boot專案的持久層框架

spring-boot中使用mybatis持久層框架與原spring專案使用方式和註解都不相同,需要依賴mybatis-spring-boot包

1、引入mybatis和資料庫及其他專案依賴

1.1、引入mybatis依賴

[html] view plain copy  print?在CODE上檢視程式碼片派生到我的程式碼片
  1. <!-- mybatis-spring-boot -->
  2.         <dependency>
  3.             <groupId>org.mybatis.spring.boot</groupId>
  4.             <artifactId>mybatis-spring-boot-starter</artifactId>
  5.             <version>1.1.1</version>
  6.         </dependency>

1.2、引入mysql 驅動

[html] view plain copy  print?在CODE上檢視程式碼片派生到我的程式碼片
  1. <!-- mysql-->
  2.         <dependency>
  3.             <groupId>mysql</groupId>
  4.             <artifactId>mysql-connector-java</artifactId>
  5.         </dependency>

1.3、專案pom.xml一覽

[html] view plain copy  print?在CODE上檢視程式碼片派生到我的程式碼片
  1. <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3.     <modelVersion>4.0.0</modelVersion>
  4.     <groupId>cn.eguid.carDeviceInfoSys</groupId>
  5.     <artifactId>carSys-web</artifactId>
  6.     <packaging>war</packaging>
  7.     <version>1.4.0-SNAPSHOT</version>
  8.     <name>carSys-web</name>
  9.     <parent>
  10.         <groupId>org.springframework.boot</groupId>
  11.         <artifactId>spring-boot-starter-parent</artifactId>
  12.         <version>1.4.0.RELEASE</version>
  13.     </parent>
  14.     <dependencies>
  15.         <!-- spring-boot web -->
  16.         <dependency>
  17.             <groupId>org.springframework.boot</groupId>
  18.             <artifactId>spring-boot-starter-web</artifactId>
  19.             <!--<exclusions><exclusion><groupId>org.springframework.boot</groupId>
  20.                 <artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions> -->
  21.         </dependency>
  22.         <!-- spring aop -->
  23.         <dependency>
  24.             <groupId>org.springframework.boot</groupId>
  25.             <artifactId>spring-boot-starter-aop</artifactId>
  26.         </dependency>
  27.         <!-- mysql-->
  28.         <dependency>
  29.             <groupId>mysql</groupId>
  30.             <artifactId>mysql-connector-java</artifactId>
  31.         </dependency>
  32.         <dependency>
  33.             <groupId>org.springframework.boot</groupId>
  34.             <artifactId>spring-boot-starter-jdbc</artifactId>
  35.         </dependency>
  36.         <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
  37.         <dependency>
  38.             <groupId>redis.clients</groupId>
  39.             <artifactId&g