Spring整合Mybatis出现Access denied for user 'Think'@'localhost' (using password: YES)

Spring整合Mybatis出现Access denied for user 'Think'@'localhost' (using password: YES)

Appears Access denied for user 'Think' @ 'localhost' (using password: YES) in the integration of mybatis

Looking for a quite a while, a lot of the Internet to change the password. Found no use, I probably was not clear description of the problem.

After reading a little crisp 512 know the problem

db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username=root
password=123456

spring-dao.xml

<context:property-placeholder location="db.properties" system-properties-mode="NEVER"/>
    <!--配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>

problem causes:

The system also has a username attribute, then the system variable overrides the value in the Properties, then get the username Administrator username value for the system, the password in the password for the properties to query the database, and the user name and password are not names match will error. Upon completion of the injection is to obtain the value of the completion Spring injected with "$ {..}" mode. And this can also be obtained by expression directly into the JVM system properties ..........

solution:

Option One:

The properties file into user's username or the other to get the connection string can successfully access the database. Recommendations: sensitive words when username, just to be safe or try not to use the username.

Option II:

Modified in the Spring configuration file: Add a system-properties-mode properties

This property has four attributes:

  • ENVIRONMENT ": placeholder should be resolved for the current environment and any local property; (starting from Spring 3.1

    Property value defaults to "ENVIRONMENT")

  • "NEVER": placeholder property only for local resolution, without parsing system for properties;

  • "FALLBACK": placeholder should be resolved for any local properties, and analyzing for system properties;

  • "OVERRIDE": placeholder should first be resolved for the system properties, and then parsed for any local property;      

original:

Controls how to resolve placeholders against system properties. As of Spring 3.1, this
    attribute value defaults to "ENVIRONMENT", indicating that resolution of placeholders
    against system properties is handled via PropertySourcesPlaceholderConfigurer and its
    delegation to the current Spring Environment object.
    
For maximum backward compatibility, this attribute is preserved going forward with the
3.1 version of the context schema, and any values other than the default "ENVIRONMENT"
will cause a traditional PropertyPlaceholderConfigurer to be registered instead of the
newer PropertySourcesPlaceholderConfigurer variant. In this case, the Spring Environment
and its property sources are not interrogated when resolving placeholders. Users are
encouraged to consider this attribute deprecated, and to take advantage of the
Environment and PropertySource mechanisms. See ConfigurableEnvironment javadoc for examples.

"ENVIRONMENT" indicates placeholders should be resolved against the current Environment and against any local properties;
"NEVER" indicates placeholders should be resolved only against local properties and never against system properties;
"FALLBACK" indicates placeholders should be resolved against any local properties and then against system properties;
"OVERRIDE" indicates placeholders should be resolved first against system properties and then against any local properties;

Machine translation:

How to control the analytical system according to the placeholder properties. From the beginning of Spring 3.1

The default value for the attribute "ENVIRONMENT", the parsing placeholder

Processing by the system property is its PropertySourcesPlaceholderConfigurer

Spring environment entrusted to the current object.

For maximum backward compatibility, this property is preserved when performing a forward

Version 3.1 context mode, as well as any value other than the default "environment" outside

Registration will result rather than a traditional PropertyPlaceholderConfigurer

New PropertySourcesPlaceholderConfigurer variants. In the present embodiment, the environment is Spring

When parsing placeholder source not ask its properties. user

It recommended that consideration be deprecated this attribute and use

Environmental properties of the source and mechanism. For an example, see ConfigurableEnvironment javadoc.

"ENVIRONMENT": placeholder should be resolved for the current environment and any local property;

"NEVER": placeholder property only for local resolution, without parsing system for properties;

"Back": placeholder should be parsed for any local property, and then parse for the system properties;

"OVERRIDE": placeholder should first be resolved for the system properties, and then parsed for any local property;

Guess you like

Origin www.cnblogs.com/whitespaces/p/12442268.html