基於MyBatis註解擴充套件,實現無需配置即可使用
一、使用篇
如果專案本身基於SpringMVC+Mybatis構建,不需新增任何配置Mapper類只需繼承BaseMapper,即擁有增刪改方法操作,無需任何配置檔案
1.
package com.springmvc.mapper;
import org.springframework.stereotype.Repository;
import com.nmtx.mybatis.ext.mapper.BaseMapper;
import com.springmvc.model.User;
@Repository
public interface UserMapper extends BaseMapper<User>{
}
2.
package com.springmvc.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.springmvc.mapper.UserMapper;
import com.springmvc.model.User;
import com.springmvc.service.UserService;
@Service
public class UserServiceImpl implements UserService{
@Resource
private UserMapper userMapper;
public int insertUser(User user) {
return userMapper.insert(user);
}
@Override
public int updateUser(User user) {
return userMapper.update(user);
}
@Override
public int deleteUser(User user) {
return userMapper.delete(user);
}
@Override
public User findUser(User user) {
return userMapper.findFirst(user);
}
}
3.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.springmvc.mapper.UserMapper">
</mapper>
擴充套件原理基於Mybatis中 @InsertProvider,@DeleteProvider,@UpdateProvider,@SelectProvider註解,具體實現程式碼如下
1.
package com.nmtx.mybatis.ext.mapper;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
import com.nmtx.mybatis.ext.common.SqlProvider;
public interface BaseMapper<T> {
@InsertProvider(type = SqlProvider.class, method = "insert")
@Options(useGeneratedKeys=true)
public int insert(T bean);
@DeleteProvider(type = SqlProvider.class, method = "delete")
public int delete(T bean);
@UpdateProvider(type = SqlProvider.class, method = "update")
public int update(T bean);
@SelectProvider(type = SqlProvider.class, method = "findFirst")
public T findFirst(T bean);
2.
package com.nmtx.mybatis.ext.common;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.util.StringUtils;
import com.nmtx.mybatis.ext.common.table.TableFormat;
import com.nmtx.mybatis.ext.common.table.annotation.Column;
import com.nmtx.mybatis.ext.common.table.annotation.Table;
import com.nmtx.mybatis.ext.common.table.impl.HumpToUnderLineFormat;
public class SqlProvider {
private TableFormat tableFormat = new HumpToUnderLineFormat();
public String insert(Object bean) {
Class<?> beanClass = bean.getClass();
String tableName = getTableName(beanClass);
Field[] fields = getFields(beanClass);
StringBuilder insertSql = new StringBuilder();
List<String> insertParas = new ArrayList<String>();
List<String> insertParaNames = new ArrayList<String>();
insertSql.append("INSERT INTO ").append(tableName).append("(");
try {
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
Column column = field.getAnnotation(Column.class);
String columnName = "";
if (column != null) {
if (!column.required())
continue;
columnName = column.value();
}
if (StringUtils.isEmpty(columnName)) {
columnName = tableFormat.getColumnName(field.getName());
}
field.setAccessible(true);
Object object = field.get(bean);
if (object != null) {
insertParaNames.add(columnName);
insertParas.add("#{" + field.getName() + "}");
}
}
} catch (Exception e) {
new RuntimeException("get insert sql is exceptoin:" + e);
}
for (int i = 0; i < insertParaNames.size(); i++) {
insertSql.append(insertParaNames.get(i));
if (i != insertParaNames.size() - 1)
insertSql.append(",");
}
insertSql.append(")").append(" VALUES(");
for (int i = 0; i < insertParas.size(); i++) {
insertSql.append(insertParas.get(i));
if (i != insertParas.size() - 1)
insertSql.append(",");
}
insertSql.append(")");
return insertSql.toString();
}
public String update(Object bean) {
Class<?> beanClass = bean.getClass();
String tableName = getTableName(beanClass);
Field[] fields = getFields(beanClass);
StringBuilder updateSql = new StringBuilder();
updateSql.append(" update ").append(tableName).append(" set ");
try {
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
Column column = field.getAnnotation(Column.class);
String columnName = "";
if (column != null) {
if (!column.required())
continue;
columnName = column.value();
}
if (StringUtils.isEmpty(columnName)) {
columnName = tableFormat.getColumnName(field.getName());
}
field.setAccessible(true);
Object beanValue = field.get(bean);
if (beanValue != null) {
updateSql.append(columnName).append("=#{").append(field.getName()).append("}");
if (i != fields.length - 1) {
updateSql.append(",");
}
}
}
} catch (Exception e) {
new RuntimeException("get update sql is exceptoin:" + e);
}
updateSql.append(" where ").append(tableFormat.getId()+" =#{id}");
return updateSql.toString();
}
public String delete(Object bean) {
Class<?> beanClass = bean.getClass();
String tableName = getTableName(beanClass);
Field[] fields = getFields(beanClass);
StringBuilder deleteSql = new StringBuilder();
deleteSql.append(" delete from ").append(tableName).append(" where ");
try {
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
Column column = field.getAnnotation(Column.class);
String columnName = "";
if (column != null) {
if (!column.required())
continue;
columnName = column.value();
}
if (StringUtils.isEmpty(columnName)) {
columnName = tableFormat.getColumnName(field.getName());
}
field.setAccessible(true);
Object beanValue = field.get(bean);
if (beanValue != null) {
deleteSql.append(columnName).append("=#{").append(field.getName()).append("}");
if (i != fields.length - 1) {
deleteSql.append(" and ");
}
}
}
} catch (Exception e) {
new RuntimeException("get delete sql is exceptoin:" + e);
}
return deleteSql.toString();
}
public String findFirst(Object bean) {
Class<?> beanClass = bean.getClass();
String tableName = getTableName(beanClass);
Field[] fields = getFields(beanClass);
StringBuilder selectSql = new StringBuilder();
List<String> selectParaNames = new ArrayList<String>();
List<String> selectParas = new ArrayList<String>();
selectSql.append("select ");
try {
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
Column column = field.getAnnotation(Column.class);
String columnName = "";
if (column != null) {
if (!column.required())
continue;
columnName = column.value();
}
if (StringUtils.isEmpty(columnName)) {
columnName = tableFormat.getColumnName(field.getName());
}
field.setAccessible(true);
Object object = field.get(bean);
selectSql.append(field.getName());
if (object != null) {
selectParaNames.add(columnName);
selectParas.add("#{" + field.getName() + "}");
}
if (i != fields.length - 1)
selectSql.append(",");
}
} catch (Exception e) {
new RuntimeException("get select sql is exceptoin:" + e);
}
selectSql.append(" from ").append(tableName).append(" where ");
for (int i = 0; i < selectParaNames.size(); i++) {
selectSql.append(selectParaNames.get(i)).append("=").append(selectParas.get(i));
if (i != selectParaNames.size() - 1)
selectSql.append(" and ");
}
return selectSql.toString();
}
private String getTableName(Class<?> beanClass) {
String tableName = "";
Table table = beanClass.getAnnotation(Table.class);
if (table != null) {
tableName = table.value();
} else {
tableName = tableFormat.getTableName(beanClass.getSimpleName());
}
return tableName;
}
private Field[] getFields(Class<?> beanClass) {
Field[] beanFields = beanClass.getDeclaredFields();
Class<?> beanSuperClass = beanClass.getSuperclass();
Field[] beanSuperFields = beanSuperClass.getDeclaredFields();
return ArrayUtils.addAll(beanFields, beanSuperFields);
}
}
相關文章
- 基於shiro的自定義註解的擴充套件套件
- 基於PHP擴充套件的WAF實現PHP套件
- Mybatis擴充套件MyBatis套件
- 使用基於策略的網路擴充套件KubernetesDeployments套件
- JWT 擴充套件具體實現詳解JWT套件
- spring4.1.8擴充套件實戰之八:Import註解Spring套件Import
- spring基於註解配置實現事務控制Spring
- yii2-websocket | 基於 yii2 實現的 WebSocket 擴充套件Web套件
- 基於MongoDB.Driver的擴充套件MongoDB套件
- PHP實現Bitmap的探索 - GMP擴充套件使用PHP套件
- [擴充套件] Hyperf-redis-lock 基於hyperf的分散式鎖的實現套件Redis分散式
- Solon詳解(六)- Solon的校驗擴充套件框架使用與擴充套件套件框架
- 基於 GatewayWorker 開發的 Laravel 擴充套件GatewayLaravel套件
- ATU 擴充套件,改變你的開發方式! 基於註解的單元測試神器套件
- 使用iToolab UnlockGo for Mac 無需密碼即可解鎖 Apple IDGoMac密碼APP
- 使用Kotlin擴充套件函式擴充套件Spring Data案例Kotlin套件函式Spring
- 擴充套件 Laravel-admin,採用分頁風格實現下拉選單,換頁無重新整理,無需寫介面,欄位無需改名稱套件Laravel
- kotlin 擴充套件(擴充套件函式和擴充套件屬性)Kotlin套件函式
- Mybatis相關:基於註解的Mybatis開發MyBatis
- Mybatis之map操作使用者和模糊查詢擴充套件MyBatis套件
- dubbo是如何實現可擴充套件的?套件
- 【mybatis annotation】資料層框架應用--Mybatis(二) 基於註解實現資料的CRUDMyBatis框架
- 基於PCNTl擴充套件的PHP多程式管理庫套件PHP
- 基於 swoole擴充套件 的即時通訊 im套件
- 編寫基於PHP擴充套件庫的後門PHP套件
- 實現近乎無限可擴充套件性的7種設計模式套件設計模式
- 《基於 swoole 擴充套件實現真正的 PHP 資料庫連線池》讀後感套件PHP資料庫
- 基於AbstractProcessor擴充套件MapStruct自動生成實體對映工具類套件Struct
- [擴充套件]laravel-xlswriter 一款基於xlswriter的laravel擴充套件包 excel極速讀寫套件LaravelExcel
- Spring Aop基於註解的實現Spring
- Mybatis外掛擴充套件以及與Spring整合原理MyBatis套件Spring
- Less(v3.9.0)使用詳解—extend(擴充套件)套件
- Source insight擴充套件宏使用套件
- 使用ctypes來擴充套件Python套件Python
- 使用cython擴充套件python庫套件Python
- Laravel 使用 phpredis 擴充套件之後,出現指定 database 無效的情況。LaravelPHPRedis套件Database
- [譯]Swift 3 中實現Dispatch once擴充套件Swift套件
- php利用pcntl擴充套件實現高併發PHP套件
- django實現自定義manage命令的擴充套件Django套件