spring Boot 整合學習
阿新 • • 發佈:2021-01-30
技術標籤:學習spring boot# mybatis-plusjava
spring Boot 整合學習
spring Boot 整合 MyBatis-Plus
1.配置pom檔案
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.2</version> </dependency>
由於是使用了spring boot 整合 mybeatis-plus:
用到了spring-boot-starter,而spring-boot-starter自帶mybeatis,mybeatis-plus也自帶mybeatis,會產生包衝突,需要把spring-boot-starter的排除掉
<exclusions> <exclusion> <artifactId>mybatis</artifactId> <groupId>org.mybatis</groupId> </exclusion> </exclusions>
2.生成實體類,dao介面,service介面,service實現類
2.1 實體類
@TableName("sys_setting")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SysSetting implements Serializable {
/**
* 表id
*/
@TableId
private String id;
/**
* 系統名稱
*/
private String sysName;
/**
* 系統logo圖示
*/
private String sysLogo;
/**
* 系統底部資訊
*/
private String sysBottomText;
/**
* 系統公告
*/
private String sysNoticeText;
/**
* 建立時間
*/
private Date createTime;
/**
* 修改時間
*/
private Date updateTime;
/**
* 使用者管理:初始、重置密碼
*/
private String userInitPassword;
/**
* 系統顏色
*/
private String sysColor;
/**
* API加密 Y/N
*/
private String sysApiEncrypt;
private static final long serialVersionUID = 1L;
2.2 dao介面
public interface SysSettingDao extends BaseMapper<SysSetting> {
}
dao繼承BaseMapper後就可以使用一些固定的sql方法
/**
* Mapper 繼承該介面後,無需編寫 mapper.xml 檔案,即可獲得CRUD功能
* <p>這個 Mapper 支援 id 泛型</p>
*
* @author hubin
* @since 2016-01-23
*/
public interface BaseMapper<T> extends Mapper<T> {
plus是mybeatis的增強版,具有mybeatis的所有功能,如果需要自定義sql的話,自己新建sql.xml檔案即可。但是需要修改配置檔案:
mybatis:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.jiang.entity
2.3 service介面
service可以繼承IService 介面。
IService 內部進一步封裝了 BaseMapper 介面的方法(當然也提供了更詳細的方法)。
使用時,可以通過 生成的 mapper 類進行 CRUD 操作,也可以通過 生成的 service 的實現類進行 CRUD 操作。(當然,自定義程式碼執行也可)。
public interface SysSettingService {
public SysSetting getById(String id)throws RuntimeException;
}
public interface SysShortcutMenuService extends IService<SysShortcutMenu> {
}
2.4 service實現類
實現類和service一樣可以繼承也可以自定義。
@Slf4j
@Service("SysSettingService")
public class SysSettingServiceImpl implements SysSettingService {
@Autowired
private SysSettingDao sysSettingDao;
@Override
public SysSetting getById(String id) throws RuntimeException {
try {
return sysSettingDao.selectById(id);
} catch (Exception e) {
log.error("系統配置查詢異常:{}",e.getMessage());
}
return null;
}
}
public interface SysShortcutMenuService extends IService<SysShortcutMenu> {
}
3. 顯示列印日誌
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl