스프링은 데이터베이스 연동을 위한 템플릿 클래스를 제공함으로써 Connection, Statement(PreparedStatement), ResultSet등을 생성하고 처리한 다음 close(반환)하는 JDBC의 중복된 코드를 줄일 수 있다
위 아래 같은 의미
위 아래 같은 의미
--------------------------ApplicationContext.xml--------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- <bean id="helloSpring" class="user.main.HelloSpring"/> 대신-->
<context:component-scan base-package="spring.conf"/>
<context:component-scan base-package="user.bean"/>
<context:component-scan base-package="user.dao"/>
<context:component-scan base-package="user.main"/>
<context:component-scan base-package="user.service"/>
</beans>
--------------------------db.properties--------------------------
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:xe
jdbc.username=c##java
jdbc.password=1234
--------------------------SpringConfiguration.java--------------------------
package spring.conf;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
// ApplicationContext.xml와 같은 역할을 하는 환경설정 파일이라고 알려줘야함 -> @Configuration
@Configuration
@PropertySource("classpath:spring/db.properties")
public class SpringConfiguration {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
private @Value("${jdbc.username}")String username;
private @Value("${jdbc.password}")String password;
@Bean
public BasicDataSource dataSource(){
/*
* BasicDataSource basicDataSource = new BasicDataSource();
* basicDataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
* basicDataSource.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
* basicDataSource.setUsername("c##java");
* basicDataSource.setPassword("1234");
*/
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setDriverClassName(driver);
basicDataSource.setUrl(url);
basicDataSource.setUsername(username);
basicDataSource.setPassword(password);
return basicDataSource;
}
}
--------------------------UserDTO.java--------------------------
package user.bean;
import org.springframework.stereotype.Component;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
@Component
public class UserDTO {
private String name;
private String id;
private String pwd;
@Override
public String toString() {
return name + "\t " + id + "\t" + pwd;
}
}
--------------------------UserDAO.interface--------------------------
package user.dao;
import java.util.List;
import java.util.Map;
import user.bean.UserDTO;
public interface UserDAO {
public void write(UserDTO userDTO);
public List<UserDTO> getUserList();
public UserDTO getUser(String id);
public void update(Map<String, String> map);
public void delete(String id);
}
--------------------------UserDAOImpl.java--------------------------
package user.dao;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;
import org.springframework.stereotype.Repository;
import user.bean.UserDTO;
@Repository // DB랑 연동하는 역할을 한다는 것을 @component를 대신에 사용(상관없음)
public class UserDAOImpl extends NamedParameterJdbcDaoSupport implements UserDAO {
@Autowired
public void setDS(DataSource dataSource) {
setDataSource(dataSource); // JdbcDaoSupport에 있는 setDataSource(~~)를 호출
}
@Override
public void write(UserDTO userDTO) {
Map<String, String> map = new HashMap<String,String>();
map.put("name", userDTO.getName());
map.put("id", userDTO.getId());
map.put("pwd", userDTO.getPwd());
String sql = "insert into usertable values(:name, :id, :pwd)";
getNamedParameterJdbcTemplate().update(sql, map);
}
@Override
public List<UserDTO> getUserList(){
String sql = "select * from usertable";
return getJdbcTemplate().query(sql, new BeanPropertyRowMapper<UserDTO>(UserDTO.class));
}
@Override
public UserDTO getUser(String id) {
String sql = "select * from usertable where id=?";
try{
return getJdbcTemplate().queryForObject(
sql, new BeanPropertyRowMapper<UserDTO>(UserDTO.class), id);
}catch(EmptyResultDataAccessException e) {
return null;
}
}
@Override
public void update(Map<String, String> map) {
String sql = "update usertale set name=:name, pwd=:pwd where id=:id";
getNamedParameterJdbcTemplate().update(sql, map);
}
@Override
public void delete(String id) {
String sql = "delete from usertale where id=?";
getJdbcTemplate().update(sql, id);
}
}
--------------------------HelloSpring.java--------------------------
package user.main;
import java.util.Scanner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import user.service.UserService;
public class HelloSpring {
public void menu(ApplicationContext applicationContext) {
Scanner scan = new Scanner(System.in);
UserService userService=null;
int num;
while(true) {
System.out.println("***********");
System.out.println(" 1. 입력");
System.out.println(" 2. 출력");
System.out.println(" 3. 수정");
System.out.println(" 4. 삭제");
System.out.println(" 5. 끝");
System.out.println("***********");
System.out.print("번호 입력 : ");
num = scan.nextInt();
System.out.println();
if(num == 5) break;
if(num == 1)
userService = (UserService)applicationContext.getBean("userInsertService");
else if(num == 2)
userService = (UserService)applicationContext.getBean("userSelectService");
else if(num == 3)
userService = (UserService)applicationContext.getBean("userUpdateService");
else if(num == 4)
userService = (UserService)applicationContext.getBean("userDeleteService");
userService.execute();
}//while
}
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/ApplicationContext.xml"); // - 사용하여 연결하고
HelloSpring helloSpring = applicationContext.getBean("helloSpring", HelloSpring.class); // - 불러오고
helloSpring.menu(applicationContext);
System.out.println("프로그램을 종료합니다.");
}
}
--------------------------HelloSpring.java--------------------------
package user.main;
import java.util.Scanner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import user.service.UserService;
@Component
public class HelloSpring {
public void menu(ApplicationContext applicationContext) {
Scanner scan = new Scanner(System.in);
UserService userService=null;
int num;
while(true) {
System.out.println("***********");
System.out.println(" 1. 입력");
System.out.println(" 2. 출력");
System.out.println(" 3. 수정");
System.out.println(" 4. 삭제");
System.out.println(" 5. 끝");
System.out.println("***********");
System.out.print("번호 입력 : ");
num = scan.nextInt();
System.out.println();
if(num == 5) break;
if(num == 1)
userService = (UserService)applicationContext.getBean("userInsertService");
else if(num == 2)
userService = (UserService)applicationContext.getBean("userSelectService");
else if(num == 3)
userService = (UserService)applicationContext.getBean("userUpdateService");
else if(num == 4)
userService = (UserService)applicationContext.getBean("userDeleteService");
userService.execute();
}//while
}
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/ApplicationContext.xml"); // - 사용하여 연결하고
HelloSpring helloSpring = applicationContext.getBean("helloSpring", HelloSpring.class); // - 불러오고
helloSpring.menu(applicationContext);
System.out.println("프로그램을 종료합니다.");
}
}
--------------------------UserInsertService.java--------------------------
package user.service;
import java.util.Scanner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import user.bean.UserDTO;
import user.dao.UserDAO;
@Service // 일을하는 집합체
public class UserInsertService implements UserService {
@Autowired // @으로 생성된 빈들 중에서 userDTO 타입을 찾아서 자동으로 매핑
private UserDTO userDTO;
@Autowired
private UserDAO userDAO;
@Override
public void execute() {
Scanner scan = new Scanner(System.in);
System.out.print("이름 입력 : ");
String name = scan.next();
System.out.print("아이디 입력 : ");
String id = scan.next();
System.out.print("비밀번호 입력 : ");
String pwd = scan.next();
System.out.println();
userDTO.setName(name);
userDTO.setId(id);
userDTO.setPwd(pwd);
// DB로 가자
userDAO.write(userDTO);
System.out.println(name + "님의 데이터를 저장하였습니다.");
}
}
--------------------------UserSelectService.java--------------------------
package user.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import user.bean.UserDTO;
import user.dao.UserDAO;
@Service
public class UserSelectService implements UserService {
@Autowired
private UserDAO userDAO;
@Override
public void execute() {
List<UserDTO> list = userDAO.getUserList();
for(UserDTO userDTO : list) {
System.out.println(userDTO); //userDTO를 찍어랑~
}//for
}
}
--------------------------UserUpdateService.java--------------------------
package user.service;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.Setter;
import user.bean.UserDTO;
import user.dao.UserDAO;
@Service
public class UserUpdateService implements UserService {
@Autowired
private UserDAO userDAO;
@Override
public void execute() {
System.out.println();
Scanner scan = new Scanner(System.in);
System.out.print("수정할 아이디 입력 : ");
String id = scan.next();
// DB
UserDTO userDTO = userDAO.getUser(id);
if(userDTO == null) {
System.out.println("수정하고자 하는 아이디가 없습니다.");
return;
}
System.out.println(userDTO);
System.out.println();
System.out.print("수정할 이름 입력 : ");
String name = scan.next();
System.out.print("수정할 비밀번호 입력 : ");
String pwd = scan.next();
Map<String, String> map = new HashMap<String, String>();
map.put("name", name);
map.put("id", id);
map.put("pwd", pwd);
userDAO.update(map);
System.out.println(id + "님의 데이터를 수정하였습니다.");
}
}
--------------------------UserDeleteService.java--------------------------
package user.service;
import java.util.Scanner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import user.bean.UserDTO;
import user.dao.UserDAO;
@Service
public class UserDeleteService implements UserService {
@Autowired
private UserDAO userDAO;
@Override
public void execute() {
System.out.println();
Scanner scan = new Scanner(System.in);
System.out.print("수정할 아이디 입력 : ");
String id = scan.next();
// DB
UserDTO userDTO = userDAO.getUser(id);
if(userDTO == null) {
System.out.println("삭제하고자 하는 아이디가 없습니다.");
return;
}
userDAO.delete(id);
System.out.println(id + "님의 데이터를 삭제하였습니다.");
}
}
--------------------------UserService.interface--------------------------
package user.service;
public interface UserService {
public void execute();
}
--------------------------pom.xml--------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>chapter04_XML</groupId>
<artifactId>chapter04_XML</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
</plugins>
</build>
<!-- Spring Context -->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
</dependency>
<!-- Connection pool -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.9.0</version>
</dependency>
<!-- Oracle -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11</artifactId>
<version>23.2.0.0</version>
</dependency>
<!-- Project Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
ORM(Object Relational Mapping) 프레임워크는 데이터베이스와 객체와의 관계를 맵핑시켜 퍼시스턴스 로직 처리를 도와주는 프레임워크이다.
myBatis는 SQL쿼리문, 예외처리, 트랙잭션 관리들을 XML형식으로 관리한다
POJO(Plain Old Java Object)객체와 테이블의 컬럼들을 편리하고 빠르고 정확하게 매칭할 수 있다.
myBatis의 목표와 특징은 쉽고, 간단하고, 의존성이 적다는 것이다
SQL문과 자바코드를 분리함으로 인해 자바 개발자는 쿼리문을 신경 쓰지 않아도 된다.
myBatis는 자바오브젝트와 SQL문 사이의 자동 매핑 기능을 지원하는 ORM 프레임워크이다
자바코드와 SQL를 분리하므로서 SQL문의 변경이 있을 때 마다 자바를 수정하지 않아도 되고 컴파일을 하지 않아도 된다
--------------------------ApplicationContext.xml--------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd">
<!-- Connection Pool & DataSource -->
<context:property-placeholder location="classpath:spring/db.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:spring/mybatis-config.xml" />
<property name="mapperLocations" value="classpath:user/dao/userMapper.xml" />
</bean>
<!-- SqlSession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" />
</bean>
<!-- Transaction -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource" />
</bean>
<bean id="helloSpring" class="user.main.HelloSpring" />
<bean id="userInsertService" class="user.service.UserInsertService">
<property name="userDTO" ref="userDTO" />
<property name="userDAO" ref="userDAOMybatis" />
</bean>
<bean id="userSelectService" class="user.service.UserSelectService">
<property name="userDAO" ref="userDAOMybatis" />
</bean>
<bean id="userUpdateService" class="user.service.UserUpdateService">
<property name="userDAO" ref="userDAOMybatis" />
</bean>
<bean id="userDeleteService" class="user.service.UserDeleteService">
<property name="userDAO" ref="userDAOMybatis" />
</bean>
<bean id="userDTO" class="user.bean.UserDTO"></bean>
<bean id="userDAOMybatis" class="user.dao.UserDAOMybatis">
<property name="sqlSession" ref="sqlSession" />
</bean>
</beans>
--------------------------db.properties--------------------------
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:xe
jdbc.username=c##java
jdbc.password=1234
--------------------------mybatis-config.xml--------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<configuration>
<typeAliases>
<typeAlias type="user.bean.UserDTO" alias="user" />
</typeAliases>
<!-- 둘중에 하나만 쓰면 됨
<mappers>
<mapper resource="user/dao/userMapper.xml"/>
</mappers>
-->
</configuration>
--------------------------UserDTO.java--------------------------
package user.bean;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class UserDTO {
private String name;
private String id;
private String pwd;
@Override
public String toString() {
return name + "\t" + id + "\t" + pwd;
}
}
--------------------------UserDAO.interface--------------------------
package user.dao;
import java.util.List;
import java.util.Map;
import user.bean.UserDTO;
public interface UserDAO {
public void write(UserDTO userDTO);
public List<UserDTO> getUserList();
public UserDTO getUser(String id);
public void update(Map<String, String> map);
public void delete(String id);
}
--------------------------UserDAOMybatis.java--------------------------
package user.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.springframework.transaction.annotation.Transactional;
import lombok.Setter;
import user.bean.UserDTO;
@Transactional
public class UserDAOMybatis implements UserDAO {
@Setter
private SqlSession sqlSession;
//@Transactional
@Override
public void write(UserDTO userDTO) {
sqlSession.insert("userSQL.write", userDTO);
}
@Override
public List<UserDTO> getUserList() {
return sqlSession.selectList("userSQL.getUserList");
}
@Override
public UserDTO getUser(String id) {
return sqlSession.selectOne("userSQL.getUser", id);
}
@Override
public void update(Map<String, String> map) {
sqlSession.update("userSQL.update", map);
}
@Override
public void delete(String id) {
sqlSession.delete("userSQL.delete", id);
}
}
--------------------------userMapper.xml--------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="userSQL">
<insert id="write" parameterType="user">
insert into usertable values(#{name}, #{id}, #{pwd})
</insert>
<select id="getUserList" resultType="user">
select * from usertable
</select>
<select id="getUser" parameterType="String" resultType="user">
select * from usertable where id=#{id}
</select>
<update id="update" parameterType="java.util.Map">
update usertable set name=#{name}, pwd=#{pwd} where id=#{id}
</update>
<delete id="delete" parameterType="String">
delete usertable where id=#{id}
</delete>
</mapper>
--------------------------HelloSpring.java--------------------------
package user.main;
import java.util.Scanner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import user.service.UserService;
public class HelloSpring {
public void menu(ApplicationContext applicationContext) {
Scanner scan = new Scanner(System.in);
UserService userService = null;
int num;
while(true) {
System.out.println();
System.out.println("**************");
System.out.println(" 1. 입력");
System.out.println(" 2. 출력");
System.out.println(" 3. 수정");
System.out.println(" 4. 삭제");
System.out.println(" 5. 끝");
System.out.println("**************");
System.out.print(" 번호 입력 : ");
num = scan.nextInt();
if(num == 5) break;
if(num == 1)
userService = (UserService) applicationContext.getBean("userInsertService");
else if(num == 2)
userService = (UserService) applicationContext.getBean("userSelectService");
else if(num == 3)
userService = applicationContext.getBean("userUpdateService", UserService.class);
else if(num == 4)
userService = applicationContext.getBean("userDeleteService", UserService.class);
userService.execute();//호출
}//while
}
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
HelloSpring helloSpring = (HelloSpring) applicationContext.getBean("helloSpring");
helloSpring.menu(applicationContext);
System.out.println("프로그램 종료합니다.");
}
}
--------------------------UserInsertService.java--------------------------
package user.service;
import java.util.Scanner;
import lombok.Setter;
import user.bean.UserDTO;
import user.dao.UserDAO;
public class UserInsertService implements UserService {
@Setter
private UserDTO userDTO;
@Setter
private UserDAO userDAO;
@Override
public void execute() {
System.out.println();
Scanner scan = new Scanner(System.in);
System.out.print("이름 입력 : ");
String name = scan.next();
System.out.print("아이디 입력 : ");
String id = scan.next();
System.out.print("비밀번호 입력 : ");
String pwd = scan.next();
userDTO.setName(name);
userDTO.setId(id);
userDTO.setPwd(pwd);
//DB
userDAO.write(userDTO);
System.out.println(name + "님의 데이터를 저장하였습니다.");
}
}
--------------------------UserSelectService.java--------------------------
package user.service;
import java.util.List;
import lombok.Setter;
import user.bean.UserDTO;
import user.dao.UserDAO;
public class UserSelectService implements UserService {
@Setter
private UserDAO userDAO;
@Override
public void execute() {
System.out.println();
List<UserDTO> list = userDAO.getUserList();
for(UserDTO userDTO : list) {
System.out.println(userDTO);
}//for
}
}
--------------------------UserUpdateService.java--------------------------
package user.service;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import lombok.Setter;
import user.bean.UserDTO;
import user.dao.UserDAO;
public class UserUpdateService implements UserService {
@Setter
private UserDAO userDAO;
@Override
public void execute() {
System.out.println();
Scanner scan = new Scanner(System.in);
System.out.print("수정 할 아이디 입력 : ");
String id = scan.next();
//DB
UserDTO userDTO = userDAO.getUser(id);
if(userDTO == null) {
System.out.println("찾고자 하는 아이디가 없습니다.");
return;
}
System.out.println(userDTO);
System.out.println();
System.out.print("수정 할 이름 입력 : ");
String name = scan.next();
System.out.print("수정 할 비밀번호 입력 : ");
String pwd = scan.next();
Map<String, String> map = new HashMap<String, String>();
map.put("name", name);
map.put("id", id);
map.put("pwd", pwd);
userDAO.update(map);
System.out.println(id + "님의 데이터를 수정하였습니다.");
}
}
--------------------------UserDeleteService.java--------------------------
package user.service;
import java.util.Scanner;
import lombok.Setter;
import user.bean.UserDTO;
import user.dao.UserDAO;
public class UserDeleteService implements UserService {
@Setter
private UserDAO userDAO;
@Override
public void execute() {
System.out.println();
Scanner scan = new Scanner(System.in);
System.out.print("삭제 할 아이디 입력 : ");
String id = scan.next();
//DB
UserDTO userDTO = userDAO.getUser(id);
if(userDTO == null) {
System.out.println("찾고자 하는 아이디가 없습니다.");
return;
}
userDAO.delete(id);
System.out.println(id + "님의 데이터를 삭제하였습니다.");
}
}
--------------------------UserService.interface--------------------------
package user.service;
public interface UserService {
public void execute();
}
--------------------------pom.xml--------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>chapter05_XML</groupId>
<artifactId>chapter05_XML</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.23</version>
</dependency>
<!-- Connection Pool -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.9.0</version>
</dependency>
<!-- Oracle -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11</artifactId>
<version>23.2.0.0</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.13</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.2</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
와 같이 바꿔줄 수 있다..............
너무 어렵다 계속 반복 학습을 해야겠다.