场景:利用Mybatis向mysql中插入数据时,出现中文乱码
思想:猜测是url出现问题
原因:
第①步:在db.properties中的写法是
db.driver=com.mysql.jdbc.Driver db.url=jdbc:mysql://xxxx:3306/test_mybatis?rewriteBatchedStatements=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 db.username=root db.password=xxxx
第②步:这样在mybatis-config.xml中的
<configuration> <properties resource="db.properties"> </properties> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/><!--开启驼峰命名映射--> </settings> <typeAliases> <package name="ssm.example.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${db.driver}"/> <property name="url" value="${db.url}"/> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/> </dataSource> </environment> </environments> <!--加载映射文件路径--> <mappers> <!-- <mapper resource="EmployeeMapper.xml"/>--><!--加载单个文件--> <package name="ssm.example.mapper"/><!--加载此包下的所有文件--> </mappers> </configuration>
这样在第②步中可能得到的链接就不一样了
解决方法:
方法1:在第①步时将链接改为如下:mybaits-config.xml中的url 不用改
db.url=jdbc:mysql://xxxx:3306/test_mybatis?rewriteBatchedStatements=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
方法2:不使用db.propertis中的url,在第②步中mybaits-config.xml中将url改为
<property name="url" value="jdbc:mysql://XXXX:3306/test_mybatis?rewriteBatchedStatements=true&characterEncoding=utf-8&serverTimezone=GMT%2B8"/>