当前位置: 首页 > news >正文

根据bean的名称获取bean,静态方法查询数据库

根据bean名称获取bean

1.先创建bean,如template

package com.test.game.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;import javax.sql.DataSource;@Configuration
public class TemplateConfig {@Beanpublic JdbcTemplate mysqlTemplate(DataSource dataSource){return new JdbcTemplate(dataSource);}
}

2.创建获取bean的工具

package com.test.game.utils;import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;@Component
public class SpringContextUtil implements ApplicationContextAware {private static ApplicationContext context = null;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {context = applicationContext;}/*** 获取当前环境* @return*/public static String getActiveProfile(){return context.getEnvironment().getActiveProfiles()[0];}/*** 根据bean名称获取bean* @param name* @return*/public static Object getBean(String name){return context.getBean(name);}/*** 通过class获取bean* @param clazz* @return* @param <T>*/public static <T> T getBean(Class<T> clazz){return context.getBean(clazz);}/*** 根据bean名和class获取bean* @param name* @param clazz* @return* @param <T>*/public static <T> T getBean(String name,Class<T> clazz){return context.getBean(name,clazz);}}

3.使用工具获取bean

package com.test.game.utils;import com.test.game.entity.PlayGift;
import com.test.game.enums.CostType;
import com.test.game.enums.DrawType;
import com.test.game.enums.GiftState;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;public class CommonDrawUtils {public static List<PlayGift> getGifts(int num,String drawType){JdbcTemplate mysqlTemplate = (JdbcTemplate)SpringContextUtil.getBean("mysqlTemplate");String sql = "select * from play_gift where state = ? and box_draw = ? and pet_draw = ? and type = ? and draw_type = ?";String costType = CostType.DRAMOND.name();if (DrawType.AMETHYST.name().equals(drawType) || DrawType.GOLD.name().equals(drawType)){costType = CostType.AMETHYST.name();}List<PlayGift> playGifts = mysqlTemplate.query(sql, new Object[]{GiftState.UP.getCode(), true, true, costType, drawType},new RowMapper<PlayGift>() {@Overridepublic PlayGift mapRow(ResultSet resultSet, int i) throws SQLException {PlayGift playGift = new PlayGift();playGift.setState(resultSet.getBoolean("state"));playGift.setBoxDraw(resultSet.getBoolean("box_draw"));playGift.setPetDraw(resultSet.getBoolean("pet_draw"));playGift.setType(resultSet.getString("type"));playGift.setDrawType(resultSet.getString("draw_type"));return playGift;}});return playGifts;}
}
http://www.lryc.cn/news/418175.html

相关文章:

  • 剪画小程序:音频剪辑新手入门:基础操作指南!
  • IDEA中maven jar下载失败问题处理
  • C++中,函数返回const类型有什么作用,请举例说明
  • Html详解——Vue基础
  • 【安规电容知识点总结】
  • R9000P 双系统安装 win11 和 ubuntu
  • 8月8日笔记
  • 【单片机开发软件】使用VSCode开发STM32环境搭建
  • 第十五届蓝桥杯大赛青少组——赛前解析(算法)
  • 工作助手C#研究笔记(5)
  • 【kali靶机之serial】--反序列化漏洞实操
  • 学习大数据DAY34 面向对象思想深化练习 将从豆瓣爬取的数据置入自己搭建的网站上
  • 【开端】通过Java 过滤器灵活配置URL访问权限,并返回403
  • 【C++综合项目】——基于Boost库的搜索引擎(手把手讲解,小白一看就会!!)
  • 强化阶段《660》和《880》哪本优先级高?
  • Redis远程字典服务器(2) —— 全局命令
  • Android平台如何不推RTMP|不发布RTSP流|不实时录像|不回传GB28181数据时实时快照?
  • tomcat文件上传漏洞练习
  • 项目实战_图书管理系统(简易版)
  • Gazebo之MyRobot建立
  • WPF学习(5)- Border控件(边框布局)+GridSplitter分割窗口
  • ADAS芯片及方案
  • 5 mysql 查询语句
  • 从网络上下载并展示图像数据
  • Machine-Learning 机器学习
  • CSP 2023 普及组第一轮 - CSP/S 2023初试题 基础部分解析
  • 解锁IPython的跨平台魔法:深入探索%%script命令的神秘力量
  • 如何避免项目发布后用户从浏览器WebPack中看到源码
  • java学习19VUE
  • Redis7(四)哨兵、集群