Spring
编写配置文件需要注意的点:
MySQL5.0+跟MySQL8.0+:
在以前使用mysql5.0+的jar包,连接池使用的驱动通常是:com.mysql.jdbc.Driver
在现在使用mysql8.0+的jar包中,建议使用连接池驱动:com.mysql.cj.jdbc.Driver”。通过SPI自动注册驱动程序,通常不需要手动加载驱动程序类。
XML中:
MySQL5.0+中的数据库连接信息:"jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"
MySQL8.0+中要设置时区信息:"jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"
- 在xml文件中,分号;要用&表示!
- useSSL=true时 数据库连接 安全认证不通过 解决办法:将useSSL true改为false (可以使用)
properties数据库连接信息:
db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC jdbc.username=root jdbc.password=xxx
通常我们都会写一个db.properties文件保存数据库连接信息,然后在xml文件中通过:<context:property-placeholder location="classpath:db.properties"/>引入配置文件
需要注意的是在properties中要使用&进行配置的分割,这个坑很重要!!!不然真的是找bug找一天~
- 使用&
SSM项目中解决乱码方案(尽量都配)
1、Tomcat
需要注意一点:tomcat 8后的版本是默认配置的,这是针对8以前的版本配置
- 找到Tomcat安装目录的conf找到Service.xml文件
- 修改<Connector>标签中的内容
2、请求乱码的过滤
在web.xml中配置过滤器:
<filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3、响应乱码
在spring的配置文件applicationContext.xml中设置
<!--启用Spring MVC的注解开发模式 并设置编码格式--> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=utf-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
4、编写Controller代码设置
/*设置编码*/ request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8");
5、更改IDEA的编码
WEB项目的部署
在我使用IDEA过程中,被Tomcat、Maven什么的本地配置,IDEA环境设置都踩过很多的坑,这里来记录一下,我们在部署WEB项目的时候需要注意哪些问题才能正确配置好一个web
- 1、打开项目结构设置(Ctrl+Alt+Shift+S)
- 2、设置对应的配置
tomcat指定即可: