Creating Spring Boot MVC application with AWS DynamoDB in 10 mins

Image
AWS DynamoDB DB is a serverless NOSQL database. You can understand how to build a spring boot Java web MVC application (Game Leaderboard) reading a AWS DynamoDB in 10 mins. Source of the demo code: https://github.com/babysteps-topro/spring-mvc-dynamodb Command to run the project: mvn spring-boot:run Video explain the table design: https://youtu.be/V0GtrBfY7XM Prerequisite: Install the AWS CLI: https://youtu.be/pE-Q_4YXlR0 Video explain the how to create the table: https://youtu.be/sBZIVLlmpxY

Lightweight Web Application Framework: PrimeFaces (JSF) + Guice + MyBatis (PART 2)

In this part, I will continue to demonstrate the integration of JSF, Guice and MyBatis. DBCP connection pool and MYSQL database is used in persistence layer

Integrate Google Guice with MyBatis
In the previous post, we have created a ServletContextListener. Now, we just bind the BasicDataSourceProvider and JdbcTransactionFactory in the contextInitialized method.

GuiceContextListener.java
package org.borislam;

import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.apache.log4j.xml.DOMConfigurator;
import org.borislam.mapper.StaffMapper;
import org.borislam.service.SimpleService;
import org.borislam.service.impl.SimpleServiceImpl;
import org.mybatis.guice.MyBatisModule;
import org.mybatis.guice.datasource.dbcp.BasicDataSourceProvider;
import org.mybatis.guice.datasource.helper.JdbcHelper;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.google.inject.name.Names;

public class GuiceContextListener implements ServletContextListener {

   public void contextDestroyed(ServletContextEvent servletContextEvent) {
     ServletContext servletContext = servletContextEvent.getServletContext();
     servletContext.removeAttribute(Injector.class.getName());
   }

   public void contextInitialized(ServletContextEvent servletContextEvent) {
     Injector injector = Guice.createInjector(
       new MyBatisModule() {
        @Override
        protected void initialize() {         
         install(JdbcHelper.MySQL);
        
         environmentId("development");
                        bindDataSourceProviderType(BasicDataSourceProvider.class);
         bindTransactionFactoryType(JdbcTransactionFactory.class);
         Names.bindProperties(binder(), createServerProperties());
         
         //add singleton service class
         bind(SimpleService.class).to(SimpleServiceImpl.class).in(Singleton.class); 
         
         //add MyBatis Service class
         addMapperClass(StaffMapper.class);
         }
        }
       );
     
     ServletContext servletContext = servletContextEvent.getServletContext();
     servletContext.setAttribute(Injector.class.getName(), injector);
     
     //log4J
  DOMConfigurator.configure(
    Thread.currentThread().getContextClassLoader()
     .getResource("log4j.xml")
    );
   }
   
   protected static Properties createServerProperties() {
         Properties myBatisProperties = new Properties();

         myBatisProperties.setProperty("JDBC.host", "localhost");
         myBatisProperties.setProperty("JDBC.port", "3306");
         myBatisProperties.setProperty("JDBC.schema", "ttcoach");
         
         myBatisProperties.setProperty("JDBC.username", "root");
         myBatisProperties.setProperty("JDBC.password", "");
         myBatisProperties.setProperty("JDBC.autoCommit", "false");
         return myBatisProperties;
   }

}


Prepare your MyBatis mapper class and model class
Staff.java
package org.borislam.model;

public class Staff {
 private String code;
 private String name;
 private String sex;
 private String tel;
 public String getCode() {
  return code;
 }
 public void setCode(String code) {
  this.code = code;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }

 public String getTel() {
  return tel;
 }
 public void setTel(String tel) {
  this.tel = tel;
 } 
 
}

StaffMapper.java
package org.borislam.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.borislam.model.Staff;


public interface StaffMapper {
    final String SELECT_ALL = "SELECT * FROM FREELANCER";
    final String SELECT_BY_CODE = "SELECT * FROM FREELANCER WHERE CODE = #{code}";
    
    /**
     * Returns the list of all Freelancer instances from the database.
     * @return the list of all Freelancer instances from the database.
     */
    @Select(SELECT_ALL)
    @Results(value = {
        @Result(property="code", column="code"),
        @Result(property="name", column="name"),
        @Result(property="sex", column="sex"),
        @Result(property="tel", column="tel")
    })
    List<Staff> selectAll();
 
    /**
     * Returns a Freelancer instance from the database.
     * @param id primary key value used for lookup.
     * @return A Freelancer instance with a primary key value equals to pk. null if there is no matching row.
     */
    @Select(SELECT_BY_CODE)
    @Results(value = {
            @Result(property="code", column="code"),
            @Result(property="name", column="name"),
            @Result(property="sex", column="sex"),
            @Result(property="tel", column="tel")
    })
    Staff selectByCode(String code);
}


Prepare your Service Class
SimpleService.java
package org.borislam.service;

public interface SimpleService {
 public void doSimpleThing();
}

SimpleServiceImpl.java
package org.borislam.service.impl;

import java.util.List;

import org.borislam.mapper.StaffMapper;
import org.borislam.model.Staff;
import org.borislam.service.SimpleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;

public class SimpleServiceImpl implements SimpleService {
 
 private StaffMapper staffMapper;
 
 Logger logger = LoggerFactory.getLogger(this.getClass());
 
 @Inject
 public void setStaffMapper(StaffMapper staffMapper) {
  this.staffMapper = staffMapper;
 }

 public void doSimpleThing() {
  List<Staff> staffList = staffMapper.selectAll();
  logger.debug("size 1: " + staffList.size());
  
  Staff staff = staffMapper.selectByCode("c001");
  logger.debug("Code1 : " + staff.getCode());
  logger.debug("Name 1: " + staff.getName());;
 }
}



Prepare your JSF Backing Bean and xhtml page
TestBean.java
package org.borislam.view;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.inject.Inject;
import org.borislam.service.SimpleService;
import org.borislam.service.TestService;

@ManagedBean
@SessionScoped
public class TestBean extends BasePageBean {
  
 private SimpleService sService;
 
 @Inject
 public void setsService(SimpleService sService) {
  this.sService = sService;
 }

 public String doTest(){
  System.out.println("test 1 inside backing bean...");
  sService.doSimpleThing();
  return "";
 }
 
 public String doTest2(){
  System.out.println("test 2 inside backing bean...");
  sService.doSimpleThing();
  return "";
 }
}

index.xhtml
<html xmlns="http://www.w3c.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<style>
.ui-widget, .ui-widget .ui-widget {
font-size: 80% !important;
}
</style>
</h:head>
<h:body>
<h:form>
 <h:outputText value="#{msg['website.title']}" />
    <p:calendar id="popupButtonCal" showOn="button" />
    <p:commandButton value="TEST2" action="#{testBean.doTest}"/>
 <p:editor/>
 <br/>
</h:form>
</h:body>
</html>
  


That's done! You can now ready to write your JSF application based on this framework.

Comments

Mehmet Sen said…
Very useful article, thanks.
Can you add a link to full source of this project?
Anonymous said…
It should be noted that whilst ordering papers for sale at paper writing service, you can get unkind attitude. In case you feel that the bureau is trying to cheat you, don't buy term paper from it.
360DigiTMG big data analytics malaysia
360DigiTMG data scientist certification malaysia
360DigiTMG data science course
360DigiTMG data analytics courses
360DigiTMG
Unknown said…
Where to Bet on Sports To Bet On Sports In Illinois
The best sports bet nba매니아 types 토토 사이트 모음 and bonuses available in www.jtmhub.com Illinois. The most common 토토 sports betting options available. Bet $20, Win $150, Win $100 or

Popular posts from this blog

Sample Apps: Spring data MongoDB and JSF Integration tutorial (PART 1)

Customizing Spring Data JPA Repository

Adding Hibernate Entity Level Filtering feature to Spring Data JPA Repository