设计得好的网站推荐如何给网站建设提意见

张小明 2026/1/11 5:48:58
设计得好的网站推荐,如何给网站建设提意见,淮安网络营销,百度竞价排名怎么做数据源自动配置剖析数据源配置方式首先我们需要选择数据库驱动的库文件dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.33/version /dependency配置数据库连接spring:d…数据源自动配置剖析数据源配置方式首先我们需要选择数据库驱动的库文件dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version8.0.33/version /dependency配置数据库连接spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3307/springboot username: root password: 123456配置spring-boot-start-jdbcdependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-jdbc/artifactId /dependency编写测试类Autowired private DataSource dataSource; Test public void contextLoads()throws SQLException{ Connection connection dataSource.getConnection(); System.out.println(connection); }连接池配置方式SpringBoot提供了三种连接池HikariCPCommons DBCP2Tomcat JDBC Connection Pool其中SpringBoot默认使用HikariCPdependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-jdbc/artifactId /dependency要使用其他两个要改为dependency groupIdorg.apache.commons/groupId artifactIdcommons-dbcp2/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-jdbc/artifactId exclusions exclusion groupIdcom.zaxxer/groupId artifactIdHikariCP/artifactId /exclusion /exclusions /dependencydependency groupIdorg.apache.tomcat/groupId artifactIdtomcat-jdbc/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-jdbc/artifactId exclusions exclusion groupIdcom.zaxxer/groupId artifactIdHikariCP/artifactId /exclusion /exclusions /dependency数据库自动配置为什么前面我们说SpringBoot默认使用HikariCP这是在哪里指定的首先我们需要找到DataSourceAutoConfiguration这个类Conditional(PooledDataSourceCondition.class)这个告诉我们指定的配置文件中必须要有type属性。现在我们进入DataSourceConfiguration这个里面里面配置类SpringBoot启动的连接池。private static final String[] DATA_SOURCE_TYPE_NAMES new String[] {com.zaxxer.hikari.HikariDataSource, org.apache.tomcat.jdbc.pool.DataSource, org.apache.commons.dbcp2.BasicDataSource}所以我们可以看出来在没有指定Type时默认就是HikariDataSource。Druid连接池的配置首先么要整合druid数据源dependency groupIdcom.alibaba/groupId artifactIddruid-spring-boot-starter/artifactId version1.1.10/version /dependency在application.yml中引入druid的相关配置spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3307/springboot username: root password: 123456 type: com.alibaba.druid.pool.DruidDataSource整合的配置类public class DruidConfig { ConfigurationProperties(prefix spring.datasource) Bean public DataSource druidDataSource(){ return new DruidDataSource(); } }SpringBoot整合Mybatis首先我们要添加maven依赖dependency groupIdorg.mybatis.spring.boot/groupId artifactIdmybatis-spring-boot-starter/artifactId version1.3.2/version /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency所以我们需要打开MybatisAutoConfiguration这个类Mybatis自动配置源码剖析springBoot项目最核心的就是自动加载配置该功能依赖的是一个注解SpringBootApplication中的EnableAutoConfigurationEnableAutoConfiguration主要是通过Import(AutoConfigurationImportSelector.class)类来加载的MybatisAutoConfiguration其中有个MybatisProperties该类是对应mybatis的配置文件有个SqlSessionFactory方法作用是创建SqlSession类Configuration类mybatis最主要的类保存着与mybatis相关的东西SqlSessionTemplate作用是mapperProoxy代理类有关然后我们要看MapperScan注解通过进一步分析源码我们可以得出以下结论MapperScan这个定义是扫描指定包下面的mapper接口然后设置每个mapper接口的beanClass属性为MapperFactoryBean类型并加入到spring的bean容器中。MapperFactoryBean实现了FactoryBean接口所以当spring从待实例化的bean容器中变量到这个bean并开始执行实例化使返回的对象实际上就是getObject方法中返回的对象。最后我们看一下MapperFactoryBean的getObject方法实际上返回的就是mybatis中通过getMapper拿到的对象熟悉mybatis源码的就应该清楚这个就是mybatis通过动态代理生成的mapper接口实现类。到此mapper接口现在也通过动态代理生成了实现类并且注入到spring的bean容器中了之后使用者就可以通过Autowired或者getBean等方法从spring容器中获取到了。SpringMybatis实现动态数据源切换动态数据源介绍现在我们的项目中订单模块氛围正向和逆向两个部分正向模块中记录了订单的基本信息包括订单基本信息订单商品信息优惠卷信息发票信息账期信息结算信息订单备注信息收货人信息等逆向模块主要包含了商品退货信息和维修信息当我们数据量超过500万时就需要考虑分库分表和读写分类需要动态切换到对应的数据库中。解决思路Spring内置了一个AbstractRoutingDataSource它可以把多个数据源配置为一个Map然后根据不同的key返回不同的数据源。编码实战首先我们需要准备基础的环境Data public class Product { private Integer id ; private String name; private Double price; }Mapper public interface ProductMapper { //查询主数据库 Select(select * from product) public ListProduct getAllProductsM(); //查询从数据库 Select(select * from product) public ListProduct getAllProductsS(); }Service public class ProductServiceImpl implements ProductService { Autowired private ProductMapper productMapper; Override public void getAllProductsM() { ListProduct products productMapper.getAllProductsM(); System.out.println(MyBatis查询结果products); } Override public void getAllProductsS() { ListProduct products productMapper.getAllProductsS(); System.out.println(MyBatis查询结果products); } }RestController RequestMapping(/product) public class ProductController { Autowired private ProductService productService; GetMapping(/getAllProductsM) public String getAllProductsM() { productService.getAllProductsM(); return master; } GetMapping(/getAllProductsS) public String getAllProductsS() { productService.getAllProductsS(); return slave; } }配置类Configuration Slf4j public class MyDataSourceConfiguration { Bean(name dataSourceM) DataSource dataSourceM(){ log.info(主数据库); return DataSourceBuilder.create().build(); } Bean(name dataSourceS) DataSource dataSourceS(){ log.info(从数据库); return DataSourceBuilder.create().build(); } }配置文件spring: druid: datasource: master: url: jdbc:mysql://localhost:3307/product_m username: root password: 123456 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver slave: url: jdbc:mysql://localhost:3307/product_s username: root password: 123456 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver其次需要编写RoutingDataSource把两个真实的数据源代理为一个动态数据源Slf4j public class RoutingDataSource extends AbstractRoutingDataSource { Override protected Object determineCurrentLookupKey() { return master数据库; } Bean Primary DataSource primaryDataSource(Autowired Qualifier(dataSourceM)DataSource dataSourceM, Autowired Qualifier(dataSourceS)DataSource dataSourceS){ log.info(正在动态创建数据库); MapObject, Object map new HashMap(); map.put(master数据库, dataSourceM); map.put(slave数据库, dataSourceS); RoutingDataSource routingDataSource new RoutingDataSource(); routingDataSource.setTargetDataSources(map); routingDataSource.setDefaultTargetDataSource(dataSourceM); return routingDataSource; } }现在RoutingDataSource配置好了但是路由的选择是写死的永远返回master数据库。现在问题来了我们该如何存储动态选择的key已经在哪里设置key使用ThreadLocal存储key最合适。public class RoutingDataSourceContext { static final ThreadLocalString threadLocalDataSourceKey new ThreadLocal(); public static String getDataSourceKey() { String key threadLocalDataSourceKey.get(); return key null ? master数据库 : key; } public RoutingDataSourceContext(String key) { threadLocalDataSourceKey.set(key); } public void close() { threadLocalDataSourceKey.remove(); } }现在我们修改RoutingDataSource获取key的代码Override protected Object determineCurrentLookupKey() { return RoutingDataSourceContext.getDataSourceKey() ; }RestController RequestMapping(/product) public class ProductController { Autowired private ProductService productService; GetMapping(/getAllProductsM) public String getAllProductsM() { RoutingDataSourceContext routingDataSourceContext new RoutingDataSourceContext(master数据库); productService.getAllProductsM(); return master; } GetMapping(/getAllProductsS) public String getAllProductsS() { RoutingDataSourceContext routingDataSourceContext new RoutingDataSourceContext(salve数据库); productService.getAllProductsS(); return slave; } }测试结果2025-12-12 14:07:48.541 [http-nio-8080-exec-3] DEBUG com.guslegend.mapper.ProductMapper.getAllProductsS - Preparing: select * from product 2025-12-12 14:07:48.541 [http-nio-8080-exec-3] DEBUG com.guslegend.mapper.ProductMapper.getAllProductsS - Parameters: 2025-12-12 14:07:48.543 [http-nio-8080-exec-3] DEBUG com.guslegend.mapper.ProductMapper.getAllProductsS - Total: 1 MyBatis查询结果[Product(id1, name桌子, price100.0)] 2025-12-12 14:07:50.442 [http-nio-8080-exec-4] DEBUG com.guslegend.mapper.ProductMapper.getAllProductsM - Preparing: select * from product 2025-12-12 14:07:50.442 [http-nio-8080-exec-4] DEBUG com.guslegend.mapper.ProductMapper.getAllProductsM - Parameters: 2025-12-12 14:07:50.443 [http-nio-8080-exec-4] DEBUG com.guslegend.mapper.ProductMapper.getAllProductsM - Total: 1 MyBatis查询结果[Product(id1, name桌子, price100.0)]优化上面需要读取数据库的地方就要加上这样的一段话是否太过于复杂了RoutingDataSourceContext routingDataSourceContext new RoutingDataSourceContext();我们可以使用声明式事务管理或者自定义注解来解决首先我们要添加aop的依赖dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-aop/artifactId /dependency自定义注解Target(ElementType.METHOD) Retention(RetentionPolicy.RUNTIME) public interface RoutingWith { String value() default master; }切面类Aspect Component public class RoutingAspect { Around(annotation(routingWith)) public Object routingWithDataSource(ProceedingJoinPoint proceedingJoinPoint,RoutingWith routingWith)throws Throwable{ String key routingWith.value(); RoutingDataSourceContext routingDataSourceContext new RoutingDataSourceContext(key); return proceedingJoinPoint.proceed(); } }改造controller方法RestController RequestMapping(/product) public class ProductController { Autowired private ProductService productService; RoutingWith(master) GetMapping(/getAllProductsM) public String getAllProductsM() { // RoutingDataSourceContext routingDataSourceContext new RoutingDataSourceContext(master数据库); productService.getAllProductsM(); return master; } RoutingWith(slave) GetMapping(/getAllProductsS) public String getAllProductsS() { // RoutingDataSourceContext routingDataSourceContext new RoutingDataSourceContext(salve数据库); productService.getAllProductsS(); return slave; } }至此我们实现了注解动态选择数据源功能。
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

搭建网站价格做一个小程序的步骤

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 创建一个疫情数据可视化项目,使用vue-baidu-map实现:1.以省级行政区划显示不同颜色表示疫情风险等级;2.鼠标悬停显示该省份确诊/治愈/死亡数据&a…

张小明 2026/1/9 19:16:59 网站建设

汉中免费做网站公司莱芜网站优化是什么

随着生成式 AI 成为品牌流量的核心入口,GEO(生成式引擎优化)服务商的选择直接影响企业在 AI 搜索中的曝光、转化与合规风险。当前市场上服务商能力参差不齐,“效果虚标”“适配性差” 等问题常见。基于对 800 余家企业合作案例的跟…

张小明 2026/1/8 15:43:44 网站建设

网站制作熊猫建站国药控股cms系统

概述 (Overview) 远程命令/代码执行 (RCE) 漏洞允许攻击者在目标服务器上执行任意的操作系统命令或应用程序代码。这是最高危的漏洞类型之一,成功利用通常意味着攻击者可以完全控制服务器。 远程命令执行: 指的是应用程序接收用户输入,并将其&#xff…

张小明 2026/1/8 15:52:28 网站建设

郑州市精神文明建设 网站前端做的网站

目录 前置知识 漏洞分析 Part1 Part2 Part3 漏洞复现 本地复现 远程复现 其他思考 很好的语言,使你的漏洞旋转😂 前置知识 RSC RSC(React Server Components,React 服务器组件)是一种 React 的新型组件模型…

张小明 2026/1/8 16:05:15 网站建设

佛山做pc端网站域名查询阿里云

第一章:Open-AutoGLM 监管政策影响分析随着生成式人工智能技术的快速发展,Open-AutoGLM 作为开源大语言模型的重要代表,正面临日益复杂的全球监管环境。各国对AI系统的透明度、数据隐私和内容安全提出更高要求,直接影响该模型的开…

张小明 2026/1/8 17:20:07 网站建设

二级网站建设基本情况云南公司网站制作

LobeChat UGC内容激励方案 在AI助手从实验室走向千家万户的今天,一个关键问题摆在开发者面前:如何让开源项目不只是“能用”,而是真正“被广泛使用”?答案或许不在于功能堆砌,而在于构建一种用户愿意参与、乐于贡献的内…

张小明 2026/1/8 17:36:03 网站建设