首页 > 其他 > 详细

Mybatis插入数据乱码

时间:2021-09-02 08:14:43      阅读:14      评论:0      收藏:0      [点我收藏+]

场景:利用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&amp;characterEncoding=utf-8&amp;serverTimezone=GMT%2B8"/>

 

Mybatis插入数据乱码

原文:https://www.cnblogs.com/zlshtml/p/15213709.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!