SpringBoot+MyBatis+中文乱码+页面显示

第一步创建一个springboot

https://mp.csdn.net/postedit/83586155

使用实体类的属性,和使用yml配置文件

https://mp.csdn.net/postedit/83586885

整合mybatis

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/goods?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull&
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=1234

mybatis.mapper-locations=classpath:mapper/**.xml
mybatis.type-aliases-package=com.bdqn.entity

mysql的jar包要用5系类的

resources-》mapper-》GoodsMapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bdqn.dao.GoodsMapper">
    <select id="findGoods" resultType="Goods">
        select * from goods
    </select>
</mapper>

其他的在main-》java-》创建com.bdqn....等包

然后在主方法加上注释

package com.bdqn;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@MapperScan("com.bdqn.dao")
@EnableTransactionManagement
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

然后在controller中

 @Autowired
    private GoodsService goodsService;


    @RequestMapping("/show")
    @ResponseBody
    public String show(){
        return id+name+price;
    }

然后跑吧

中文乱码:页面浏览器显示中文乱码

在配置文件中

spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8

controller页面显示

加一个jar包

 <!--页面视图依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

然后就可以在resources下的templates下直接写页面,就能访问

https://blog.csdn.net/minolk/article/details/81750304

猜你喜欢

转载自blog.csdn.net/jinqianwang/article/details/83990127
今日推荐