Spring Boot 整合 MyBatis--跟著慕課熊貓學
阿新 • • 發佈:2020-10-21
Spring Boot 整合 MyBatis
企業級應用資料持久層框架,最常見的應該是 Hibernate 和 MyBatis 。
Hibernate 是相當徹底的 ORM 物件 - 關係對映框架,使用 Hibernate ,開發者可以不考慮 SQL 語句的編寫與執行,直接操作物件即可。
與 Hibernate 相比, MyBatis 還是需要手工編寫 SQL 語句的。
除此之外,MyBatis 是更加簡單,更容易上手的框架,但是功能也是相對簡陋
相關依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- 熱部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <!-- Web支援 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- JDBC --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- MySQL驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 整合MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!-- 測試 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency>
配置檔案
# 配置資料庫驅動 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # 配置資料庫url spring.datasource.url=jdbc:mysql://127.0.0.1:3306/shop?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC # 配置資料庫使用者名稱 spring.datasource.username=root # 配置資料庫密碼 spring.datasource.password=123456 # 指定MyBatis配置檔案位置 mybatis.mapper-locations=classpath:mapper/*.xml
資料物件類
/**
* 商品類
*/
public class GoodsDo {
/**
* 商品id
*/
private Long id;
/**
* 商品名稱
*/
private String name;
/**
* 商品價格
*/
private String price;
/**
* 商品圖片
*/
private String pic;
// 省略 get set方法
}
開發資料訪問層
資料訪問層直接使用介面實現即可,介面中新增商品的增刪改查基本操作。
例項:
/** * 商品資料庫訪問介面 */ @Repository // 標註資料訪問元件 public interface GoodsDao { /** * 新增商品 */ public int insert(GoodsDo Goods); /** * 刪除商品(根據id) */ public int delete(Long id); /** * 修改商品資訊(根據id修改其他屬性值) */ public int update(GoodsDo Goods); /** * 查詢商品資訊(根據id查詢單個商品資訊) */ public GoodsDo selectOne(Long id); /** * 查詢商品列表 */ public List<GoodsDo> selectAll(); }
啟動檔案
@SpringBootApplication
@MapperScan("com.imooc.springbootmybatis") // 指定MyBatis掃描的包,以便將資料訪問介面註冊為bean
public class SpringBootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisApplication.class, args);
}
}
對映檔案
在 resources/mapper
目錄下新建 GoodsMapper.xml
檔案,該檔案就是 goods 表對應的對映檔案,內容如下:
例項:
<?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">
<!-- 本對映檔案對應GoodsDao介面 -->
<mapper namespace="com.imooc.springbootmybatis.GoodsDao">
<!-- 對應GoodsDao中的insert方法 -->
<insert id="insert" parameterType="com.imooc.springbootmybatis.GoodsDo">
insert into goods (name,price,pic) values (#{name},#{price},#{pic})
</insert>
<!-- 對應GoodsDao中的delete方法 -->
<delete id="delete" parameterType="java.lang.Long">
delete from goods where id=#{id}
</delete>
<!-- 對應GoodsDao中的update方法 -->
<update id="update" parameterType="com.imooc.springbootmybatis.GoodsDo">
update goods set name=#{name},price=#{price},pic=#{pic} where id=#{id}
</update>
<!-- 對應GoodsDao中的selectOne方法 -->
<select id="selectOne" resultMap="resultMapBase" parameterType="java.lang.Long">
select <include refid="sqlBase" /> from goods where id = #{id}
</select>
<!-- 對應GoodsDao中的selectAll方法 -->
<select id="selectAll" resultMap="resultMapBase">
select <include refid="sqlBase" /> from goods
</select>
<!-- 可複用的sql模板 -->
<sql id="sqlBase">
id,name,price,pic
</sql>
<!-- 儲存SQL語句查詢結果與實體類屬性的對映 -->
<resultMap id="resultMapBase" type="com.imooc.springbootmybatis.GoodsDo">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="price" property="price" />
<result column="pic" property="pic" />
</resultMap>
</mapper>
測試
/**
* 新增一個商品
*/
@Test
void test_01() {
GoodsDo goods = new GoodsDo();
goods.setName("手機");
goods.setPic("phone.jpg");
goods.setPrice("2000");
int count = goodsDao.insert(goods);
assertEquals(1, count);// count值為1則測試通過
}