Mybatis学习记录-增删改查问题总结之一

1.运行环境:

  • JDK1.8
  • MyBatis 3.3.0
  • MySql 5.7.21(运行于Centos 7)
  • Eclipse 4.4.0
  • Apache Maven构建工具

2.问题点

  链接字符串: jdbc:mysql://192.168.100.139:3306/mybatis?useSSL=false&characterEncoding=UTF-8

  • MyBatis配置JDBC链接MySql字符串需要添加SSL设定(useSSL=false),不然有如下警告
    WARN: Establishing SSL connection without server's identity verification is not recommended. 
    According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn
    't set.
    For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'.
    You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
  • 再配置Update 语句完成后,使用测试方法测试时,出现中文乱码问题
     1     public void testUpdate(){
     2         //获取Session
     3         SqlSession sqlSession = getSqlSession();
     4         try {
     5             //获取UserMapper接口
     6             UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
     7             //从数据库查询id = 1的用户
     8             SysUser user = userMapper.selectById(1l);
     9             TestCase.assertEquals("admin", user.getUserName());
    10             user.setUserName("admin_test");
    11             user.setUserEmail("test@mybatis.tk");
    12             int result = userMapper.update(user);
    13             TestCase.assertEquals(1, result);
    14             //再次从数据库查询id = 1的用户
    15             user = userMapper.selectById(1l);
    16             //userName = admin_test
    17             TestCase.assertEquals("admin_test", user.getUserName());
    19         } finally {
    20             sqlSession.commit();
    21             // 不要忘记关闭sqlSession
    22             sqlSession.close();
    23         }
    24     }
  • 特别说明,首次selectById并不会出现中文乱码,是在update资料过后再次执行selectById才会出现中文乱码。 解决方案:jdbc链接字符串加上&characterEncoding=UTF-8,因为是在XML中设定的,其中&amp为&的转义字符。

posted @ 2018-03-13 22:26  玉雨鱼  阅读(357)  评论(0编辑  收藏  举报