1. 程式人生 > >Incorrect string value ‘xx’ for column ‘xx’at row 1 Query問題

Incorrect string value ‘xx’ for column ‘xx’at row 1 Query問題

這個讓我頭疼了很長時間,網上說是字符集問題。

其實最主要的問題為傳入的引數與資料庫型別對應不上。

我遇到的是我的實體中定義了屬性為物件,而mybatis中傳入的維物件型別。導致錯誤的發生。#{userid.id},

<?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.thinkgem.jeesite.modules.kaoqin.dailysale.dao.TscDailysaleDao">
    
	<sql id="tscDailysaleColumns">
		a.id AS "id",
		a.userid AS "userid",
		a.recorddate AS "recorddate",
		a.product AS "product",
		a.sale AS "sale",
		u.name AS "userName"
	</sql>
	
	<sql id="tscDailysaleJoins">
		LEFT JOIN SYS_USER u ON u.id = a.userid
	</sql>
    
	<select id="get" resultType="TscDailysale">
		SELECT 
			<include refid="tscDailysaleColumns"/>
		FROM tsc_dailysale a
		<include refid="tscDailysaleJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="TscDailysale">
		SELECT 
			<include refid="tscDailysaleColumns"/>
		FROM tsc_dailysale a
		<include refid="tscDailysaleJoins"/>
		<where>
			
			<if test="userid != null and userid != ''">
				AND a.userId = #{userid}
			</if>
			<if test="recorddate != null and recorddate != ''">
				AND a.recorddate = #{recorddate}
			</if>
			<if test="product != null and product != ''">
				AND a.product = #{product}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="TscDailysale">
		SELECT 
			<include refid="tscDailysaleColumns"/>
		FROM tsc_dailysale a
		<include refid="tscDailysaleJoins"/>
		<where>
			
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO tsc_dailysale(
			id,
			userId,
			recordDate,
			product,
			sale
		) VALUES (
			#{id},
			#{userid.id},
			#{recorddate},
			#{product},
			#{sale}
		)
	</insert>
	
	<update id="update">
		UPDATE tsc_dailysale SET 	
			userId = #{userid.id},
			recorddate = #{recorddate},
			product = #{product},
			sale = #{sale}
		WHERE id = #{id}
	</update>
	
	<update id="delete">
		DELETE FROM tsc_dailysale
		WHERE id = #{id}
	</update>
	
</mapper>

下面是抄的為別人歸納的字符集解決的方案

1.在mysql的安裝目錄下找到my.ini,作如下修改:

[mysqld]

character-set-server=utf8mb4

[mysql]

default-character-set=utf8mb4

修改後重啟Mysql

2. 將已經建好的表也轉換成utf8mb4

命令:

更改資料庫編碼:ALTER DATABASE caitu99 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

alter table TABLE_NAME convert to character set utf8mb4 collate utf8mb4_bin; (將TABLE_NAME替換成你的表名)

然後就OK了。網上流傳的一個版本增加了一個步驟,就是把mysql環境變數將character_set_client,character_set_connection,character_set_database,character_set_results,character_set_server 都修改成utf8mb4,不過我沒有做這一步,也正常,所以可能是這一步是多餘的,如果需要改,則按照下面介紹做修改。

3修改mysql配置檔案my.cnf(windows為my.ini) 

my.cnf一般在etc/mysql/my.cnf位置。找到後請在以下三部分裡新增如下內容:  [client]  default-character-set = utf8mb4  [mysql]  default-character-set = utf8mb4  [mysqld]  character-set-client-handshake = FALSE  character-set-server = utf8mb4  collation-server = utf8mb4_unicode_ci  init_connect='SET NAMES utf8mb4'