CREATION OF THE WEB APP PERSISTENCE LAYER
We will now see, after creating the first domain class, how to create the persistence layer. We are going to create specific interfaces that provide methods, which are normally CRUD operations, that is, create, read, update and delete on the database. Once the interfaces are created, we will create the classes that implement them, which will have a fundamental notation, @Repository never to be forgotten in order to avoid Spring MVC errors. We create a new package, com.xantrix.webapp.repository. Since we have implemented the Articoli domain class, it is clear that the first interface we will create will be the ArticoliRepository.
As can be seen, we have two overloaded selection methods, in the former a dynamic filter by item code and description is specified, in the latter in addition to filtering the items, we can decide the sorting and on which field to filter the records in the ARTICOLI table. InsArticle will deal with insertion and editing, while the last method deals with deletion. We now create the class that implements this interface ArticoliRepositoryImpl. We must not forget to put the notation @Repository otherwise the IOC will go into error sometimes even showing unintelligible messages. To interface with the database we will use the T-SQL language, which is very powerful but, as you will see, sometimes has pitfalls.
THE JDBC TEMPLATE
In order to run SQL queries, one of the components seen in the configuration package, the JdbcTemplate with the @Autowired notation for dependencies Injection, must be used. This means that when the project is at startup, the JdbcTemplate will be started; in fact, the instance that will be created at startup is the essence of the IOC. The JdbcTemplate alone is not enough to execute the SQL query, we need an ArticoliMapper class that plays the following important role, that is, it takes the recordset obtained from the JdbcTemplate and maps it to our Articoli domain class. The end result will be a collection of articles appropriately mapped by the ArticoliMapper class. Let’s see what this class looks like.
THE MAPROW METHOD
The first argument of the mapRow method represents our recordset, the second the row number. Basically, the code you see takes the columns in the database’s ARTICOLI table one row at a time and maps them with their respective setters to the Articoli domain class. This technique is not widely used; it is preferable to use Store Procedures, which give us more flexibility and greater security.
STORE PROCEDURE CREATION IN SQL SERVER
SQL code we can also use in our Web Apps, however it has a problem, it is prone to Sql Injection a technique widely used in the hacker field. Using Store Procedures puts us somewhat safe from possible breaches of our databases, plus it provides portability. This means that with a Store Procedure the SQL code does not reside directly in the Web App but is embedded within the database; therefore, a change to the SQL code does not have to be repeated throughout the application but only in the procedure. Now let’s get down to the concrete and see how to create a Store Procedure. The code I report explains step by step how to create SP in SQL Server.
CREATION OF THE STORE ENTRY PROCEDURES
Having created the Store Selection Procedures, we go on to create the SP that handles editing and insertion. There will be a check on the item code, if present we will proceed with the change otherwise with the insertion of a new item.
CREATION OF THE SERVICE LAYER
The service layer allows you to connect the controller with the persistence layer. In MVC architecture you never have to directly connect the persistence layer with the controller, we need an intermediate layer. Normally it is in this layer that you put the application logic. Let’s create a new package, com.xantrix.webapp.service, with a new ArticoliService interface; we will put the exact same methods in this interface as we saw in the persistence layer.
Now we create the class that will implement this interface ArticoliServiceImpl. The most important part is to insert the @Service notation, which should never be forgotten, otherwise the IOC of our application will fail and will not start. Once the interface has been implemented we need to add one more important piece, the persistence layer via dependencies injection. In this ways we are doing code injection of the repository layer within the service layer. Business Logic should be put here, for example, if we need to implement a discount, we will do it in this layer.
DOWNLOAD ARTICLE CODE
Below is the video for importing into your project workspace. At the moment I have not explained the controller and the JSP page articles yet. I will do that in the next post.
Leave A Comment