import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.DeptDTO;
import com.service.DeptService;
public class MainClass {
public static void main(String[] args) {
ApplicationContext ctx = new GenericXmlApplicationContext("classpath:com/config/dept.xml");
DeptService service = ctx.getBean("deptService", DeptService.class);
// 1. 저장 ==> Spring + Mybatis는 자동으로 commit된다.
// int num = service.insert(new DeptDTO(99, "IT", "부산"));
// int num2 = service.update(new DeptDTO(99, "AI", "서울"));
// int num3 = service.delete(99);
// transaction 처리
try {
service.tx();
} catch (Exception e) {
System.out.println(e.getMessage());
}
List<DeptDTO> list = service.selectAll();
System.out.println(list);
}
}
DeptDAO.java
package com.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.dto.DeptDTO;
@Repository("deptDAO")
public class DeptDAO {
@Autowired
SqlSession session; // null
// SqlSessionTemplate session;
public List<DeptDTO> selectAll(){
return session.selectList("com.config.DeptMapper.selectAll");
}
public DeptDTO selectByDeptno(int deptno) {
return session.selectOne("com.config.DeptMapper.selectByDeptno", deptno);
}
public int insert(DeptDTO dto) {
return session.insert("com.config.DeptMapper.insert", dto);
}
public int update(DeptDTO dto) {
return session.update("com.config.DeptMapper.update", dto);
}
public int delete(int deptno) {
return session.delete("com.config.DeptMapper.delete", deptno);
}
}
DeptDTO.java
package com.dto;
import org.apache.ibatis.type.Alias;
@Alias("DeptDTO")
public class DeptDTO {
int deptno;
String dname;
String loc;
public DeptDTO() {
}
public DeptDTO(int deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
@Override
public String toString() {
return "DeptDTO [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
}
}
DeptService.java
package com.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.dto.DeptDTO;
@Service("deptService")
public interface DeptService {
// 전체 레코드 조회
public List<DeptDTO> selectAll();
// 특정 레코드 조회
public DeptDTO selectByDeptno(int deptno);
// 저장
public int insert(DeptDTO dto);
// 수정
public int update(DeptDTO dto);
// 삭제
public int delete(int deptno);
// 트랜잭션 실습 메서드
public void tx() throws Exception;
}
DeptServiceImpl.java
package com.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.dao.DeptDAO;
import com.dto.DeptDTO;
@Service("deptService")
public class DeptServiceImpl implements DeptService {
@Autowired
DeptDAO dao; //null
@Override
public List<DeptDTO> selectAll() {
return dao.selectAll();
}
@Override
public DeptDTO selectByDeptno(int deptno) {
return dao.selectByDeptno(deptno);
}
@Override
public int insert(DeptDTO dto) {
return dao.insert(dto);
}
@Override
public int update(DeptDTO dto) {
return dao.update(dto);
}
@Override
public int delete(int deptno) {
return dao.delete(deptno);
}
// 트랜잭션 실습 메서드
@Transactional
@Override
public void tx() throws Exception {
int num = dao.insert(new DeptDTO(96, "IT", "서울"));
int num2 = dao.delete(1);
}
}
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.DeptDTO;
import com.service.DeptService;
public class MainClass {
public static void main(String[] args) {
ApplicationContext ctx = new GenericXmlApplicationContext("classpath:com/config/dept.xml");
DeptService service = ctx.getBean("deptService", DeptService.class);
// 1. 저장 ==> Spring + Mybatis는 자동으로 commit된다.
// int num = service.insert(new DeptDTO(99, "IT", "부산"));
// int num2 = service.update(new DeptDTO(99, "AI", "서울"));
// int num3 = service.delete(99);
List<DeptDTO> list = service.selectAll();
System.out.println(list);
}
}
DeptDAO.java
package com.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.dto.DeptDTO;
@Repository("deptDAO")
public class DeptDAO {
@Autowired
SqlSession session; // null
// SqlSessionTemplate session;
public List<DeptDTO> selectAll(){
return session.selectList("com.config.DeptMapper.selectAll");
}
public DeptDTO selectByDeptno(int deptno) {
return session.selectOne("com.config.DeptMapper.selectByDeptno", deptno);
}
public int insert(DeptDTO dto) {
return session.insert("com.config.DeptMapper.insert", dto);
}
public int update(DeptDTO dto) {
return session.update("com.config.DeptMapper.update", dto);
}
public int delete(int deptno) {
return session.delete("com.config.DeptMapper.delete", deptno);
}
}
DeptDTO.java
package com.dto;
import org.apache.ibatis.type.Alias;
@Alias("DeptDTO")
public class DeptDTO {
int deptno;
String dname;
String loc;
public DeptDTO() {
}
public DeptDTO(int deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
@Override
public String toString() {
return "DeptDTO [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
}
}
DeptService.java
package com.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.dto.DeptDTO;
@Service("deptService")
public interface DeptService {
// 전체 레코드 조회
public List<DeptDTO> selectAll();
// 특정 레코드 조회
public DeptDTO selectByDeptno(int deptno);
// 저장
public int insert(DeptDTO dto);
// 수정
public int update(DeptDTO dto);
// 삭제
public int delete(int deptno);
}
DeptServiceImpl.java
package com.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dao.DeptDAO;
import com.dto.DeptDTO;
@Service("deptService")
public class DeptServiceImpl implements DeptService {
@Autowired
DeptDAO dao; //null
@Override
public List<DeptDTO> selectAll() {
return dao.selectAll();
}
@Override
public DeptDTO selectByDeptno(int deptno) {
return dao.selectByDeptno(deptno);
}
@Override
public int insert(DeptDTO dto) {
return dao.insert(dto);
}
@Override
public int update(DeptDTO dto) {
return dao.update(dto);
}
@Override
public int delete(int deptno) {
return dao.delete(deptno);
}
}
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service = ctx.getBean("service", UserService.class);
List<String> list = service.list();
System.out.println(list);
}
}
UserDAO.java
package com.dao;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
// @Component // @Component는 전부 Class Level
@Repository("dao")
public class UserDAO {
public UserDAO() {
System.out.println("UserDAO 생성자");
}
// DB 연동 가정
public List<String> list(){
List<String> list = Arrays.asList("고길동", "홍길동", "신길동");
return list;
}
}
UserService.java
package com.service;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.dao.UserDAO;
// POJO Class
// @Component // @Component는 전부 Class Level
@Service("service")
public class UserService {
@Autowired
UserDAO dao; // property (인스턴스 변수)
public UserService() {
System.out.println("UserService 생성자");
}
public List<String> list(){
return dao.list();
}
}
user.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"
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">
<!-- @ 어노테이션 활성화 -->
<!-- <context:annotation-config /> --> <!-- empty tag -->
<!-- 내부적으로 <context:annotation-config /> 기능 포함 -->
<!-- 마킹된(Component) Bean들의 Pakage Name 지정 -->
<context:component-scan base-package="com.service, com.dao" /> <!-- empty tag -->
<!-- <context:component-scan base-package="com.*" /> -->
</beans>
<!-- @ 어노테이션 활성화 -->
<context:annotation-config />
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx = new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service = ctx.getBean("service", UserService.class);
List<String> list = service.list();
System.out.println(list);
UserService service2 = ctx.getBean("service2", UserService.class);
List<String> list2 = service2.list();
System.out.println(list2);
}
}
UserService.java
package com.service;
import java.util.List;
import com.dao.UserDAO;
// POJO Class
public class UserService {
// property (인스턴스 변수)
UserDAO dao;
// setter 메서드 주입
public UserService(UserDAO dao) {
this.dao = dao;
}
public List<String> list(){
return dao.list();
}
}
UserDAO.java
package com.dao;
import java.util.Arrays;
import java.util.List;
public class UserDAO {
// DB 연동 가정
public List<String> list(){
List<String> list = Arrays.asList("고길동", "신길동", "홍길동");
return list;
}
}
user.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:c="http://www.springframework.org/schema/c"
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">
<bean id="dao" class="com.dao.UserDAO" /> <!-- bean 태그로 등록하면 기본 생성자 호출 가능 -->
<!-- 다음 코드와 동일
UserDAO userDAO = new UserDAO();
-->
<bean id="service" class="com.service.UserService"> <!-- id는 식별 가능한 값, class는 클래스 입력 -->
<constructor-arg name="dao" ref="dao" />
</bean>
<!-- 다음 코드와 동일
UserService service = new UserService();
service.setDAO(UserDAO);
-->
<!--
c namespace
-->
<bean id="service2" class="com.service.UserService" c:dao-ref="dao" />
<!--
UserService service2= new UserService(dao);
-->
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service =
ctx.getBean("service", UserService.class);
System.out.println(service.getMesg());
UserService service2 =
ctx.getBean("service2", UserService.class);
System.out.println(service2.getMesg());
}
}
UserService.java
package com.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// POJO Class
public class UserService {
String mesg; // null ==> 외부에서 문자열을 주입
// 생성자 주입
public UserService(String m) {
this.mesg = m;
}
public String getMesg() {
return mesg;
}
}
user.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:c="http://www.springframework.org/schema/c"
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">
<bean id="service" class="com.service.UserService">
<constructor-arg name="m" value="helloWorld" />
</bean>
<!--
위의 코드는 다음 코드와 동일하다.
UserService service = new UserService("helloWorld");
-->
<!--
c namespace
-->
<bean id="service2" class="com.service.UserService" c:m="happyworld" />
</beans>
* 출력 화면
Spring Injection_shortcut_p 2
MainClass.java
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx = new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service = ctx.getBean("service", UserService.class);
List<String> list = service.list();
System.out.println(list);
UserService service2 = ctx.getBean("service2", UserService.class);
List<String> list2 = service2.list();
System.out.println(list2);
}
}
UserService.java
package com.service;
import java.util.List;
import com.dao.UserDAO;
// POJO Class
public class UserService {
// property (인스턴스 변수)
UserDAO dao;
// setter 메서드 주입
public UserService(UserDAO dao) {
this.dao = dao;
}
public List<String> list(){
return dao.list();
}
}
UserDAO.java
package com.dao;
import java.util.Arrays;
import java.util.List;
public class UserDAO {
// DB 연동 가정
public List<String> list(){
List<String> list = Arrays.asList("고길동", "신길동", "홍길동");
return list;
}
}
user.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:c="http://www.springframework.org/schema/c"
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">
<bean id="dao" class="com.dao.UserDAO" /> <!-- bean 태그로 등록하면 기본 생성자 호출 가능 -->
<!-- 다음 코드와 동일
UserDAO userDAO = new UserDAO();
-->
<bean id="service" class="com.service.UserService"> <!-- id는 식별 가능한 값, class는 클래스 입력 -->
<constructor-arg name="dao" ref="dao" />
</bean>
<!-- 다음 코드와 동일
UserService service = new UserService();
service.setDAO(UserDAO);
-->
<!--
c namespace
-->
<bean id="service2" class="com.service.UserService" c:dao-ref="dao" />
<!--
UserService service2= new UserService(dao);
-->
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service =
ctx.getBean("service", UserService.class);
System.out.println(service.getMesg());
UserService service2 =
ctx.getBean("service2", UserService.class);
System.out.println(service2.getMesg());
}
}
UserService.java
package com.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// POJO Class
public class UserService {
String mesg; // null ==> 외부에서 문자열을 주입
// setter 메서드 주입
public void setMesg(String mesg) {
System.out.println("setMesg 메서드");
this.mesg = mesg;
}
public String getMesg() {
return mesg;
}
}
user.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="service" class="com.service.UserService"> <!-- id는 식별 가능한 값, class는 클래스 입력 -->
<property name="mesg" value="HelloWorld" /> <!-- System.out.println("setMesg 메서드"); property에 의해 출력 -->
</bean>
<!-- 아래 코드와 동일
UserService service = new UserService();
service.setMesg("HelloWorld");
-->
<bean id="service2" class="com.service.UserService" p:mesg="HappyWorld" />
<!-- 아래 코드와 동일
UserService service2 = new UserService();
service2.setMesg("HelloWorld");
-->
</beans>
* 출력 화면
Spring Injection_shortcut_p 2
MainClass.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx = new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service = ctx.getBean("service", UserService.class);
System.out.println(service.getMesg());
System.out.println(service);
UserService service2 = ctx.getBean("service2", UserService.class);
System.out.println(service2.getMesg());
System.out.println(service2);
UserService service3 = ctx.getBean("service3", UserService.class);
System.out.println(service3.getMesg());
System.out.println(service3);
UserService service4 = ctx.getBean("service4", UserService.class);
System.out.println(service4.getMesg());
System.out.println(service4);
}
}
UserService.java
package com.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// POJO Class
public class UserService {
// property (인스턴스 변수)
String mesg; // null ==> 외부에서 문자열을 주입
int num; // 0 ==> 외부에서 값을 주입
// setter 메서드 주입
public void setMesg(String mesg) {
this.mesg = mesg;
}
public void setNum(int num) {
this.num = num;
}
public void setMesgNum(String mesg, int num) {
this.mesg = mesg;
this.num = num;
}
// getter
public String getMesg() {
return mesg;
}
public int getNum() {
return num;
}
// toString
@Override
public String toString() {
return "UserService [mesg=" + mesg + ", num=" + num + "]";
}
}
user.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="service" class="com.service.UserService"> <!-- id는 식별 가능한 값, class는 클래스 입력 -->
<property name="mesg" value="HelloWorld" /> <!-- System.out.println("setMesg 메서드"); property에 의해 출력 -->
</bean>
<!--
UserService service = new UserService();
service.setMesg("HelloWorld");
-->
<bean id="service2" class="com.service.UserService">
<property name="num" value="20" />
</bean>
<bean id="service3" class="com.service.UserService">
<property name="mesg" value="Happy" />
<property name="num" value="30" />
</bean>
<!-- setter based injection -->
<!--
p namespace 사용
-->
<bean id="service4" class="com.service.UserService" p:mesg="Lucky" p:num="40"/>
</beans>
* 출력 화면
Spring Injection_shortcut_p 3
MainClass.java
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx = new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service = ctx.getBean("service", UserService.class);
List<String> list = service.list();
System.out.println(list);
UserService service2 = ctx.getBean("service2", UserService.class);
List<String> list2 = service.list();
System.out.println(list2);
}
}
UserService.java
package com.service;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.dao.UserDAO;
// POJO Class
public class UserService {
// property (인스턴스 변수)
UserDAO dao;
// setter 메서드 주입
public void setDao(UserDAO dao) {
this.dao = dao;
}
public List<String> list(){
return dao.list();
}
}
UserDAO.java
package com.dao;
import java.util.Arrays;
import java.util.List;
public class UserDAO {
// DB 연동 가정
public List<String> list(){
List<String> list = Arrays.asList("고길동", "신길동", "홍길동");
return list;
}
}
user.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDAO" class="com.dao.UserDAO" /> <!-- bean 태그로 등록하면 기본 생성자 호출 가능 -->
<!-- 아래 코드와 동일
UserDAO userDAO = new UserDAO();
-->
<bean id="service" class="com.service.UserService"> <!-- id는 식별 가능한 값, class는 클래스 입력 -->
<property name="dao" ref="userDAO"></property>
</bean>
<!-- 아래 코드와 동일
UserService service = new UserService();
service.setDAO(UserDAO);
-->
<!--
p namespace 설정
-->
<bean id="service2" class="com.service.UserService" p:dao-ref="userDAO" />
<!-- 아래 코드와 동일
UserService service = new UserService();
service2.setDAO(UserDAO);
-->
</beans>