spring boot mybatis 註解sql
spring boot 約定優於配置 ,整合mybatis 時候,一般也採用mybatis 註解sql .
下面簡單介紹一下。
1:spring boot pom 新增對mybatis 的支援及分頁外掛。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.github.abel533</groupId>
<artifactId>mapper</artifactId>
<version>3.0.0</version>
</dependency>
2: 編寫mapper
public interface Mtper<T> {
@InsertProvider(method = "insertSelective", type = BaseProvider.class)
int save(T t);
@UpdateProvider(method = "updateByPrimaryKeySelective", type = BaseProvider.class)
int update(T t);
@DeleteProvider(method = "deleteByPrimaryKey", type = BaseProvider.class)
int deleteByKey(T t,Object val);
@SelectProvider(type=BaseProvider.class,method = "selectByPrimaryKey")
T selectByKey(Class<?> clazz,Object val);
@InsertProvider(type = BaseProvider.class, method = "insertAll")
int insertAll(@Param("list") List<T> list,T t);
}
3: 編寫sql 指令碼
public class UserDynaSqlProvider extends BaseProvider {
public String findTutorByIdSql(User user) {
return "select u.id as id,c.cards as cards from user u LEFT JOIN card c on u.id=c.uid and u.id="
+ user.getId();
}
}
4:在service 層注入對應的mapper, 處理好業務邏輯即可
5:需要原始碼的可以聯絡我qq .2354764247 備註 ‘王芳平美女’ 。