Spring boot integrates mybatis and connects MySQL with c3p0 data source

I've just come into contact with spring boot and I'm not familiar with many things, such as its annotation method, its configuration method, etc; I heard it was awesome, so I tried to learn. After being basically familiar with the first program of springboot. I thought that spring used the data source to connect to the database when integrating mybatis, so I also wanted to try to use c3p0 to connect to the database. Therefore, there are the following contents:

First, create the Maven project import package:

pom. xml

Application service class

Profile content (configuration database)

If you are prepared correctly, try to enable the application service. You can continue to configure the data source without reporting an error. Try many methods. It is impossible to inject by yourself. Therefore, I think of spring configuration and try to configure it in that way. Therefore, datasourceconfig java

There may be some questions about why only one sqlsessionfactory is needed here. Specifically, you should be familiar with spring integration mybatis. And look at the bottom layer, so you can assemble it automatically.

The data source is ready. Try to connect to the database

Edit interface

ExamPleMapper. java

Edit mapper XML file

Edit the entity class because you want to reflect the results into the bean class, users java

Editing implementation

Example2. java

Enter address: http://localhost:8080/example2/gu

Face display results: [{userid: 1, "username": "as"}, {userid: 2, "username": "we"}, {userid: 3, "username": "ZX"}]

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>