03.Project Creation

Project Creation

This is the GitHub URL for sample project which is related to this part.

https://github.com/LahiruPriyankara/SpringBootAppOne.git

-----------------------------------------------------------------------------------



Java – You need java8 or upper versions.

IDE

Spring tool suite (STS) – Similar to eclipse ide because it develop on eclipse.

Intellij idea – If use this you have to take commercial edition. Community edition not provide spring boot.

To create spring boot project online, go to https://start.spring.io/ 

Spring Boot වලදී එය default JSP වලට support නොකරන අතර එය enable කර ගත යුතුය.මේ සදහා dependency එක add කර ගත යුතුය.මෙහිදී එම dependency එක embed කර ඇති tomcat version එකට සමාන version එකකින් ගැනීම වඩා සුදුසු වේ.

 Go to mvn repository and add tomcat jasper.

<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.37</version>
</dependency>



Creating a spring boot project.

File – new – spring starter project


Then,


H2 database is in memory database. It is not in outside. It is in application.

Database accessing URL : 

http://localhost:8080/h2-console/login.jsp?jsessionid=8c5cb85923229ec20c82fb6a26834721


NOTE : 

In spring boot  Database hitting part is different.

@Controller
public class HomeController {

    @Autowired
    StudentDao stu;
        
    @RequestMapping("student/getStudents")
    public String getStudents(@RequestParam("name") String name,@RequestParam("id") int id,Model model ) {

        model.addAttribute("sudents",stu.findAll());
        model.addAttribute("findByName"+stu.findByName(name));
        model.addAttribute("findByAgeGreaterThan"+stu.findByAgeGreaterThan(id));
        model.addAttribute("findByAgeAndName"+stu.findByAgeAndName(name,id));
        
        return "index";
    }
}

---------------------------------------------------

public interface StudentDao extends JpaRepository<Student, Integer> {
    
    List<Student> findByName(String name);
    List<Student> findByAgeGreaterThan(int age);
    
    @Query("from Student where name = ?1 and age = ?2")
    List<Student> findByAgeAndName(String name,int num);
    
}


As well as repository also can be convert to rest end point as fallows, 

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {

List<Person> findByLastName(@Param("name") String name);

}


NOTE : Rest response ලෙස default string return කරන අතර අවශ්‍ය නම් Jason return කරවා ගත හැක.නමුත් Rest එකෙන් xml return කරවගන්න නම් xml dependencies අවශ්‍ය වෙනවා.it is better to use version same as  jakson version.


<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.11.2</version>
</dependency>


-----------------------------------------------------------------------------------



Comments

Popular posts from this blog

09.Data Binding.

Database - Topics

02. Spring – Creating spring project clone it with GIT step by step.