Spring + Ibatis 통합 개발 실례
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<sqlMap resource="ch10/SpringAndIbatis/Ibatis.xml" />
</sqlMapConfig>
2, ibatis sql map 파일 ibatis. xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap >
<typeAlias type="ch10.SpringAndIbatis.Ibatis" alias="user"/>
<resultMap id="ibatisTest" class="user" >
<result column="id" property="id" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
</resultMap>
<!-- -->
<select id="getAllUsers" resultMap="ibatisTest">
select * from Ibatis
</select>
<!-- -->
<select id="getUsersByName" resultMap="ibatisTest">
select * from Ibatis where name=#value#
</select>
<!-- id -->
<select id="getUsersById" resultMap="ibatisTest">
select * from Ibatis where id=#value#
</select>
<!-- -->
<insert id="insertUsers" parameterClass="user">
insert into ibatis (id,name) values (#id#,#name#)
</insert>
<!-- -->
<delete id="deleteUsers">
delete from ibatis where id=#value#
</delete>
<!-- -->
<delete id="updateUsers" parameterClass="user">
update ibatis set name=#name# where id=#id#
</delete>
</sqlMap>
3.spring 프로필
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>1234</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/spring</value>
</property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<!-- ibatis , sqlMap , “there is no statement..... ” -->
<property name="configLocation">
<value>ch10/SpringAndIbatis/sqlMapConfig.xml</value>
</property>
</bean>
<bean id="testDAO" class="ch10.SpringAndIbatis.TestDAO">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="sqlMapClient">
<ref bean="sqlMapClient"/>
</property>
</bean>
</beans>
4. Dao 구현 인터페이스
ackage ch10.SpringAndIbatis;
import java.util.List;
public interface IDAO {
public List getList();
public Ibatis getByName(String name);
public Ibatis getById(String id);
public void save(Ibatis ibatis);
public void delete(String id);
public void update(Ibatis ibatis);
}
5. DAO 실현 클래스
package ch10.SpringAndIbatis;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
public class TestDAO extends SqlMapClientDaoSupport implements IDAO {
public void delete(String id) {
getSqlMapClientTemplate().delete("deleteUsers", id);
}
public Ibatis getById(String id) {
return (Ibatis)getSqlMapClientTemplate().queryForObject("getUsersById",id);
}
public Ibatis getByName(String name) {
return (Ibatis)getSqlMapClientTemplate().queryForObject("getUsersByName",name);
}
public List getList() {
return getSqlMapClientTemplate().queryForList("getAllUsers",null);
}
public void save(Ibatis ibatis) {
getSqlMapClientTemplate().insert("insertUsers",ibatis);
}
public void update(Ibatis ibatis) {
getSqlMapClientTemplate().update("updateUsers", ibatis);
}
}
6. domain 대상
package ch10.SpringAndIbatis;
public class Ibatis {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Ibatis(){
}
public Ibatis(String id, String name) {
super();
this.id = id;
this.name = name;
}
}
7. 테스트 코드
package ch10.SpringAndIbatis;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("ch10/SpringAndIbatis/applicationContext.xml");
TestDAO testDAOImpl=(TestDAO)context.getBean("testDAO");
//
System.out.println(" ");
List result=new ArrayList();
result=testDAOImpl.getList();
for (Iterator iter = result.iterator(); iter.hasNext();) {
Ibatis element = (Ibatis) iter.next();
System.out.println(element.getName());
}
//
System.out.println(" ");
Ibatis ibatis=testDAOImpl.getByName("1");
System.out.println(ibatis.getName());
// id
System.out.println(" id ");
Ibatis ibatis1=testDAOImpl.getById("1");
System.out.println(ibatis.getName());
//
System.out.println("-----------------");
System.out.println(" ");
List result1=new ArrayList();
result1=testDAOImpl.getList();
for (Iterator iter = result1.iterator(); iter.hasNext();) {
Ibatis element = (Ibatis) iter.next();
System.out.println(element.getName());
}
testDAOImpl.save(new Ibatis("3","3")); //
System.out.println(" ");
List result2=new ArrayList();
result2=testDAOImpl.getList();
for (Iterator iter = result2.iterator(); iter.hasNext();) {
Ibatis element = (Ibatis) iter.next();
System.out.println(element.getName());
}
System.out.println("-----------------");
//
System.out.println("-----------------");
System.out.println(" ");
List result3=new ArrayList();
result3=testDAOImpl.getList();
for (Iterator iter = result3.iterator(); iter.hasNext();) {
Ibatis element = (Ibatis) iter.next();
System.out.println(element.getName());
}
testDAOImpl.delete("3"); //
System.out.println(" ");
List result4=new ArrayList();
result4=testDAOImpl.getList();
for (Iterator iter = result4.iterator(); iter.hasNext();) {
Ibatis element = (Ibatis) iter.next();
System.out.println(element.getName());
}
System.out.println("-----------------");
//
System.out.println("-----------------");
System.out.println(" ");
List result5=new ArrayList();
result5=testDAOImpl.getList();
for (Iterator iter = result5.iterator(); iter.hasNext();) {
Ibatis element = (Ibatis) iter.next();
System.out.println(element.getName());
}
Ibatis ibatis3=testDAOImpl.getById("1");
ibatis3.setName("new1");
testDAOImpl.update(ibatis3);//
System.out.println(" ");
List result6=new ArrayList();
result6=testDAOImpl.getList();
for (Iterator iter = result6.iterator(); iter.hasNext();) {
Ibatis element = (Ibatis) iter.next();
System.out.println(element.getName());
}
System.out.println("-----------------");
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.