Skip to content
Snippets Groups Projects
Commit ee83b363 authored by 황 재인's avatar 황 재인
Browse files

third

parent 15e7f382
Branches master
No related tags found
No related merge requests found
package com.example.demo;
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.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.ApplicationContext;
import java.util.Arrays;
import com.example.demo.entity.Item;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.Resource;
@RestController
@SpringBootApplication
public class DemoApplication {
// @EntityScan(basePackageClasses = {Item.class})
public class DemoApplication{// implements CommandLineRunner{
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
// @Autowired
// private ApplicationContext appContext;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
Resource[] res = new PathMatchingResourcePatternResolver().getResources("mappers/UserMapper.xml");
sessionFactory.setMapperLocations(res);
return sessionFactory.getObject();
}
}
package com.example.demo.controller;
import java.util.List;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.dao.UserDAO;
import com.example.demo.dto.UserDTO;
@RestController
@MapperScan(basePackages="com.example.demo.dao")//탐색할 패키시 설정
public class UserController {
@Autowired
private UserDAO userDAO;//UserDAO bean을 자동으로 주입
@RequestMapping("/users")
public List<UserDTO> users(@RequestParam(value="country", defaultValue = "")String country) throws Exception { //query String으로 country를 받도록 설정
final UserDTO param = new UserDTO(0, null, country);
final List<UserDTO> userList = userDAO.selectUsers(param);
return userList;
}
}
\ No newline at end of file
package com.example.demo.dao;
import java.util.List;
import com.example.demo.dto.UserDTO;
public interface UserDAO {
List<UserDTO> selectUsers(UserDTO param) throws Exception;
}
\ No newline at end of file
package com.example.demo.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@AllArgsConstructor // 자동으로 모든 매개변수를 받는 생성자를 생성
@Getter // Getter 생성
@Setter // Setter 생성
public class UserDTO {
private int seq;
private String name;
private String country;
}
\ No newline at end of file
<?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.example.demo.dao.UserDAO"><!--namespace를 통해 UserDAO와 연결합니다. -->
<select id="selectUsers" parameterType="com.example.demo.dto.UserDTO"
resultType="com.example.demo.dto.UserDTO">
SELECT `seq`, `name`, `country` FROM lab03
<if test='country != null and country != ""'>
WHERE country = #{country}
</if>
</select>
</mapper>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment