Mac idea 通用mapper生成中文注释乱码问题

1、查看mysql编码格式是否为utf-8

在/usr/local/mysql/bin路径下输入./mysql -u root -p命令通过密码登录mysql,或者直接通过Navicat新建查询,输入show variables like '%char%',mysql的默认编码格式是latin1,如果不是的话进行修改,首先在/etc/mysql路径下新建一个my.cnf文件:vim my.cnf,再把以下内容粘贴进去:

            #    change in this file to the variables' values below will be ignored and
            #    overridden by the content of the master.info file, unless you shutdown
            #    the slave server, delete master.info and restart the slaver server.
            #    For that reason, you may want to leave the lines below untouched
            #    (commented) and instead use CHANGE MASTER TO (see above)
            #
            # required unique id between 2 and 2^32 - 1
            # (and different from the master)
            # defaults to 2 if master-host is set
            # but will not function as a slave if omitted
            #server-id       = 2
            #
            # The replication master for this slave - required
            #master-host     =   <hostname>
                #
                # The username the slave will use for authentication when connecting
                # to the master - required
                #master-user     =   <username>
                #
                # The password the slave will authenticate with when connecting to
                # the master - required
                #master-password =   <password>
                #
                # The port the master is listening on.
                # optional - defaults to 3306
                #master-port     =  <port>
                #
                # binary logging - not required for slaves, but recommended
                #log-bin=mysql-bin

                # Uncomment the following if you are using InnoDB tables
                #innodb_data_home_dir = /usr/local/mysql/data
                #innodb_data_file_path = ibdata1:10M:autoextend
                #innodb_log_group_home_dir = /usr/local/mysql/data
                # You can set .._buffer_pool_size up to 50 - 80 %
                # of RAM but beware of setting memory usage too high
                #innodb_buffer_pool_size = 16M
                #innodb_additional_mem_pool_size = 2M
                # Set .._log_file_size to 25 % of buffer pool size
                #innodb_log_file_size = 5M
                #innodb_log_buffer_size = 8M
                #innodb_flush_log_at_trx_commit = 1
                #innodb_lock_wait_timeout = 50

                [mysqldump]
                quick
                max_allowed_packet = 16M

                [mysql]
                no-auto-rehash
                # Remove the next comment character if you are not familiar with SQL
                #safe-updates
                default-character-set=utf8

                [myisamchk]
                key_buffer_size = 20M
                sort_buffer_size = 20M
                read_buffer = 2M
                write_buffer = 2M

                [mysqlhotcopy]
                interactive-timeout

这段代码是网上找来的,不知道可不可行,如果配置了这个文件还不行的话(比如说像我这样的),在mysql中使用下面几句sql语句:

SET character_set_client = 'utf8'; 
SET character_set_results = 'utf8'; 
SET character_set_connection = 'utf8';

反正加完之后我的就好了
在这里插入图片描述

2、查看idea项目编码格式

打开项目配置
在这里插入图片描述
将编码格式都改成utf-8,Apply
在这里插入图片描述

3、在项目配置文件application.properties里配置

spring.datasource.url=jdbc:mysql://localhost:3306/项目名?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&useSSL=false
里面记得不要换行或者加空格

4、设置项目读取编码格式

idea右下角修改为utf-8,注意reload和convert的区别,前者类似预览,后者会覆盖原文件,使用时请谨慎
在这里插入图片描述
在这里插入图片描述

5、修改edit configurations

添加-Dfile.encoding=UTF-8
在这里插入图片描述
在这里插入图片描述

6、修改utf8为utf8mb4

参考文档:https://mathiasbynens.be/notes/mysql-utf8mb4#utf8-to-utf8mb4
大致意思就是,由于种种原因,mysql的utf8被弃用,后改用utf8mb4,所以上面所有的地方用到utf8的都改成utf8mb4。

7、修改数据库连接方式

无意中点开数据库属性
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
保存

总结

如果进行了以上所有操作最后还是乱码的话,抱歉我也帮不了你,以上就是我的毕生所学了 。持续了2天的问题,最后以这种方式解决,心情复杂。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用MyBatis等持久层框架时,通常需要编写Mapper层的SQL代码。为了减少手动编写SQL的工作量,可以考虑使用代码生成工具来自动生成Mapper层的SQL代码。 一种常见的做法是使用MyBatis Generator(简称MBG)来生成Mapper层的代码。MBG是一个功能强大的代码生成工具,可以根据数据库表结构自动生成Mapper接口、XML文件以及实体类。 下面是一个简单的步骤来实现Mapper层SQL代码的自动生成: 1. 配置MBG:在项目中引入MBG的依赖,并配置MBG的配置文件。配置文件中需要指定数据库连接信息、生成的目标包结构、生成规则等。 2. 定义表结构:在数据库中创建表,并确保表的命名规范符合MBG的要求。MBG会根据表结构生成对应的实体类和Mapper接口。 3. 编写MBG配置文件:在MBG配置文件中定义要生成的表、生成的目标路径、生成的文件类型等信息。可以使用XML或者Java代码两种方式来编写MBG配置文件。 4. 运行MBG:使用命令行或者IDE插件运行MBG,让其读取配置文件并生成代码。MBG会根据配置文件中定义的规则,自动生成Mapper接口、XML文件和实体类。 5. 使用生成的代码:将生成Mapper接口、XML文件和实体类拷贝到项目中的对应位置,并在业务代码中使用它们。 通过以上步骤,就可以实现Mapper层SQL代码的自动生成。这样可以大大减少手动编写SQL的工作量,提高开发效率。当数据库表结构有变动时,只需要重新运行MBG即可更新生成的代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值