JDBC connection pool principle

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[principle of JDBC connection pool]

Hello, I'm a java student of Xi'an Branch of it Academy. I'm an honest, pure and kind java programmer. Today, I'd like to share with you the knowledge points in deep thinking about Java task 1 on the official website of the Academy, the principle of JDBC connection pool

1、 A Background introduction

What is mybatis? When it comes to accessing a database with JDBC, you need to write your own SQL and also operate hand-assisted codes such as connection, station and resultset; Access to different tables, but also write a lot of the same code, cumbersome and boring. With mybatis, you only need to provide key SQL statements. Other tasks, such as loading drivers, establishing connections, statements, and JDBC related actions, are handed over to mybatis to reduce repetitive work, pay more attention to the operation level of addition, deletion, modification, and query, and seal the technical details at the bottom

In the actual development of mybatis, the database query is difficult to achieve overnight. We often have to splice different SQL statements according to different scenarios, which is undoubtedly a complex work. When we use mybatis, mybatis provides us with dynamic SQL, which allows us to splice different SQL statements according to specific business logic. OK, let's take a look at how to use dynamic SQL in mybatis today.

2、 Knowledge analysis

2. Knowledge analysis

The main elements of dynamic SQL in mybatis

1. If is the judgment element in mybatis dynamic SQL, which is somewhat similar to the if statement in Java. The difference is that if here is often used with test.

When the address passed in by the user is not null or empty string, I will add a where condition, otherwise I will not add any conditions.

2. Choose is a bit similar to switch in Java. It is often used together with when and otherwise. In the query criteria, if the user sends an ID, I will query the data of the ID. if the user sends an address, I will add the query criteria of address. If the user sends a username, I will add the query criteria of username. Finally, if the user does not add any query criteria, The default query condition is to query all data with ID less than 10.

3. In the above case, small and medium-sized partners may have found a problem, that is, when we add query criteria, we first add where 1 = 1 before the query criteria, and then add and or something directly after it. So it's obviously a little troublesome to write each time. Is there a simpler scheme? Of course, we can use the where element. Only when the conditions in the where element are true, can we assemble the where keyword into SQL, which is much simpler than the previous method

4. Trim means element replacement. In the above case, we can replace and with where,

Set is the element we use when updating the table. Through the set element, we can modify a piece of data field by field. If a comma is encountered in the set element, the system will automatically remove it

5. The foreach element is used to traverse the set. For example, if I want to query people in multiple cities, my SQL statement may be like select * from user2 where address in ('xi'an ',' Beijing '). When I query, I may just pass in a list set with Xi'an and Beijing query conditions. How can I assemble this set into an SQL statement?

6. Collection represents the name of the collection in the passed in parameters, index represents the subscript of the current element in the collection, open and close represent how to wrap the data in the collection, separator represents the separator, and item represents the current element during the loop. The SQL finally combined from such a configuration is select * from user2 where address in ('xi'an ',' Beijing ').

7. Bind using the bind element, we can define some variables in advance and then use them in query statements

3、 Frequently asked questions

1. When using the dynamic SQL function of mybatis, the single parameter method may not execute or throw an exception that the property does not have a get or set method

2. The choose tag has no else tag

3. An exception occurs when id = null is used in the set tag if statement. Update table set id = #{ID, jdbctype = integer}, name = #{name, jdbctype = varchar} where id = #{id, jdbctype = integer}

4、 Solutions Solutions

1 mark the parameter in mapper interface by @ param

2 you can use the when otherwise tag to achieve something similar to if else... Foreach tag select * from tables where id in (3,6,...); Either 3, or 6

3-node label: the main function of trim is to add some prefixes (prefixes) before the contents contained in trim, or add some suffixes (suffixes) after the contents contained in trim, and ignore some contents in the header of the contents contained in trim (prefixoverrides), You can also ignore some content at the end of the content contained in trim (suffixoverrides) utm_source = copy method 2: put the content in the update condition label in the label

5、 Coding practice

```

public class Man implements Human {

private QQCar car; public Man() {

this. car = new QQCar(); }

@Override public void xiabibi() { }

public void driveCar(){ car.drive(); } }

```

The interface car has two implementations: Mercedes Benz and QQ. In the above highly coupled codes of man class and qqcar class, the old driver only creates QQ car objects through the constructor, so he can only drive QQ cars. What should the old driver do if he wants to drive Mercedes Benz? Do you want him to re create the object of Mercedes Benz? Such highly coupled code seems to be helpless, so let's improve the above code by injecting objects:

```

public class Man implements Human {

private Car car; public Man(Car car) { this.car = car; }

@Override public void xiabibi() { } public void driveCar() { car.drive(); }

}

```

According to the polymorphic characteristics, the above code shields the specific object implementation by means of constructor interface injection, so that the old driver can drive whatever car he wants. This is the benefit of dependency injection.

6、 Extended thinking

1. What other ways to implement dynamic SQL?

2. What are the differences between different versions of mybaits when using dynamic SQL?

7、 References

http://blog.csdn.net/jpzhu16/article/details/52810747

https://www.cnblogs.com/dongying/p/4092662.html

https://www.cnblogs.com/zkongbai/p/5336015.html

http://how2j.cn/k/mybatis/mybatis-tutorial/1087.html?p=13495

8、 More discussion

1. What other ways to implement dynamic SQL?

You can also use annotations to implement dynamic SQL, script SQL, build SQL in methods, and structured SQL

2 in dynamic SQL, the value transfer of list is wrong. Sometimes batch data needs to be processed. It is inevitable to use list as a parameter

```

@SelectProvider(type = UserDaoProvider.class,method = "find")

public List find(List list);

class UserDaoProvider {

public String find(List list) {

```

This is the simplest list parameter transfer, but a parameter transfer error will be reported at run time. This is caused by the internal mechanism of mybatis. Its parameters need to be a key / value structure. When you encounter a list that is not a key / value structure here, mybatis will convert it into a key / value structure. Key is his name "list" and value is his value list. To correctly transfer parameters, you need to use the map of the key / value structure, as shown below

```

@SelectProvider(type = UserDaoProvider.class,method = "find")

public List find(List list);

class UserDaoProvider {

public String find(Map map) {

List list = (List) map. get("list");

```

How does 3 mybatis parse dynamic SQL statements?

I recommend this article, which is very detailed and analyzed by analyzing the source code

http://www.importnew.com/24160.html

IX. acknowledgement:

Thanks to elder martial brothers XX and XX. This tutorial is based on their previous technology sharing.

X. conclusion:

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Ppt link video link

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
分享
二维码
< <上一篇
下一篇>>