Like last time, I will make it with reference to here.
Introduce it because it is convenient for development
STS
in Help
→ ʻEclipse Marketplace` and install it.Help
→ Install new software
. EnterDoma Tools
and Next
TestEntity.java
package com.tharao.createdesigndocuments.entity;
import org.seasar.doma.Entity;
import org.seasar.doma.GeneratedValue;
import org.seasar.doma.GenerationType;
import org.seasar.doma.Id;
@Entity
public class TestEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
public String name;
}
TestEntityDao.java
package com.tharao.createdesigndocuments.dao;
import java.util.List;
import org.seasar.doma.Dao;
import org.seasar.doma.Insert;
import org.seasar.doma.Select;
import org.seasar.doma.boot.ConfigAutowireable;
import org.springframework.transaction.annotation.Transactional;
import com.tharao.createdesigndocuments.entity.TestEntity;
@ConfigAutowireable
@Dao
public interface TestEntityDao {
@Select
List<TestEntity> selectAll();
@Insert
@Transactional
int insert(TestEntity reservation);
}
TestService.java
package com.tharao.createdesigndocuments.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.tharao.createdesigndocuments.dao.TestEntityDao;
import com.tharao.createdesigndocuments.entity.TestEntity;
@Service
@Transactional
public class TestService {
@Autowired
TestEntityDao dao;
public List<TestEntity> getAllEntities() {
return dao.selectAll();
}
}
TestService.java
package com.tharao.createdesigndocuments.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.tharao.createdesigndocuments.entity.TestEntity;
import com.tharao.createdesigndocuments.service.TestService;
@RestController
public class TestController {
@Autowired
TestService service;
@RequestMapping(value = "test", method = RequestMethod.GET)
public List<TestEntity> getEntities() {
return service.getAllEntities();
}
}
selectAll
method of the Dao interface and select Doma
→ Jump to Slq File
to create it.selectAll.sql
selectAll.sql
select
id
,name
from
TestEntity
order by
id asc
Enter initial data
CreateDesignDocumentsApplication.java
package com.tharao.createdesigndocuments;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.tharao.createdesigndocuments.dao.TestEntityDao;
import com.tharao.createdesigndocuments.entity.TestEntity;
@SpringBootApplication
public class CreateDesignDocumentsApplication {
public static void main(String[] args) {
SpringApplication.run(CreateDesignDocumentsApplication.class, args);
}
@Autowired
TestEntityDao testEntityDao;
//Reservation Dao at startup#Insert initial data with insert
@Bean
CommandLineRunner runner() {
return args -> Arrays.asList("spring", "spring boot", "doma").forEach(s -> {
TestEntity r = new TestEntity();
r.name = s;
testEntityDao.insert(r);
});
}
}
Then set the SQL dialect in ʻapplication.properties`
application.properties
doma.dialect=h2
View the Eclipse project properties and select Java Compiler
→ Annotation Processing
Check Allow project-specific settings
Set the generated source directory
to .apt_generated
Add the following to processor options
ey | value |
---|---|
ao.subpackage | impl |
Select Java Compiler
→ Annotation Processing
→ Factory Path
Check Allow project-specific settings
Add the doma
jar to the annotation processor included plugin and JAR
and press ʻOK`
When I accessed [http: // localhost: 8080 / test](http: // localhost: 8080 / test), the data was safely displayed in JSON format.
I created the SQL files under src / main / resources
, but all the files are set in the exclusion
setting of src / main / resources
in the Java build path of the project, created by DaoImpl Was not done.
When I set the exclusion
of src / main / resources
to none, DaoImpl was successfully created and compiled.
Referenced URL Use Spring Boot + Doma2
Recommended Posts