Learning Spring Supplements

The book "JavaEE Framework Integrated Development Introduction to Practice" is still too shallow, and some basic knowledge needs to be supplemented.

First, modify the shortcut keys so that you can use alt+/ to quickly complete the code like eclipse.

Reference blog:

(33 messages) IDEA sets code hints or shortcut keys for auto-completion_idea completion tips_thirty.st's Blog-CSDN Blog

Learning a shortcut key: quickly organize the code ctrl+alt+L  

First example to learn Spring:

(33 messages) [Spring] Spring entry case_spring example_Teacher.Hu's Blog-CSDN Blog

order:

Step 1: Create a Maven project. (The previous JavaEE learning is introduced)

Step 2: Add the Spring dependency package under the pom.xml file. If it is an ordinary web class, it is automatically introduced by importing Maven as introduced earlier. (This is the key point, take it out and study it separately)

Step 3: Write your first Java code.

Step 4: Create the applicationContext.xml file. Add a bena in the file, write the id yourself, and class is the path of the Java code just created (package name + class name).

Step 5: Write the test class. carry out testing.

Summary: Through this example, I probably learned that Spring handed over the work of the test class to the Spring container.

A normal working call is, see picture:

 

 The invocation of the Spring container will hand over the work to the id. Call method by ID.

Code problem analysis: 

First problem: Caused by: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 73; cvc-elt.1: Cannot find declaration for element 'beans'.

Supplementary study on the structure of the applicationContext.xml file configuration:

The file configuration of applicationContext.xml:

This is by default, click on the configuration of Spring Config.

Import Click on this directory, and then quote, and the Spring Config option will appear:

 The default situation that appears is this: the <bean> tag is added later.

 Reference blog:

Spring configuration file beans.xml head configuration explanation - EasonJim - Blog Park (cnblogs.com)

Here are some other configuration import functions, such as tx function (transaction management function), context function (about spring context, including loading resource files)

Supplementary study on pom.xml dependent packages:

This is the second step in creating a Spring program. This step should be placed in front of the configuration of the previous ApplicationContext.xml file.

From the previous study, we know that the configuration XML file is valid only after the dependency package is imported.

But there are a few issues that confuse me:

Compared with using myeclipse, we directly import jar files, but here we use dependent packages, so how do we determine which package we need? In addition to copying and pasting dependent packages, how to manually construct dependent packages? What is the construction of dependent packages?

Application example: the first Spring operation to connect to the database.

(33 messages) [Spring] Spring JDBC configuration and use_Teacher.Hu's Blog-CSDN Blog

The order of steps is the same as the order of creating Spring. The difference is that the second step imports the dependent packages, which dependent packages are required, and the XML file created in the fourth step, the code in the container is used to connect to the database.

First of all, you need to learn the class method of Spring JdbcTemplat: (the class of database operation is different from eclipse, because it uses the interface method) It belongs to the class in the core package. Contains four methods of adding, deleting, modifying and checking. See below for specific usage:

Mainly introduce the methods of update and query.

public int update(String sql,Object args[ ] ):

例如:public int update(String sql,Object param[ ] ):

public List<T> query(String sql,RowMapper<T> rowMapper,Object args[ ]):

例如:public List<register> query(String sql,Object param[ ] ):

The improvement of the second step Import dependent packages:

Spring programming mainly uses the core and dataSource packages of the Spring JDBC module. There are commonly used JdbcTemplat classes in the core package.

Improvement of the third step Write code: (The essence of the method of the Dao class is to redefine the class method of JdbcTemplat)

Write the code of the dao layer, because the JdbcTemplat class already provides methods for adding, deleting, modifying and checking. The writing of the code is to rewrite the jdbc method, and then use the class method whose return object is JdbcTemplat to complete. (Because the class method of JdbcTemplat is too verbose, rewriting it is good for construction.)

The improvement of the fourth step configures the XML file:

Write operations for database connections. memory is required.

Step 5: Test the class, you can use the method of the dao class to introduce specific data.

problem analysis:

The first problem: the package where the scanned dao is located is not specified, so the id cannot be found.

 

The second problem: there is a general problem of time zone setting. serverTimezone=UTC (set to China time zone)

Remember to add after the url statement: &serverTimezone=UTC

The third question: the final output is the address of the register table. As mentioned earlier, this solution is to rewrite the toString method in the po class.

Now back to the question at the beginning: how to choose the dependency on pom.xml, and what is the relationship with applicationContext.xml?

Supplementary learning of pom.xml dependent packages and applicationContext.xml configuration files:

The pom.xml depends on the package, which is added according to the requirements.

For example: in the testDaoImpl class: the imported classes belong to the dependent packages in the package. For example, as shown in the figure below:

There are two packages that need to be imported here: the context package and the jdbc package. How to find the required package, refer to the blog:

Go to the Maven repository to search for these two packages: Maven Repository: Search/Browse/Explore (mvnrepository.com)

(34 messages) Quickly configure maven dependencies - pom.xml configuration_eknown's blog-CSDN blog

Test class test: the class of the package is also used, and you can see that only the package that needs to be imported is context.

 

Now analyze why it is not a bean package, but a context package,

 

Finally, because our project needs to import the database, the jdbc package is finally added to complete the pom.xml configuration.

Add the functions of these packages: (You can add them to the Maven warehouse as needed without copying here)

(34 messages) Introduction to various dependency packages in Java_jetty dependency_jjw_zyfx's Blog-CSDN Blog

Then let's look at the configuration of applicationContext.xml:

 

 

At this point, how to create a Spring program and a Spring program connected to the database is over. It also introduces the import of IDEA's pom.xml dependency package and the configuration of the applicationContext.xml file.

Summary: Spring Development Project

1. Create a Maven project

2. Import the pom.xml dependency package that may be required, and obtain it from the Maven repository.

3. Write code.

4. Create the application.xml file. Configure the header file as required.

5. Test.

Exercise: Write a query that inserts into a Spring development project.

Guess you like

Origin blog.csdn.net/qq_55928086/article/details/131416577