須要添加下面三個依賴:html
dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.0' implementation 'mysql:mysql-connector-java' }
配置文件的編寫須要注意參數的配置,好比SSL那個通常要設置爲false,driver-class-name也要注意一下,不要寫錯了,文件的大體內容以下:java
mybatis.type-aliases-package=com.seckill.spring.mapper spring.datasource.url=jdbc:mysql://10.33.8.189:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver
在入口函數添加Mapper掃描配置,這樣沒必要在每一個Mapper上加上Mapper註解,大體以下:mysql
package com.seckill.spring; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.seckill.spring.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
編輯商品實體類,大體以下:web
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.validation.constraints.Size; @Entity public class Goods { public Goods(int id, String name, int amount) { this.id = id; this.name = name; this.amount = amount; } @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; @Size(min = 1, max = 50) private String name; private int amount; }
編寫Mapper接口類,大體內容以下:spring
import com.seckill.spring.entity.Goods; import org.apache.ibatis.annotations.*; import java.util.List; public interface GoodsMapper { @Insert("INSERT INTO goods(name, amount) VALUES('${name}', #{amount})") Integer insertGoods(@Param("name")String name, @Param("amount")Integer amount) throws Exception; @Select("SELECT * FROM goods") List<Goods> findAll(); @Select("SELECT * FROM goods WHERE id = #{id}") Goods findById(@Param("id") Integer id); @Update("UPDATE goods SET amount = #{goods.amount} WHERE id = #{goods.id}") Integer updateGoods(@Param("goods") Goods goods) throws Exception; @Delete("Delete FROM goods") Integer deleteAll(); }
其中要注意的是$和#的用法,前者用於字符串變量,後者用於整型變量sql
// This example creates a prepared statement, something like select * from teacher where name = ?; @Select("Select * from teacher where name = #{name}") Teacher selectTeachForGivenName(@Param("name") String name); // This example creates n inlined statement, something like select * from teacher where name = 'someName'; @Select("Select * from teacher where name = '${name}'") Teacher selectTeachForGivenName(@Param("name") String name);
測試還有些坑,不如類上面的註解應該如代碼中的那樣纔有用,而且有時發現不了測試函數,須要去掉@Test註解,再從新添加後運行。大體打代碼以下:apache
import com.seckill.spring.Application; import com.seckill.spring.entity.Goods; import com.seckill.spring.mapper.GoodsMapper; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest( classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT ) @DirtiesContext public class GoodsMapperTest { @Autowired private GoodsMapper goodsMapper; @Test public void testAll() throws Exception { goodsMapper.deleteAll(); Assert.assertEquals(0, goodsMapper.findAll().size()); Integer response = goodsMapper.insertGoods("good1", 1000); Assert.assertEquals(1, response.intValue()); List<Goods> goods = goodsMapper.findAll(); Assert.assertEquals(1, goods.size()); int id = goods.get(0).getId(); Assert.assertNotNull(goodsMapper.findById(id)); Goods newGoods = new Goods(id, "good1", 100); Assert.assertEquals(1, goodsMapper.updateGoods(newGoods).intValue()); Assert.assertEquals(100, goodsMapper.findById(id).getAmount()); } }