I didn't bother so much a while ago, I had a hard time doing it recently, so I used it as a memo for myself.
Search for sts in the eclipse marketplace and press the install button.
You can use a dedicated development tool (STS), This time we will use a plugin.
Select New → Project → Spring Starter Project and
The dependency is like this.
Project creation is OK by pressing Done.
I will go like this.
HelloController.java
@Controller
public class HelloController {
@Autowired
private HelloService helloService;
@RequestMapping(value="hello")
public String init(Model model) {
List<HelloBean> list = helloService.selectName();
model.addAttribute("list",list);
return "hello";
}
}
HelloSerivce.java
@Service
public class HelloService {
@Autowired
private HelloMapper helloMapper;
public List<HelloBean> selectName(){
return helloMapper.selectEmpAll();
}
}
python
CREATE TABLE emp_name(
id int,
name varchar(20)
)
Create a table with the above contents. Since it is for testing, neither PRIMARY nor UNIQUE is set for the time being.
HelloMapper.java
@Mapper
public interface HelloMapper {
List<HelloBean> selectEmpAll();
}
HelloMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.mapper.HelloMapper">
<select id="selectEmpAll" resultType="com.demo.bean.HelloBean">
select * from
emp_name
</select>
</mapper>
hello.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<tr>
<th>employee number</th>
<th>Employee name</th>
</tr>
<tr th:each="emp : ${list}">
<td th:text="${emp.id}"></td>
<td th:text="${emp.name}"></td>
</tr>
</table>
</body>
</html>
Create mybatis config. If the column name of the table is snake case, convert it to camel case and Added a setting that associates with the bean variable name. I won't use it this time, but I want to remember it.
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
Load the created mybatis-config
SampleApplication.java
package com.demo;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
@SpringBootApplication
public class SampleApplication {
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
//Read config file
sessionFactory.setConfigLocation(new ClassPathResource("/mybatis-config.xml"));
return sessionFactory.getObject();
}
}
Added connection settings with DB.
application.properties
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/sample
spring.datasource.username=postgres #According to your own environment
spring.datasource.password=postgres #According to your own environment
That's it.
Right click on the project and run → maven install When BUILD SUCCESS appears, right-click again and execute → Spring boot application selection Open http: // localhost: 8080 / hello from your browser.
If it looks like this, it's OK.
Is the setting reduced compared to Spring MVC? I think it will be easier to understand once you get used to it.
Recommended Posts