일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- Spring Batch
- reactive
- Spring Framework
- 서버운영
- ipTIME
- 웹 커리큘럼
- 웹앱
- 웹 스터디
- reactor
- spring reactive
- 공유기 서버
- reactor core
- Today
- Total
Hello World
[펌]Getting started with Spring JDBC in a web application 본문
[펌]Getting started with Spring JDBC in a web application
EnterKey 2016. 1. 10. 11:58I have shown you how to setup a basic Spring 3 MVC web application in my previous article. Reusing that project setup as template, I will show you how to enhance it to work with JDBC. With this you can store and retrieve data from database. We will add a new controller and a data service through Spring so you can see how Spring injection and annotation configuration works together.
A direct JDBC based application is easy to setup in comparison to a full ORM such as Hibernate. You don’t need to worry AOP, TranactionManager, Entity mapping and full array of other configurations. On top of the JDK’s
API, the Spring comes with module that can boots your productively with their well known class. Let’s explore how this can be setup and run as web application.Getting started and project setup
For demo purpose, I will use the in-memory version of H2Database as JDBC store. It’s simple to use and setup. And if you decided to use their FILE or TCP based database, you would simply have to re-set the datasource, and you may continue to explore more.
We will start by adding new dependencies to your existing
file.With this, you will have access to Spring module classes for configuration. Find the previous
file in your existing project and add what’s new compare to below:What’s new in here is we introduced a new
class that will be loaded inside method. The is just another Spring annotation based configuration that creates a new Spring context for bean definitions. We’ve created a bean there that will run the in-memory database. The bean returned by the builder also conveniently implemented the interface, so we can actually inject this into any data service and start using it immediately.One more cool thing about the Spring embedded database builder is that it also runs any SQL script as part of the startup! For this demo, we will create a
table in the file. This file is visible to Spring as in root of the CLASSPATH due to Maven standard source structure.That’s the datasource setup. Now notice that I did not add this datasource Spring bean definition into existing
class. The reason is that we want a separate Spring context to configure all service level beans, while reserving the for all Spring MVC related beans (such as Controller, URL mapping etc). This helps organize your bean definitions in hierarchical order of Spring contexts; with as parent and as child layers. This also means that all service beans in are automatically visible to ; for the purpose of injection etc.Also notice that with separated config classes, we are able to specify two distinct packages to scan for service components; we use
for and for . This is important and it can save you some troubles letting Spring auto detecting your all these annotation based components.Creating Data Service
Now it’s time we use the JDBC, so let’s write a data service class under
file.The service is very straight forward. I expose two methods: one for insert and one for retrieve all Ping data. Noticed that I used
to indicate to Spring that this class is a component service that perform data service. Also note how we inject the using a setter method, and then instantiate the as member field. From that we can take full advantage of the Spring JDBC API for query and update.A note on logging. Spring core itself uses Apache
, so I reused that API without even explicitly declare them in my . If you want to see more details from log output, you should add logger implementation to the project, and it should automatically work. I will leave that as your exercise.Next we will need to write a Controller that can bring data to web UI page. We will create this under
file.In this controller, you can easily see that the Ping data is fetched and updated through our data service by injection. I’ve declared and map URL
to insert Ping data into the database. Spring has this very nice short hand syntax annotation mapping that can extract parameter from your URL. I allow user to set a simple tag word to be insert as Ping record so we can identify the source in database.The other controller handler
URL is very straight forward; it simply returns all the records from PING table.For demo purpose, I choose to not use JSP as view, but to return plain text directly from the Controller. Spring let you do this by adding
to the handler method. Notice also we can specify the content type as as output directly using the annotation.Testing
To see your hard labor with above, you simply need to run the Maven tomcat plugin. The previous article has shown you an command to do that. Once you restarted it, you should able to open a browser and use these URLS for testing.
- http://localhost:8081/spring-web-annotation/ping/tester1
- http://localhost:8081/spring-web-annotation/ping/tester2
- http://localhost:8081/spring-web-annotation/ping/tester3
- http://localhost:8081/spring-web-annotation/pings
Happing programming
From this simple exercise, you can quickly see Spring MVC brings you many benefits; and a lot of fun in developing web application. Spring, by design principles, tends to be developers friendly, boots productivity and un-intrusively in your environment. It’s one of the reason I like to work with it a lot. I hope you enjoy this tutorial and go further explore on your own.
Happy programming!
출처: http://www.javacodegeeks.com/2013/10/getting-started-with-spring-jdbc-in-a-web-application.html?utm_content=buffer358fb&utm_medium=social&utm_source=facebook.com&utm_campaign=buffer
'Spring > Boot(4.x)' 카테고리의 다른 글
[펌]Spring Cloud AWS를 이용한 S3 연동 (0) | 2016.01.10 |
---|---|
[펌]Spring Cloud AWS with proxy settings (0) | 2016.01.10 |
[펌]How to mock Spring bean (version 2) (0) | 2016.01.10 |
[펌]Spring Data Redis Example (0) | 2016.01.10 |
[펌]Getting started with Spring Cloud (0) | 2016.01.10 |