Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What if there is an error in the Mybatis-Spring connection mysql8.0 configuration steps?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "Mybatis-Spring connection mysql8.0 configuration steps how to do wrong", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "Mybatis-Spring connection mysql8.0 configuration steps how to do wrong" this article.

Environment and configuration fil

JDBC jar version: 8.0.11 Mybatis jar version: 3.4.6 Spring jar version: 4.3.18 Mybatis-Spring jar version: 1.3.1 configuration Information File: db.properties Spring configuration File: applicationContext.xml

Configure db.properties

Fill in the following contents in the configuration db.properties

# the driver name is written like jdbc.driver=com.mysql.cj.jdbc.Driver# url, and the jdbc.url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true# database user name, jdbc.username=root# data password, jdbc.password=password.

There is an error point here, that is, the key of the user name uses username, so the data source is configured with ${username} in applicationContext.xml, which leads to a problem because the ${username} in the expression of XML represents the username in the path of the computer environment! That is to say, the user name of your computer is used, not the user name of the database! All in all, don't use username as the key name. I use jdbc.username.

Configure applicationContext.xml

1. In applicationContext.xml, introduce the db.properties file.

two。 Configure the data source

3. Configure Mybatis to scan mapper.XML files

4. Scan all dao layer interfaces

Configure dao layer interfaces and mapper files

Dao interface

Public interface UserDao {public List selAll ();}

MapperXML file

Select * from user

test

@ Testpublic void selAll () {ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext.xml"); UserDao userDao = context.getBean (UserDao.class); List list = userDao.selAll (); System.out.println (list);}

The above is all the contents of the article "what to do if something goes wrong in the configuration steps of Mybatis-Spring connection mysql8.0". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report