booost怎么处理emoji

主要是因为MySQL 默认使用的是UTF8 编码,UTF8 编码只支持 1-3 个字节。

而 emoji 占有 4 个字节的存储空间,所以自然保存不了。但是从 MYSQL5.5 开始,可支持 4 个字节 UTF 编码,只要将编码标记成 utf8mb4 即可。并且utf8mb4 是兼容 UTF8 的。

1.设置数据库,表,字段编码为utf8mb4

_ 根据自己的需求选择设置字符编码,可以手动设置(直接选中某个字段设置编码方式。数据库只能创建的时候设置,但是不推荐手动创建方式),也可以通过sql语句设置,但是用这两种方式都有可能造成锁表!前提是数据量特别大,所以请慎重!

设置某个字段字符集编码

alter table `tableName` change 字段名 字段名 varchar(20) character set 字符集编码

设置数据库表的字符集编码

# 默认的字符排序规则

ALTER TABLE offline_hana_ztbasic CONVERT TO CHARACTER SET utf8mb4

# 可以设置字符排序规则

ALTER TABLE offline_hana_ztbasic9 CONVERT TO CHARACTER SET utf8mb4

COLLATE utf8mb4_unicode_ci

修改某个数据库的字符集编码

alter database `databaseName` default character set 字符集编码

修改数据库默认的字符集编码

_ 打开 MySQL 配置文件(Windows 下是 my.ini,Linux 下是 my.cnf)修改配置,将编码改成 utf8mb4

修改之后必须重启mysql服务。

2.设置客户端连接数据库编码

_ 可能有些人跟我一样,设置完数据库里面的字符编码集后,程序还是报错,问题还是没解决。可能每个人的编码软件不一样或者设置不一样。反正经过上面的设置,已经可以在数据库手动插入emoji表情,并且可以保存。

那么问题来了,怎么解决呢?

根据数据源的不同,设置的也不同。

# 以下是最常用的数据源

# druid的方式

spring.datasource.druid.connection-init-sqls=set names utf8mb4

# hikari

spring.datasource.hikari.connection-init-sql=set names utf8mb4

设置完成之后,就大功告成啦!

画外音:目前Druid在开源中国举办的2019年度最受欢迎中国开源软件中排名第7名,支持Druid的朋友可以去投票哇。 2019年度最受欢迎中国开源软件

maxWait :从连接池中获取连接的最大等待时间,单位ms,默认-1,即会一直等待下去

笔者在使用Druid时都会设置这个参数,这样如果是获取连接超时,更容易从日志中获取调用失败的原因。

如果超时,Druid会抛出以下异常

在DruidDataSource中的getConnectionInternal方法使用到了maxWait

maxWait默认是不超时,即如果连接池没有空闲连接,则会一直等待下去,但是一般的接口都是有超时时间的,如果接口超时,不方便定位出来是获取不到连接导致的,最好设置maxWait,并且小于接口的超时时间。

直接上代码 首先是从程序的入口开始说:

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<!--

设置session过期的时间

-->

<session-config>

<session-timeout>20</session-timeout>

</session-config>

<!--

读取spring的配置文件

-->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:config/spring.xmlclasspath:config/spring-myBatis.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<listener>

<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>

</listener>

<!--

设置字符编码,将所有的字符编码同意设置为utf-8

-->

<filter>

<filter-name>filterEncoding</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>utf-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>filterEncoding</filter-name>

<url-pattern>/</url-pattern>

</filter-mapping>

<!--

生成一次性验证码的servlet

-->

<servlet>

<servlet-name>verifyCode</servlet-name>

<servlet-class>com.longhang.tool.verifyCode.VerifyCodeServlet</servlet-class>

</servlet>

<!--

将所有*.do的请求交给springMVC的DispatcherServlet来处理

-->

<servlet>

<servlet-name>DispatcherServlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:config/springMVC-config.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>verifyCode</servlet-name>

<url-pattern>/verifyCode</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>DispatcherServlet</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

</web-app>

springMVC的配置文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!--

配置自动扫描的包,让其扫描com.longhang,controller下面的所有包

-->

<context:component-scan base-package = "com.longhang.controller"></context:component-scan>

<!--

配置视图解析器

将视图逻辑名解析为/*.jsp

-->

<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name = "prefix" value = "/"></property>

<property name = "suffix" value = ".jsp"></property>

</bean>

</beans>

spring.xml的配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

">

<context:property-placeholder location="classpath:config/druid.properties" />

<!-- 自动扫描(自动注入) -->

<context:component-scan base-package = "com.longhang.service"></context:component-scan>

</beans>

spring-myBatis.xml配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

">

<!-- 配置数据源 --><!--

<bean name = "datasource" class = "com.alibaba.druid.pool.DruidDataSource" init-method = "init" destroy-method = "close">

<property name ="url" value = "${jdbc_url}"></property>

<property name="username" value="${jdbc_userName}" />

<property name="password" value="${jdbc_password}" />

</bean>

--><bean name = "datasource" class = "com.alibaba.druid.pool.DruidDataSource" init-method = "init" destroy-method = "close">

<property name ="url" value = "jdbc:mysql://localhost:8000/bookShopping"></property>

<property name="username" value="root" />

<property name="password" value="13072399672" />

</bean>

<!--配置sqlSessionFactory 并读取mybatis的一些配置-->

<bean name = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean">

<property name = "dataSource" ref = "datasource"></property>

<property name="mapperLocations" value="classpath:mapper/*.xml"/>

</bean>

<!--

自动扫描 将Mapper接口生成代理注入到Spring

-->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="com.longhang.dao" />

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

</bean>

<!--

配置事物

-->

<bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name = "dataSource" ref = "datasource"></property>

</bean>

<!--

<tx:annotation-driven transaction-manager = "transactionManager"/>

-->

<!--

事物的具体内容

-->

<tx:advice id="transactionAdvice" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="add*" propagation="REQUIRED" />

<tx:method name="append*" propagation="REQUIRED" />

<tx:method name="insert*" propagation="REQUIRED" />

<tx:method name="save*" propagation="REQUIRED" />

<tx:method name="update*" propagation="REQUIRED" />

<tx:method name="modify*" propagation="REQUIRED" />

<tx:method name="edit*" propagation="REQUIRED" />

<tx:method name="delete*" propagation="REQUIRED" />

<tx:method name="remove*" propagation="REQUIRED" />

<tx:method name="repair" propagation="REQUIRED" />

<tx:method name="delAndRepair" propagation="REQUIRED" />

<tx:method name="get*" propagation="SUPPORTS" />

<tx:method name="find*" propagation="SUPPORTS" />

<tx:method name="load*" propagation="SUPPORTS" />

<tx:method name="search*" propagation="SUPPORTS" />

<tx:method name="datagrid*" propagation="SUPPORTS" />

<tx:method name="*" propagation="SUPPORTS" />

</tx:attributes>

</tx:advice>

<!--

定义一个切面,在定义的切面上加入事物

-->

<aop:config>

<aop:pointcut id="transactionPointcut" expression="execution(* com.longhang.service..*Impl.*(..))" />

<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />

</aop:config>

</beans>

关于mybatis的映射文件

<?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.longhang.dao.userDao.UserDao" >

<resultMap id = "baseResultMap" type = "com.longhang.entity.user.User">

<id column = "uid" property = "uid" jdbcType = "CHAR"/>

<result column = "loginname" property = "loginname" jdbcType = "VARCHAR"/>

<result column = "loginpass" property = "loginpass" jdbcType = "VARCHAR"/>

<result column = "email" property = "email" jdbcType = "VARCHAR"/>

<result column = "status" property = "status" jdbcType = "VARCHAR"/>

<result column = "activationCode" property = "activationCode" jdbcType = "CHAR"/>

</resultMap>

<sql id = "base_column_list">

uid,loginname,loginpass,email,status,activationCode

</sql>

<!--根据id查询

返回的类型为User

-->

<select id="findById" resultMap="baseResultMap" parameterType="java.lang.String" >

select

<include refid="base_column_list" />

from l_user

where uid = #{uid,jdbcType=CHAR}

</select>

<!--

根据activationCode查询

返回值是User

-->

<select id = "findByActivationCode" resultMap = "baseResultMap" parameterType = "java.lang.String">

select <include refid = "base_column_list"/>

from l_user

where activationCode = #{activationCode}

</select>

<!--


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12082608.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存