SpringBoot 封装组件方式二解析

无情 阅读:212 2021-03-31 17:00:00 评论:0

第二种:基于通用配置参数包+Spring 注解标签,实现相关组件封装

任务要求:完成数据库指定表转换为SQL语句(增、删、改和查)

为什么采用通用配置参数模块,之前采用SpringBoot 封装组件方式一,打包jar 时,maven 提示相关资源无法找到,所以将各个封装组件依赖的配置参数统一配置到一个专门存储配置参数的jar 包中,截图如下:

核心功能代码:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.Properties; 
 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.jdbc.core.JdbcTemplate; 
import org.springframework.jdbc.datasource.DriverManagerDataSource; 
import com.***.ucas.mapping.config.domain.MappingConfigEntity; 
import com.***.ucas.mapping.util.MappingUtil; 
 
@Configuration 
public class MappingConfig { 
	 
	public static MappingConfigEntity config = new MappingConfigEntity(); 
	static { 
		Properties properties = new Properties();  
		InputStream stream = MappingConfig.class.getClassLoader().getResourceAsStream("mapper.properties"); 
		BufferedReader reader= new BufferedReader(new InputStreamReader(stream)); 
		 
		try { 
			properties.load(reader); 
			config.setDriverClassName(properties.getProperty("jdbc.driverClassName")); 
			config.setUrl(properties.getProperty("jdbc.url")); 
			config.setUsername(properties.getProperty("jdbc.username")); 
			config.setPassword(properties.getProperty("jdbc.password")); 
			 
		} catch (IOException e) { 
			// 自定义错误信息提示输出,并终止程序执行。 
		} 
	} 
	 
	/** 
	 * DataSource 实例化对象 
	 * @return 
	 */ 
	@Bean 
	public DriverManagerDataSource getDataSource(){ 
		DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
		dataSource.setDriverClassName(config.getDriverClassName()); 
		dataSource.setUrl(config.getUrl()); 
		dataSource.setUsername(config.getUsername()); 
		dataSource.setPassword(config.getPassword()); 
		return dataSource; 
	} 
	 
	/** 
	 * JdbcTemplate 实例化对象 
	 * @return 
	 */ 
	@Bean 
	public JdbcTemplate getJdbcTemplate(){ 
		JdbcTemplate template = new JdbcTemplate(getDataSource()); 
		return template; 
		 
	} 
	 
	@Bean 
	public MappingUtil getMappingUtil(){ 
		MappingUtil util = new MappingUtil(); 
		util.setTemplate(getJdbcTemplate()); 
		return util; 
	} 
 
}
public class MappingConfigEntity { 
	    // 数据库url 
		private String url; 
		// 数据库用户名 
		private String username; 
		// 数据库密码 
		private String password; 
		// 数据库驱动 
		private String driverClassName; 
		 
		// set 和  get 
		public String getUrl() { 
			return url; 
		} 
		public void setUrl(String url) { 
			this.url = url; 
		} 
		public String getUsername() { 
			return username; 
		} 
		public void setUsername(String username) { 
			this.username = username; 
		} 
		public String getPassword() { 
			return password; 
		} 
		public void setPassword(String password) { 
			this.password = password; 
		} 
		public String getDriverClassName() { 
			return driverClassName; 
		} 
		public void setDriverClassName(String driverClassName) { 
			this.driverClassName = driverClassName; 
		} 
 
} 
import java.lang.reflect.Field; 
import java.lang.reflect.Modifier; 
import java.util.List; 
import java.util.Vector; 
 
import com.***.common.util.ClassConvertTable; 
import com.***.common.util.ReflectionUtils; 
import com.***.ucas.mapping.annotation.ID; 
 
/** 
 * 基于java 实体对象创建sql的增删改查功能。 
 *  
 * @author zzg 
 * 
 */ 
public class EntityUtil { 
	 
	 
	// class目标 
	private Class<?> target; 
	// 实例化对象 
	private Object obj; 
	// sql 数据类型 
	private SqlType currentType; 
	// sql 构建器 
	private String sqlBuffer; 
	// sql 请求参数 
	private List<Object> param = new Vector<Object>(); 
	 
	// 创建枚举对象 
	public enum SqlType { 
		INSERT, UPDATE, DELETE 
	} 
 
	// 工具实列化(构造函数) 
	public EntityUtil(SqlType sqlType, Class<?> target, Object obj) { 
		this.obj =  
		this.target = target; 
		switch (sqlType) { 
		case INSERT: 
			currentType = SqlType.INSERT; 
			createInsert(); 
			break; 
		case UPDATE: 
			currentType = SqlType.UPDATE; 
			createUpdate(); 
			break; 
		case DELETE: 
			currentType = SqlType.DELETE; 
			createDelete(); 
			break; 
		} 
	} 
	 
	// set 和  get 方法 
	public Class<?> getTarget() { 
		return target; 
	} 
 
	public void setTarget(Class<?> target) { 
		this.target = target; 
	} 
 
//	public String getIdName() { 
//		return idName; 
//	} 
// 
//	public void setIdName(String idName) { 
//		this.idName = idName; 
//	} 
// 
//	public Object getIdValue() { 
//		return idValue; 
//	} 
// 
//	public void setIdValue(Object idValue) { 
//		this.idValue = idValue; 
//	} 
 
	public SqlType getCurrentType() { 
		return currentType; 
	} 
 
	public void setCurrentType(SqlType currentType) { 
		this.currentType = currentType; 
	} 
 
	public String getSqlBuffer() { 
		return sqlBuffer; 
	} 
 
	public void setSqlBuffer(String sqlBuffer) { 
		this.sqlBuffer = sqlBuffer; 
	} 
 
	public List<Object> getParam() { 
		return param; 
	} 
 
	public void setParam(List<Object> param) { 
		this.param = param; 
	} 
 
	public Object getObj() { 
		return obj; 
	} 
 
	public void setObj(Object obj) { 
		this.obj = obj; 
	} 
 
	public void createDelete() { 
		// 数据库表名称 
		String tableName = ClassConvertTable.getTableName(target.getClass()); 
		// 实体对象涉及属性值 
		Field[] fields = ReflectionUtils.getAllFields(target.getClass()); 
		StringBuffer sqlBuffer = new StringBuffer(); 
		sqlBuffer.append("DELETE FROM ").append(tableName).append(" WHERE "); 
		for (Field field : fields) { 
			if (!Modifier.isStatic(field.getModifiers())) { 
				ID id = field.getAnnotation(ID.class); 
				if (null != id) { 
					sqlBuffer.append(field.getName()).append("=?"); 
				} 
			} 
		} 
		// 赋值sql delete 语句 
		this.sqlBuffer = sqlBuffer.toString(); 
	} 
 
	 
 
	public void createUpdate() { 
		// 数据库表名称 
		String tableName = ClassConvertTable.getTableName(target.getClass()); 
		// 实体对象涉及属性值 
		Field[] fields = ReflectionUtils.getAllFields(target.getClass()); 
		StringBuffer sqlBuffer = new StringBuffer(); 
		sqlBuffer.append("UPDATE ").append(tableName).append(" SET "); 
		for (Field field : fields) { 
			if (!Modifier.isStatic(field.getModifiers())) { 
				ID id = field.getAnnotation(ID.class); 
				if (id == null) { 
					sqlBuffer.append(field.getName()).append("=?"); 
					param.add(ReflectionUtils.getFieldValue(target, field.getName())); 
				} else { 
//					idName = field.getName(); 
//					idValue = ReflectionUtils.getFieldValue(target, field.getName()); 
				} 
			} 
		} 
		// 处理条件 
//		if (idName == null) { 
//			throw new RuntimeException("not found of " + target.getClass() + "'s ID"); 
//		} 
//		sqlBuffer.append(" WHERE ").append(idName).append("=?"); 
//		param.add(idValue); 
		// 赋值sql update 语句 
		this.sqlBuffer = sqlBuffer.toString(); 
	} 
 
	public void createInsert() { 
		// 数据库表名称 
		String tableName = ClassConvertTable.getTableName(target); 
		// 实体对象涉及属性值 
		Field[] fields = ReflectionUtils.getAllFields(target); 
		StringBuffer sqlBuffer = new StringBuffer(); 
		sqlBuffer.append("INSERT INTO ").append(tableName).append("("); 
 
		for (Field field : fields) { 
			sqlBuffer.append(field.getName()).append(","); 
			// 填充请求参数 
			param.add(ReflectionUtils.getFieldValue(obj, field.getName())); 
	 
		} 
		int length = sqlBuffer.length(); 
		sqlBuffer.delete(length - 1, length).append(")values("); 
		int size = param.size(); 
		for (int x = 0; x < size; x++) { 
			if (x != 0) { 
				sqlBuffer.append(","); 
			} 
			sqlBuffer.append("?"); 
		} 
		sqlBuffer.append(")"); 
		// 赋值sql insert 语句 
		this.sqlBuffer = sqlBuffer.toString(); 
	} 
 
}

mapper组件依赖的pom.xml 文件

	<!-- 集中定义管理依赖版本号 --> 
	<dependencies> 
		<!--依赖***-common-base 基础包 --> 
		<dependency> 
			<groupId>com.***</groupId> 
			<artifactId>***commmon-base</artifactId> 
			<version>0.0.1-SNAPSHOT</version> 
		</dependency> 
		<!--依赖***-setting 全局配置包  --> 
		<dependency> 
			<groupId>com.***</groupId> 
			<artifactId>***-common-setting</artifactId> 
			<version>0.0.1-SNAPSHOT</version> 
		</dependency> 
	</dependencies>

base 模块的pom.xml 文件

<!-- 集中定义管理依赖版本号 --> 
	<properties> 
		<spring.version>4.3.21.RELEASE</spring.version> 
		<commons-lang.version>2.6</commons-lang.version> 
		<commons-codec.version>1.10</commons-codec.version> 
		<commons-lang3.version>3.8.1</commons-lang3.version> 
		<commons-net.version>3.6</commons-net.version> 
		<commons-io.version>2.6</commons-io.version> 
		<commons-collections.version>3.2.1</commons-collections.version> 
		<servlet-api.version>3.1.0</servlet-api.version> 
		<fastjson.version>1.2.48</fastjson.version> 
		<oshi-core.version>3.9.1</oshi-core.version> 
		<mysql-connector-java.version>8.0.11</mysql-connector-java.version> 
		<spring-data-redis.version>2.1.2.RELEASE</spring-data-redis.version> 
		<jedis.version>3.0.1</jedis.version> 
	</properties> 
 
	<!--digipower core 核心jar 包依赖 --> 
	<dependencies> 
		<!--spring IOC 功能 --> 
		<!--spring-core 核心包 --> 
		<dependency> 
			<groupId>org.springframework</groupId> 
			<artifactId>spring-core</artifactId> 
			<version>${spring.version}</version> 
		</dependency> 
		<!--spring-context 全局IOC容器 --> 
		<dependency> 
			<groupId>org.springframework</groupId> 
			<artifactId>spring-context</artifactId> 
			<version>${spring.version}</version> 
		</dependency> 
		<!--spring-context-support 拓展 --> 
		<dependency> 
			<groupId>org.springframework</groupId> 
			<artifactId>spring-context-support</artifactId> 
			<version>${spring.version}</version> 
		</dependency> 
		<!--spring-beans Bean注册 --> 
		<dependency> 
			<groupId>org.springframework</groupId> 
			<artifactId>spring-beans</artifactId> 
			<version>${spring.version}</version> 
		</dependency> 
		<!--spring-experession EL表达式 --> 
		<dependency> 
			<groupId>org.springframework</groupId> 
			<artifactId>spring-expression</artifactId> 
			<version>${spring.version}</version> 
		</dependency> 
 
		<!-- Spring AOP 功能 --> 
		<!-- 集成spring-aop --> 
		<dependency> 
			<groupId>org.springframework</groupId> 
			<artifactId>spring-aop</artifactId> 
			<version>${spring.version}</version> 
		</dependency> 
 
		<!-- Spring JDBC 功能 --> 
		<dependency> 
			<groupId>org.springframework</groupId> 
			<artifactId>spring-jdbc</artifactId> 
			<version>${spring.version}</version> 
		</dependency> 
		<dependency> 
			<groupId>org.springframework</groupId> 
			<artifactId>spring-tx</artifactId> 
			<version>${spring.version}</version> 
		</dependency> 
		<!--spring web功能  --> 
		<dependency> 
			<groupId>org.springframework</groupId> 
			<artifactId>spring-web</artifactId> 
			<version>${spring.version}</version> 
		</dependency> 
		<!-- mysql 数据库驱动jar --> 
		<dependency> 
			<groupId>mysql</groupId> 
			<artifactId>mysql-connector-java</artifactId> 
			<version>${mysql-connector-java.version}</version> 
		</dependency> 
 
		<!--集成AspectJ --> 
		<dependency> 
			<groupId>org.springframework</groupId> 
			<artifactId>spring-aspects</artifactId> 
			<version>${spring.version}</version> 
		</dependency> 
 
		<!--common-lang 常用工具包 --> 
		<dependency> 
			<groupId>commons-lang</groupId> 
			<artifactId>commons-lang</artifactId> 
			<version>${commons-lang.version}</version> 
		</dependency> 
		<!--commons-lang3 工具包 --> 
		<dependency> 
			<groupId>org.apache.commons</groupId> 
			<artifactId>commons-lang3</artifactId> 
			<version>${commons-lang3.version}</version> 
		</dependency> 
 
		<!--commons-codec 加密工具包 --> 
		<dependency> 
			<groupId>commons-codec</groupId> 
			<artifactId>commons-codec</artifactId> 
			<version>${commons-codec.version}</version> 
		</dependency> 
		<!--commons-net 网络工具包 --> 
		<dependency> 
			<groupId>commons-net</groupId> 
			<artifactId>commons-net</artifactId> 
			<version>${commons-net.version}</version> 
		</dependency> 
		<!--common-io 工具包 --> 
		<dependency> 
			<groupId>commons-io</groupId> 
			<artifactId>commons-io</artifactId> 
			<version>${commons-io.version}</version> 
		</dependency> 
		<!--common-collection 工具包 --> 
		<dependency> 
			<groupId>commons-collections</groupId> 
			<artifactId>commons-collections</artifactId> 
			<version>${commons-collections.version}</version> 
		</dependency> 
 
		<!-- 集成servlet --> 
		<dependency> 
			<groupId>javax.servlet</groupId> 
			<artifactId>javax.servlet-api</artifactId> 
			<version>${servlet-api.version}</version> 
		</dependency> 
		<!-- fastjson --> 
		<dependency> 
			<groupId>com.alibaba</groupId> 
			<artifactId>fastjson</artifactId> 
			<version>${fastjson.version}</version> 
		</dependency> 
 
		<!--windows 系统性能监控依赖jar --> 
		<dependency> 
			<groupId>com.github.oshi</groupId> 
			<artifactId>oshi-core</artifactId> 
			<version>${oshi-core.version}</version> 
		</dependency> 
		<!-- redis 功能模块集成  --> 
		<!-- redis-spring适配器 --> 
		<dependency> 
			<groupId>org.springframework.data</groupId> 
			<artifactId>spring-data-redis</artifactId> 
			<version>${spring-data-redis.version}</version> 
		</dependency> 
		<!--redis客户端 依赖 --> 
		<dependency> 
			<groupId>redis.clients</groupId> 
			<artifactId>jedis</artifactId> 
			<version>${jedis.version}</version> 
		</dependency> 
	</dependencies>

在其他业务模块调用mapper封装组件

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们