菜鸟搭建web服务笔记3

2019-04-06  本文已影响0人  DizzyDwarf

回顾

上一篇笔者用Spring MVC框架改造了web服务,在这一篇中将通过集成MyBatis框架使用MySQL存储数据,同时利用servlet原生的方法实现处理图片上传的功能。

环境

集成MyBatis

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.0</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.13</version>
</dependency>
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/yourdbname"/>

注意driver用的是com.mysql.cj.jdbc.Driver,而不是com.mysql.jdbc.Driver,后者已经过时了。

运行测试

com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

在配置的url中增加参数serverTimezone=UTC即可

<property name="url" value="jdbc:mysql://localhost:3306/yourdbname?serverTimezone=UTC"/>

servlet原生方式处理文件

public void upload(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    req.setCharacterEncoding("utf-8");
    Collection<Part> parts = req.getParts();
    for (Part part : parts) {
        InputStream in = part.getInputStream();
        // your code here
    }
}

总结

上一篇下一篇

猜你喜欢

热点阅读