이전 기사에서 세 가지 방법을 요약했지만 SQL에 주석을 달 수있는 두 가지 방법이 있습니다. 이 방법은 매우 혼란 스럽기 때문에 모든 사람이 많이 사용하지 않습니다. 일반적으로 사용되는 두 가지 방법을 요약하겠습니다.
1. DAO를 쓰지 않고 동적 프록시 구현 클래스
이 방법은 비교적 간단합니다. DAO 계층을 구현할 필요는 없으며 인터페이스를 정의하면됩니다. 여기에서는 구성 파일을 녹음하는 것이므로 프로그램은 매우 간단합니다.
1. 전체 구조 다이어그램 :
2. 3 개의 구성 파일과 1 개의 매핑 파일
(1) 프로그램 입구 및 프론트 엔드 컨트롤러 구성 Web.xml
<? xml version = "1.0"encoding = "utf-8"?> <web-app xmlns : xsi = "http://www.w3.org/2001/xmlschema-instance"xmlns = "http://java.sun.com/xml/ns/javaee" xsi : schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_3_0.xsd"3. ""webapp_ "" " <display-name> 웹 사이트 1 </display-name> <!-웹 컨테이너가 시작될 때 ApplicationContext의 구성 정보를 자동으로 조립하고 자동 조립-> <liseer-class.spramework.web.context.contextLoaderListener </listerclass> </lartiser> <!-Context-Param | <contept-param incling set spring 컨테이너 <Param-Name> ContextConfigLocation </param-name> <param-value> classpath : config/springmvc-servlet.xml, configg/applicationcontext.xml </param- value> </context-param> <!-문자 인코딩 필터 --> <filter> <filter-name> encipilter </filter-name> <filter-class> org.springframework.web.filter.characterencodingfilter </filter-class> <init-param> incoding </param-name> <param-value> utf-8 </param-value> <init-param> init-param> <param-name> force-name> <Param-value> true </param-value> </init-param> </filter> <filter-mapping> <filter-name> encodingfilter </filter-name> <Url-pattern>*. <Servlet-Class> org.springframework.web.servlet.dispatcherServlet </servlet-class> <init-param> <param-name> contextConfigLocation </param-name> <param-value> classpath : config/springmvc-servlet.xml </init-param> <! </load-on-startup> </load-on-startup> </servlet> <servlet-mapping> <servlet-mapping> <servlet-name> springmvc </servlet-name> <!-인터셉트 요청-> <Url-pattern>*. do </url-pattern> </servlet--list> <나는 환영-파일> index.html.html. <welcome-file> index.htm </welcome-file> <welcome-file> index.jsp </welcome-file> default.html </welcome-file> <welcome-file> default.htm </welcome-file> <welcce-file> default.jsp </welcome-file> </welcome-file-list> </web-app>
(2) 스캔 제어 레이어, 자동 분사 및 뷰 파서 구성 SpringMVC-Servlet.xml
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.w3.org/2001/xmlschema-instance" xmlns : aop = "http://www.springframework.org/schema/aop"xmlns : context = "http://www.springframework.org/schema/context"xmlns : mvc = "http://www.spramframwerk.org/schema/gramegr.org/gramegrogr.org/ xmlns : util = "http://www.springframework.org/schema/util"xmlns : tx = "http://www.springframework.org/schema/tx"xmlns : jdbc = "http://www.spramframwork.org/schema/gramework.org/schema/spramwork.org. XMLNS : "http://www.springframework.org/schema/cache"xsi : schemalocation = "http://www.springframework.org/schema/beans http://ww.springframework.org/schema/spred-beans- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.spramework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/sprring-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.spramework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd "> <!-주석 드라이버-> <mvc : mvc : mvc : mantation-driven/> <!-<context : annotation-config/>-> <!-context : component-scan-<! <context : component-scan base-package = "com.website.controller"> </context : comperment-scan> <!-뷰 파서-> <bean id = "viewResolver"> <property name = "value ="/web-inf/view/"> </property name> <property name ="suffix "value =".
(3) 데이터 소스, 서비스 자동 스캔 및 주입, 스프링 관리 MyBatissQlSessionFactory, DAO 계층 인터페이스 동적 프록시 및 트랜잭션 구성 ApplicationContext.xml
여기에는 여러 구성 파일이 있습니다
1) 단일 데이터 소스, 동적 프록시는 DAO 계층 인터페이스를 구현할 때 SQLSESSIONFACTORYBEANNAME 또는 SQLSESSIONTEMPLATEBEANNAME의 두 속성 값을 설정하지 않습니다.
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.w3.org/2001/xmlschema-instance" xmlns : tx = "http://www.springframework.org/schema/tx"xmlns : context = "http://www.springframework.org/schema/context"xsi : schemalocation = "http://www.spramwork.org/schema/spramwork http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/sprging-conxt-2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.xsd "> <!-로드 구성 JDBC 파일-> <context : property-placeholder location ="Classepperties "/>/>/>. id = "dataSource"> <property name = "driver className"> <value> $ {jdbc.driverclassname} </value> </value> <property name = "url"> <value> $ {jdbc.url} </value> </property> <property name = "username"</value> $} name = "password"> <value> $ {jdbc.password} </value> </property> </bean> <!-주석 구성, 즉 autowried-> <!-<context : annotation-config/>->-> <! BASE-PACKAGE = "com.website.service" /> <! /> <!-엔티티 클래스 맵 파일 경로, 그 중 하나만 끝에 기록되어 있으며, 여러 가지가 myBatis/*. XML-> <속성 이름 = "mapperlocations"value = "classPath : myBatis/usermapper.xml"/> </bean> <!-<sqlsession "> indelfor-arg ="> <ref indector-arg = "> <ref. bean = "sqlsessionfactory"/> </constructor-arg> </bean>-> <!-동적 프록시 구현은 dao를 쓸 필요가 없습니다-> <bean id = "mapperscannerconfigurer"> <!-여기에서 dao 계층 인터페이스는 지정되며, 여기에서 dao 인터페이스는 그 자체로 구현 될 필요는 없습니다. value = "com.website.dao" /> <!-데이터 소스가 하나만 있으면 지정할 수 없지만 여러 데이터 소스가 있으면 지정해야합니다.> <!-<속성 이름 = "SQLSESSIONFACTORYBEANNAME"value = "SqlSessionActory" />-> <!-SQLSESSIONTMETEP PLATET를 직접 Sceciftion합니다. 이름 = "sqlsessiontemplatebeanname"value = "sqlsession" />-> < /bean> <!-트랜잭션 관리자-> <bean id = "transactionManager"> <property name = "dataSource"ref = "dataSource" /< /bean> <!-완전 주석 거래-> <tx : annotation-driven-manager "> < /bean> < /bean> <! < /bean> <!2) 단일 데이터 소스에 대한 SQLSESSIONFACTORYBEANNAME의 속성 값 구성
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.w3.org/2001/xmlschema-instance" xmlns : tx = "http://www.springframework.org/schema/tx"xmlns : context = "http://www.springframework.org/schema/context"xsi : schemalocation = "http://www.spramwork.org/schema/spramwork http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/sprging-conxt-2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.xsd "> <!-로드 구성 JDBC 파일-> <context : property-placeholder location ="Classepperties "/>/>/>. id = "dataSource"> <property name = "driver className"> <value> $ {jdbc.driverclassname} </value> </value> <property name = "url"> <value> $ {jdbc.url} </value> </property> <property name = "username"</value> $} name = "password"> <value> $ {jdbc.password} </value> </property> </bean> <!-주석 구성, 즉 autowried-> <!-<context : annotation-config/>->-> <! BASE-PACKAGE = "com.website.service" /> <! /> <!-엔티티 클래스 맵 파일 경로, 그 중 하나만 끝에 기록되어 있으며, 여러 가지가 myBatis/*. XML-> <속성 이름 = "mapperlocations"value = "classPath : myBatis/usermapper.xml"/> </bean> <!-<sqlsession "> indelfor-arg ="> <ref indector-arg = "> <ref. bean = "sqlsessionfactory"/> </constructor-arg> </bean>-> <!-동적 프록시 구현은 dao를 쓸 필요가 없습니다-> <bean id = "mapperscannerconfigurer"> <!-여기에서 dao 계층 인터페이스는 지정되며, 여기에서 dao 인터페이스는 그 자체로 구현 될 필요는 없습니다. value = "com.website.dao" /> <!-데이터 소스가 하나만 있으면 지정할 수 없지만 여러 데이터 소스가 있으면 지정해야합니다.> <속성 이름 = "SQLSESSIONFACTORYBEANNAME"value = "SqlSessionActory" /> <! 이름 = "sqlsessiontemplatebeanname"value = "sqlsession" />-> < /bean> <!-트랜잭션 관리자-> <bean id = "transactionManager"> <property name = "dataSource"ref = "dataSource" /< /bean> <!-완전 주석 거래-> <tx : annotation-driven-manager "> < /bean> < /bean> <! < /bean> <!3) 단일 데이터 소스의 SQLSESSIONTEMPLATEBEANNAME 속성 값 구성
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.w3.org/2001/xmlschema-instance" xmlns : tx = "http://www.springframework.org/schema/tx"xmlns : context = "http://www.springframework.org/schema/context"xsi : schemalocation = "http://www.spramwork.org/schema/spramwork http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/sprging-conxt-2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.xsd "> <!-로드 구성 JDBC 파일-> <context : property-placeholder location ="Classepperties "/>/>/>. id = "dataSource"> <property name = "driver className"> <value> $ {jdbc.driverclassname} </value> </value> <property name = "url"> <value> $ {jdbc.url} </value> </property> <property name = "username"</value> $} name = "password"> <value> $ {jdbc.password} </value> </property> </bean> <!-주석 구성, 즉 autowried-> <!-<context : annotation-config/>->-> <! BASE-PACKAGE = "com.website.service" /> <! /> <!-엔티티 클래스 맵 파일 경로, 그중 하나만 끝에 기록되어 있으며, 여러 가지가 myBatis /*. XML-> <속성 이름 = "mapperLocations"value = "classPath : myBatis /usermapper.xml" /> < /bean> <sqlsession "> <sqlsactor-index = "0"> < "sqlsActory"> < "0 < /bean> <sqlsession"> "0" "> < /bean> < /bean>. </constructor-arg> </bean> <!-동적 프록시 구현은 dao를 작성할 필요가 없습니다-> <bean id = "mapperscannerconfigurer"> <!-여기 DAO 계층 인터페이스 전력이 지정되고 여기에서 DAO 인터페이스는 그 자체로 구현할 필요가 없습니다-> value = "! 하나의 데이터 소스는이를 지정할 수 없지만 여러 데이터 소스가있는 경우-> <!-<속성 이름 = "sqlSessionFactoryBeanName"value = "sqlsessionFactory" />-> <!-sqlSessionTemplate 이름을 직접 공식화합니다. </bean> <!-트랜잭션 관리자-> <bean id = "transactionManager"> <property name = "dataSource"ref = "dataSource"/</bean> <!-완전 주석 트랜잭션을 사용하여-> <tx : 주석 중심 트랜잭션-매너 = "TransactionManager"/>4) 여러 데이터 소스
멀티 데이터 소스 인 경우 특정 데이터 소스를 지정하려면 SQLSESSIONFACTORYBEANNAME 또는 SQLSESSIONTEMPLATEBEANNAME를 사용해야합니다. 위의 구성에서 알 수 있는지 모르겠습니다. sqlsessiontemplatebeanname을 사용하는 경우해야합니다
<bean id = "sqlsession"> <생성자-arg index = "0"> <ref bean = "sqlsessionFactory"/> </constructor-arg> </bean>
특정 인스턴스를 생성하고 SQLSESSIONTEMPLATEBEANNAME 속성에 할당하려면
(4), MyBatis SQL 매핑 파일 usermapper.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"> <! 이 데모의 경우 네임 스페이스는 userdao.java의 전체 경로입니다.> <mapper 네임 스페이스 = "com.website.dao.userdao"> <!-여기에 ID는 인터페이스의 메소드 이름입니다-> <insert id = "saveuser"parametertype = "java.util.map"> user in user (id, name). 값 (#{id},#{name}) </insert> </mapper>확인 구성 파일로 가서 완료하겠습니다. 제어 계층, 비즈니스 로직 계층 및 DAO 계층 코드를 살펴 보겠습니다.
3. 컨트롤러 계층
패키지 com.website.controller; java.util.hashmap import; java.util.map import; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; org.springframework.beans.factory.annotation.autowired; org.springframework.stereotyp.controller import; org.springframework.web.bind.annotation.requestmapping; org.springframework.web.bind.annotation.requestmethod; import com.website.service.userservice; @Controller @RequestMapping (value = "/user") 공개 클래스 USERCONTROLLER {// userErvice 객체를 주입 @autowired private userervice userervice; @requestmapping (value = "/save.do", method = requestmethod.get) public string saveuser (httpservletrequest request, httpservletreponse responsk) {string id = request.getParameter ( "id"); 문자열 이름 = request.getParameter ( "name"); map <string, string> map = new Hashmap <String, String> (); map.put ( "id", id); map.put ( "이름", 이름); uservice.saveuser (지도); "색인"을 반환합니다. }}4. 서비스 계층
패키지 com.website.service; java.util.map import; org.springframework.beans.factory.annotation.autowired; org.springframework.stereotyp.service; org.springframework.transaction.annotation.transactional import; import com.website.dao.userdao; @Service ( "userVice") @transactional public class userervice {// 주입 DAO 인터페이스 구현 클래스 인스턴스 // 두 주입 방법 모두 @autowized private userdao userdao 일 수 있습니다. public void saveuser (map <string, string> map) {int end = userdao.saveuser (map); System.out.println ( "end :" + end); }}5. DAO 레이어 인터페이스
패키지 com.website.dao; java.util.map import; //com.website.dao.userdao public interface userdao {int saveuser (map <string, string> map); } DAO 인터페이스의 전체 경로는이 DAO 인터페이스에 해당하는 매핑 파일의 네임 스페이스이며 메소드 이름은 ID의 값입니다.
자,이 구성 메소드는 끝났으며 완전한 작은 데모도 있습니다. 아래를 간단히 요약하겠습니다.
이전 구성 방법과 비교하여 (아래에 작성 될) DAO 레이어 인터페이스의 동적 프록시 방법을 사용한다는 것입니다. 전에, 우리는 DAO 레이어에서 DAO 레이어를 수동으로 구현 한 다음 SQLSESSESSETEMPLATE 인스턴스를 자동으로 주입하여 insert ( "", "") SELECTONE ( "", "") 및 기타 방법과 같은 특정 메소드를 호출합니다. 첫 번째 매개 변수는 매핑 파일의 주소 : 네임 스페이스+ID이고 두 번째 매개 변수는 전달 된 조건입니다. 이러한 방식으로 MyBatis는 구문 분석 및 쿼리를 위해 전달한 두 매개 변수에 따라 특정 매핑 파일을 찾을 것입니다. 여기에서 동적 프록시를 사용하면 DAO 인터페이스를 구현하는 단계가 제거되지만 Spring으로 구현했습니다. 문제가 있습니다. 쿼리 조건 매개 변수를 전달했지만 매핑 파일의 특정 경로는 다음과 같습니다. 통과되지 않으면 어떻게해야합니까? 즉, 매핑 파일의 네임 스페이스는 인터페이스 클래스의 전체 이름이어야하며 ID는 인터페이스의 메소드 이름이어야하므로 동적 프록시가 경로를 찾고 매개 변수를 갖도록해야합니다. 당신은 그것이 똑같이 생각합니까? 하하하!
2. DAO 계층 인터페이스를 수동으로 구현합니다
먼저 DAO 계층을 수동으로 구현하기위한 구성 및 코드를 살펴 보겠습니다.
1. 주제의 구조 다이어그램
2. 3 개의 구성 파일 및 매핑 파일
(1) 프로그램 입력, 프론트 엔드 컨트롤러 구성 Web.xml
<? xml version = "1.0"encoding = "utf-8"?> <web-app xmlns : xsi = "http://www.w3.org/2001/xmlschema-instance"xmlns = "http://java.sun.com/xml/ns/javaee" xsi : schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_3_0.xsd"3. ""webapp_ "" " <display-name> weffore2 </display-name> <!-로드 스프링 컨테이너 구성-> <liseer> <layer-class> org.spramework.web.context.contextloaderListener </laiseer-class> </liatser> <! <context-param> <param-name> conte-name> <Param-value> classpath : config/springmvc-servlet.xml, classpath : config/applicationcontext.xml </param-value> </context-param> <!-문자 인코딩 필터-> <filter> <filter-name> encodingfilter </filter-name> <filter-class> org.springframework.web.filter.characterencodingfilter </filter-class> <init-param> incoding </param-name> <param-value> utf-8 </param-value> <init-param> init-param> <param-name> force-name> <Param-value> true </param-value> </init-param> </filter> <filter-mapping> <filter-name> encodingfilter </filter-name> <Url-pattern>*. <Servlet-Class> org.springframework.web.servlet.dispatcherServlet </servlet-class> <init-param> <param-name> contextConfigLocation </param-name> <param-value> classpath : config/springmvc-servlet.xml </init-param> <! </load-on-startup> </load-on-startup> </servlet> <servlet-mapping> <servlet-name> springmvc </servlet-name> <!-인터셉트 요청-> <url-pattern>*. do </url-pattern> </servlet-mapping> <welcome-list> <welcome-file> index> html> html> html>. <colling-file> index.jsp </welcome-file> <welcome-file> default.html </welcome-file> default.htm </welcome-file> <welcome-file> default.jsp </welcome-file> </welcle-file-list> </web-app>
(2) 스캔 제어 레이어, 자동 분사 및 뷰 파서 구성 SpringMVC-Servlet.xml
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.w3.org/2001/xmlschema-instance" xmlns : aop = "http://www.springframework.org/schema/aop"xmlns : context = "http://www.springframework.org/schema/context"xmlns : mvc = "http://www.spramframwerk.org/schema/gramegr.org/gramegrogr.org/ xmlns : util = "http://www.springframework.org/schema/util"xmlns : tx = "http://www.springframework.org/schema/tx"xmlns : jdbc = "http://www.spramframwork.org/schema/gramework.org/schema/spramwork.org. XMLNS : "http://www.springframework.org/schema/cache"xsi : schemalocation = "http://www.springframework.org/schema/beans http://ww.springframework.org/schema/spred-beans- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.spramework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/sprring-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.spramework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd "> <!-주석 드라이버-> <mvc : 주석-구동/> <!-<context : annotation-config/>-> <!-scan-> context : component-scan BASE-PACKAGE = "com.website.controller"> </context : component-scan> <!-view parser-> <bean id = "viewResolver"> <property name = "prefix"value = "/web-inf/vive/"> </property> <속성 이름 = "suffix"value = ". jsp"> </bean>
(3) 데이터 소스, 서비스 자동 스캔 및 주입, 스프링 관리 MyBatissQlSessionFactory 및 Transaction Configuration ApplicationContext.xml
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.w3.org/2001/xmlschema-instance" xmlns : tx = "http://www.springframework.org/schema/tx"xmlns : context = "http://www.springframework.org/schema/context"xsi : schemalocation = "http://www.spramwork.org/schema/spramwork http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/sprging-conxt-2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.xsd "> <!-로드 구성 JDBC 파일-> <context : property-placeholder location ="Classepperties "/>/>/>. id = "dataSource"> <property name = "driver className"> <value> $ {jdbc.driverclassname} </value> </value> <property name = "url"> <value> $ {jdbc.url} </value> </property> <property name = "username"</value> $} 이름 = "password"> <value> $ {jdbc.password} </value> </propertion> </bean> <!-주석 구성을 켜십시오. 즉, Autowried-> <!-구성 요소 스캔은 주석-컨디의 기능을 가지고 있습니다. 사용하기 전에 콩을 스캔하는 것이-> <context : component-scan base-package = "com.website.service, com.website.dao" /> <!-mybatis를 사용할 때 sqlsessionfactorybean을 사용하여 sqlsession의 sqlsession factory --> <!을 사용하여 SQLSESSION을 사용하는 방법을 사용합니다. 몇 가지 방법을 제공합니다-> <bean id = "sqlsessionfactory"> <property name = "dataSource"ref = "dataSource" /> <!-myBatis 구성 파일 경로-> <property name = "configlocation"value = "" /> <!-Entity Class MAP 파일이 있어야합니다. value = "classpath : mybatis/usermping.xml"/> </bean> <!-사실,이 클래스의 예는 mybatis의 sqlsession입니다-> <bean id = "sqlsession"> <constructor-arg index = "0"> <ref bean = "sqlsessionsory"/> </bean> <no bean> </bean> name = "dataSource"ref = "dataSource" /> < /bean> <!-전체 주석 거래-> <TX : 주석 중심 트랜잭션-매너 = "TransactionManager" /> < /beans>(4) mybatis 매핑 파일
<? xml version = "1.0"alcoding = "utf-8"?> <! doctype mapper public "-// mybatis.org//dtd mapper 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">-아래의 ID는 완전한 경로입니다. DAO 계층에 완전한 경로를 작성한 후 MyBatis는이 파일에서 관련 SQL 문의 매핑입니다 .-> <Mapper Nesmpace = "com.website.usermapper"> <!-ParameterType는 허용하는 매개 변수의 유형입니다 .-> <!-> <Insert ID = "InsertUser"ParameterType = "Java.util.map" user (id, name, password) 값 (#{id},#{name},#{password}) </insert> </mapper>여기서 매핑 파일의 네임 스페이스 +ID가 DAO 레이어 인터페이스의 전체 클래스 이름 +ID 대신 사용자 정의되어 있음을 알 수 있습니다.
3. 컨트롤러
패키지 com.website.controller; java.util.hashmap import; java.util.map import; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; org.springframework.beans.factory.annotation.autowired; org.springframework.stereotyp.controller import; org.springframework.web.bind.annotation.requestmapping; import com.website.service.userservice; /*** @author whd data 2016 년 6 월 5 일*/@controller @RequestMapping (value = "/user") 공개 클래스 USERCONTROLLER {@autowired private usererVice userErvice; @requestmapping (value = "/save.do") public String saveuser (httpservletrequest request, httpservletrepsonge 응답) {string id = request.getParameter ( "id"); 문자열 이름 = request.getParameter ( "name"); 문자열 비밀번호 = request.getParameter ( "password"); map <string, string> map = new Hashmap <String, String> (); map.put ( "id", id); map.put ( "이름", 이름); map.put ( "비밀번호", 비밀번호); uservice.saveuser (지도); "색인"을 반환합니다. }}4. 비즈니스 로직 계층 서비스
패키지 com.website.service; java.util.map import; org.springframework.beans.factory.annotation.autowired; org.springframework.stereotyp.service; org.springframework.transaction.annotation.transactional import; import com.website.dao.userdao; / *** @author whd 데이터 2016 년 6 월 5 일*/ @service ( "userervice") @transactional public class userservice {@autowired private userdao userdao; public void saveuser (map <string, string> map) {userdao.saveuser (map); }}5. dao 층
패키지 com.website.dao; java.util.map import; import org.mybatis.spring.sqlsessiontemplate; org.springframework.beans.factory.annotation.autowired; org.springframework.stereotype.repository import; / *** @author whd 데이터 2016 년 6 월 5 일*/ @repository ( "userDao") 공개 클래스 userDao {@autowired private sqlsessiontemplate sqlsession; public void saveUser (map <string, string> map) {int end = sqlsession.insert ( "com.website.usermapper.insertuser", map); System.out.println ( "end" + end); }}역전 된 DAO 층의 sqlsessionTemplate을 살펴 봅니다. 이것은 실제로 mybatis의 sqlsession 객체입니다. ApplicationContext.xml에서 구성되어 있음을 알 수 있으므로 사용하면 Spring이 자동으로 주입됩니다. 우리는 직접 만들지 않고 직접 사용할 수 있습니다. 이것은 소위 제어 역전입니다. OK이 두 파일의 구성 방법은 끝났습니다.
요약
위의 것은 스프링 및 mybatis의 구성 방법에 대한 자세한 설명입니다 (일반적으로 사용되는 두 가지 방법). 나는 그것이 당신에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!