Spring和SQLServer2005不得不说的那点事儿

本文记录了一次使用Java连接SQL Server数据库的经历,由于错误地使用了不匹配的JDBC驱动而导致的问题。文中详细介绍了SQL Server 2000与2005版本之间在驱动名称及URL写法上的区别。

今天要从SQLServerl中取些数据,不曾想有段时间没有操纵SQLServer了,竟然被SQLServer戏耍了一回,

 

 

原因如下:

1、SQLServerl2000和SQLServerl2005的JDBC驱动居然不能通用-_-!

 

2、SQLServerl2000和SQLServerl2005的驱动Class名居然也不一样!!

2000:  <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>

2005:  <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>

 

3、SQLServerl2000和SQLServerl2005的URL的写法居然还不一样!!!

2000:  <property name="url" value="jdbc:microsoft:sqlserver://......."/>

2005:  <property name="url" value="jdbc:sqlserver://......."/>

<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>*.properties</include> <include>*.yml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <includes> <include>*.sql</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <includes> <include>static/**</include> </includes> <targetPath>${project.build.outputDirectory}</targetPath> </resource> </resources> server.port=8081 server.servlet.context-path=/demo logging.level.com.esoft.crm.mapper=debug spring.datasource.url=jdbc:h2:file:./test;MODE=MySQL;CASE_INSENSITIVE_IDENTIFIERS=TRUE;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.h2.console.enabled=true spring.h2.console.path=/h2-console #spring.sql.init.mode=always #spring.sql.init.schema-locations=classpath:schema.sql #spring.sql.init.data-locations=classpath:data.sql spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect logging.level.org.hibernate.SQL=debug logging.level.org.hibernate.type.descriptor.sql=trace spring.datasource.hikari.connection-timeout=30000 spring.datasource.hikari.maximum-pool-size=10 #spring.sql.init.mode=always #spring.sql.init.schema-locations=classpath:schema.sql #spring.sql.init.data-locations=classpath:data.sql spring.datasource.schema=classpath:schema.sql spring.datasource.data=classpath:data.sql spring.datasource.initialization-mode=always spring.web.resources.static-locations=classpath:/static/ spring.mvc.static-path-pattern=/static/** mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.esoft.crm.po,com.esoft.crm.vo 且前端页面资源(css,img,index.html)等都存在与resource/static下,且没有打包,直接在idea环境下运行,运行后前端打不开,怎么回事
07-03
在你提供的配置信息中,前端页面资源(如 `index.html`、CSS、JS、图片等)存放在 `src/main/resources/static/` 目录下,并且你使用的是 Spring Boot 框架。但在 IDEA 中运行项目时,无法访问这些静态资源,这通常与资源位置配置或访问路径有关。 --- ### ✅ 问题分析 1. **Spring Boot 静态资源默认目录**: - Spring Boot 默认会在以下目录查找静态资源: ``` /static /public /resources /META-INF/resources ``` - 如果你的 HTML 文件放在 `src/main/resources/static/` 下,Spring Boot 是可以识别的。 2. **访问路径问题**: - 你设置了: ```properties spring.mvc.static-path-pattern=/static/** spring.web.resources.static-locations=classpath:/static/ ``` - 这意味着你需要通过 `/static/xxx.js`, `/static/xxx.css`, `/static/index.html` 来访问这些资源。 - 所以如果你直接访问 `http://localhost:8081/demo/index.html` 是不会成功的,必须访问: ``` http://localhost:8081/demo/static/index.html ``` 3. **HTML 页面作为入口的问题**: - Spring Boot 并不会自动将 `index.html` 设置为根路径 `/` 的映射页面,除非你显式地配置一个控制器来返回它。 --- ### ✅ 解决方案 #### 方案一:确保正确访问路径 假设你的文件结构如下: ``` src/ └── main/ └── resources/ └── static/ ├── index.html ├── css/ └── js/ ``` 那么你应该通过以下方式访问: ``` http://localhost:8081/demo/static/index.html ``` #### 方案二:配置控制器将根路径指向 index.html 创建一个简单的控制器类: ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class IndexController { @GetMapping("/") public String getIndexPage() { return "forward:/static/index.html"; } } ``` 这样你可以通过访问: ``` http://localhost:8081/demo/ ``` 就会跳转到 `index.html` 页面。 #### 方案三:检查是否与其他框架冲突(如 Thymeleaf) 如果你引入了模板引擎(如 Thymeleaf),Spring Boot 会优先从模板目录加载页面,而不是静态资源目录。请确认你没有引入 Thymeleaf 或其他模板引擎依赖,或者将其关闭。 --- ### ✅ 总结 - 确保访问路径是 `/demo/static/index.html` - 添加控制器使 `/` 能跳转到 `index.html` - 检查是否有模板引擎干扰静态资源处理 ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值