1 添加相關maven文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
2、application.yml 添加相關配置
1.添加數據庫相關配置
springboot會自動加載spring.datasource.*相關配置,數據源就會自動注入到sqlSessionFactory中, sqlSessionFactory會自動注入到Mapper中,對了你一切都不用管了,直接拿起來使用就行了
spring:
datasource:
url: jdbc:mysql://localhost:3306/ms_datadictionary?useUnicode=true&characterEncoding=UTF8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
2.添加對mapper和xml文件的掃描
mybatis:
basepackage: com.yss.ms.mapper
xmlLocation: classpath:mapper/**/*.xml
3.開發
1.創建實體
在entity裏面 新建Datadict類
@Table(name="t_datadict")
public class Datadict {
@Id
private String fid;
@Column(name="FDICTATEGORY_id")
private String fdictategoryId;
private String fname;
private String fcode;
@Column(name="fcreator_id")
private String fcreatorId;
.......set/get方法略
2.創建mapper
在mapper 裏面新建一個Interface
public interface DatadictMapper extends Mapper<Datadict> {
}
注意:該接口必須繼承Mapper接口並傳入相應實體。
Mapper中封裝了簡單的增刪改成方法,可以直接調用。
/**
* 通用Mapper接口,其他接口繼承該接口即可
* <p/>
* <p>這是一個例子,自己擴展時可以參考</p>
* <p/>
* <p>項目地址 : <a href="https://github.com/abel533/Mapper" target="_blank">https://github.com/abel533/Mapper</a></p>
*
* @param <T> 不能爲空
* @author liuzh
*/
public interface Mapper<T> extends
BaseMapper<T>,
ExampleMapper<T>,
RowBoundsMapper<T>,
Marker {
}
public interface BaseMapper<T> extends
BaseSelectMapper<T>,
BaseInsertMapper<T>,
BaseUpdateMapper<T>,
BaseDeleteMapper<T> {
}