如果这种情况用了各种办法去进行设置都不管用的话,可以尝试在启动器代码中添加控制台输出编码格式,代码如下:
// 设置控制台编码
System.setOut(new java.io.PrintStream(System.out, true, "UTF-8"));
System.setErr(new java.io.PrintStream(System.err, true, "UTF-8"));
添加示例
未修改代码:
import com.domain.Comment;
import com.mapper.CommentMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test02ApplicationTests {
@Autowired
private CommentMapper commentMapper;
@Test
public void selectComment() {
Comment comment = commentMapper.findById(1);
System.out.println(comment);
}
}
修改如下:
import com.domain.Comment;
import com.mapper.CommentMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.UnsupportedEncodingException;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test02ApplicationTests {
@Autowired
private CommentMapper commentMapper;
@Test
public void selectComment() {
try {
// 设置控制台编码
System.setOut(new java.io.PrintStream(System.out, true, "UTF-8"));
System.setErr(new java.io.PrintStream(System.err, true, "UTF-8"));
Comment comment = commentMapper.findById(1);
System.out.println(comment);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
最后,问题解决: