通常,我們利用mybatis寫持久層方法。要么按照傳統定義mapper方法,定義xml文件的方式,全部手寫。要么需要通過mybatis-generator逆向工程插件生成大量的xxxExample文件,使得系統看起來比較臃腫。而通用mapper
的引入,我們不需再生成大量的Example文件,並且通用mapper已經封裝好了所有的單表操作。通用mapper與springboot項目集成配置如下:
pom.xml配置
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
定義通用Mapper基類
package com.gogle.mgt.dataaccess.mybatis;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/**
* @ClassName TkMapper
* @Description TODO
* @Date 2019/7/16 16:15
* @Created by sunyiwei
*/
public interface TkMapper<T> extends Mapper<T>,MySqlMapper<T> {
}
yml文件配置
mybatis:
mapper-locations: classpath:mapper/*.xml #定義xml文件位置,不是必須的,如果需要在xml寫sql請配置此選項
type-aliases-package: com.gogle.mgt.domain # 注意:對應實體類的路徑
mapper:
mappers: com.gogle.mgt.dataaccess.mybatis.TkMapper #通用基類配置
identity: MYSQL
注:通用基類請不要與我們下面要定義的mapper位於同一包下,否則運行會報錯。
定義MapperScan
package com.gogle.mgt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import tk.mybatis.spring.annotation.MapperScan;
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
@MapperScan(basePackages = { "com.gogle.mgt.dataaccess.mybatis.dao" })
public class VslmApplication {
public static void main(String[] args) {
SpringApplication.run(VslmApplication.class, args);
}
}
注:此處MapperScan為tk.mybatis.spring.annotation.MapperScan,非org.mybatis.spring.annotation.MapperScan;
定義實體類
package com.gogle.mgt.domain;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
/**
* @ClassName StreamLine
* @Description TODO
* @Date 2019/7/16 16:16
* @Created by sunyiwei
*/
@Table(name="stream_line")
public class StreamLine {
@Id
String streamLineId;
@Column(name = "name")
String name;
@Column(name = "area")
String area;
@Column(name = "spec_id")
String specId;
@Column(name = "vpl_id")
String vplId;
@Column(name = "status")
byte status;
@Column(name = "create_time")
Date createTime;
@Column(name = "modify_time")
Date modifyTime;
@Column(name = "modify_person")
String modifyPerson;
public StreamLine() {
}
}
注:如果bean實體中含有數據表中不存在的字段,使用@Transient注解
mapper定義
package com.gogle.mgt.dataaccess.mybatis.dao;
import com.gogle.mgt.dataaccess.mybatis.TkMapper;
import com.gogle.mgt.domain.StreamLine;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @ClassName StreamLineMapper
* @Description TODO
* @Date 2019/7/16 16:23
* @Created by sunyiwei
*/
@Repository
public interface StreamLineMapper extends TkMapper<StreamLine> {
/**
* 注解sql
* @return
*/
@Select("select * from stream_line")
List<StreamLine> getAll();
/**
* xml定義語句
*/
StreamLine getById(String streamLineId);
}
注:mapper中已經繼承了TkMapper里面的所有單表操作的增刪改查方法,上面的兩個方法
getAll
與getById
是我們自定義的方法,一種通過注解定義,另外一種是我們常用的在xml文件里面寫方法。
測試
package com.gogle.mgt;
import java.util.List;
/**
* @ClassName MapperTest
* @Description TODO
* @Date 2019/7/16 16:28
* @Created by sunyiwei
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class MapperTest {
@Autowired
private StreamLineMapper mapper;
/**
* 繼承自通用mapper的單表操作方法測試
*/
@Test
public void test(){
StreamLine line = new StreamLine();
line.setStreamLineId("001");
line.setArea("蕭山");
int insert = mapper.insert(line);
System.out.println(insert);
}
/**
* 我們注解自定義的方法測試
*/
@Test
public void test2(){
List<StreamLine> all = mapper.getAll();
System.out.println(all);
}
/**
* xml文件中定義接口測試
*/
@Test
public void test3(){
StreamLine one = mapper.getById("001");
System.out.println(one);
}
}