spring boot 项目操作数据库遇到的一些问题

spring boot项目的数据库连接的一些问题

楼主现在练习spring boot项目,连接数据库时遇到的一点问题和大家分享

一 、数据库驱动

在spring boot2.x中,mysql的依赖为
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

mysql-connector-java的版本是6.x或者是8.x
这时数据库驱动要使用com.mysql.cj.jdbc.Driver,不能使用com.mysql.jdbc.Driver

否则数据库会报如下错误:
    * Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
如果手动修改mysql-connector-java的版本,修改为5.x,则com.mysql.cj.jdbc.Driver会报红,需要使用com.mysql.jdbc.Driver

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.46</version>
    </dependency>
如果在5.x中使用com.mysql.cj.jdbc.Driver,项目报错java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver

有的人在8.x中使用com.mysql.jdbc.Driver也不会报错,只是会有一个警告,但是楼主还是建议6.x和8.x的版本使用com.mysql.cj.jdbc.Driver

二、数据库时区相关问题

1. 数据库时区报错问题

    楼主的项目是spring boot 2.1.4,默认mysql-connector-java的版本为:<version>8.0.15</version>
    如果不指定时区   spring.datasource.url=jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=UTF-8
    则项目报错 :
        * java.sql.SQLException: 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.
    解决方案 :在后面加上&serverTimezone=UTC,制定时区为世界标准时间,同时就带来了第二个问题
2. 数据库时间和本地时间不一致的问题
    楼主的本地时间 : 
    楼主是使用的spring data jpa 来进行数据库的操作
    建表语句为:
    CREATE TABLE `product_category` (
          `category_id` INT(11) NOT NULL AUTO_INCREMENT,
          `category_name` VARCHAR(64) NOT NULL COMMENT '类目名字',
          `category_type` INT(11) NOT NULL COMMENT '类目编号',
          `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
          `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
          PRIMARY KEY (`category_id`)
    ) ENGINE=INNODB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
    楼主的实体类为

    @Entity
    @ToString
    @Data
    public class ProductCategory implements Serializable {
        /**
        * 类目id
        */
        @Id@GeneratedValue(strategy = GenerationType.AUTO)
        private Integer categoryId;
        /**
        * 类目名称
        */
        private String categoryName;
        /**
        * 类目编号
        */
        private Integer categoryType;
        /**
        * 创建时间
        */
        private Date createTime;
        /**
        * 修改时间
        */
        private Date updateTime;
    }

    楼主使用的编辑器为idea,添加了lombok这个插件
    创建 dao层

    @Repository
    public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer> {
    }
    进行数据插入
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ProductCategoryTest {

        @Autowired
        private ProductCategoryRepository repository;


        @Test
        public void testProductCategorySave(){
            ProductCategory category = new ProductCategory();
            category.setCategoryName("女生最爱");
            category.setCategoryType(3);
            repository.save(category);
        }

    }
    此时查询数据库,时间为 : 

    * 此时数据库的时间和我的本地时间相差了8小时,楼主瞬间懵比,这个不能差啊,楼主立马各种百度
    * 第一个解决方案是修改serverTimezone,楼主把url修改为jdbc:mysql://192.168.200.134/weixinsell?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    * 其实就是把serverTimezone修改为Asia/Shanghai,but,没用,数据库的时间还是要早8小时
    * 
    * 楼主继续修改serverTimezone为Asia/Hongkong
    * 然后程序直接报错:
    * java.sql.SQLException: No timezone mapping entry for 'Asia/Hongkong'
    * 楼主接着修改serverTimezone为Hongkong
    * 但是依然不成功
    * 程序为:
        @Test
        public void testProductCategoryUpdate(){
        ProductCategory category = new ProductCategory();
        category.setCategoryId(6);
        category.setCategoryName("男生最爱");
        category.setCategoryType(3);
        repository.save(category);
    }
    * 
    * 楼主接着百度,立马找到了第二种方法
    *   @SpringBootApplication
        public class SellApplication {

            @PostConstruct
            void started() {
            //TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
            TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
            }

            public static void main(String[] args) {
                SpringApplication.run(SellApplication.class, args);
            }

        }
    * 但是程序运行依然不成功,在程序中楼主的PostConstruct使用了Asia/Shanghai和GMT+8都没用
    * 再次百度也无非是修改url中的serverTimezone,楼主修改成GMT+8那些都没用
    * 接着百度,第三种方法是set/get方法上面加注解
    * @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    * 楼主果断的试了一下,然而依然没有成功

解决方案

1. 楼主在数据库中直接进行数据的查询
2. 如图
    * 
    * 数据库层面的时间直接比当前时间早8小时
    * 楼主这时候赶紧的修改数据库的时区
    * 如图 : 数据库方面修改成功
    * 
3. 楼主赶紧的修改程序
    * 如图 : 终于成功了
    * but楼主又发现了一个大问题
4. 大家注意这个男生最爱,create_time和update_time是一样的,楼主明明对create_time这个字段没有设置函数,怎么他也修改了,楼主立马去百度
    * 百度上面一个哥们让楼主的pojo类上面添加注解@DynamicUpdate
    * 楼主果断的进行测试,run一下项目
    * 大家看一下图片,果然没有用,楼主赶紧的接着百度,一个小伙伴也有过类似问题
   * 这个小伙伴让我把这两个字段在pojo类中删掉,楼主赶紧的试了一下,修改pojo类为: @Entity @ToString @Data public class ProductCategory implements Serializable { /** * 类目id */ @Id@GeneratedValue(strategy = GenerationType.AUTO) private Integer categoryId; /** * 类目名称 */ private String categoryName; /** * 类目编号 */ private Integer categoryType; } * run一下项目 * 代码是: @Test public void testProductCategoryUpdate(){ ProductCategory category = new ProductCategory(); category.setCategoryId(6); category.setCategoryName("年轻最爱"); repository.save(category); } * 成功了,不容易啊,一下子遇到这么多问题 *
  * 但是pojo类中有这两个字段,时间还是解决不了,楼主接着查阅资料,待查出来的时候就改帖子,也希望各路大神批评指正

猜你喜欢

转载自www.cnblogs.com/cpq1995/p/10889616.html