Saya merangkum tiga metode dalam artikel sebelumnya, tetapi ada dua metode yang membuat anotasi SQL. Metode ini cukup membingungkan, jadi semua orang tidak banyak menggunakannya. Mari kita ringkas dua metode yang umum digunakan:
1. Kelas Implementasi Proksi Dinamis Tanpa Menulis DAO
Metode ini relatif sederhana. Anda tidak perlu menerapkan lapisan DAO, Anda hanya perlu mendefinisikan antarmuka. Ini dia hanya untuk merekam file konfigurasi, jadi programnya sangat sederhana:
1. Diagram struktur keseluruhan:
2. Tiga file konfigurasi dan satu file pemetaan
(1) Pintu masuk program dan konfigurasi pengontrol front-end 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/xmlns =" http://java.com/xmlns = "http://java.com/xmlns = XSI: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/we-pappe." "" "" "" "" "" "" "" "" "" <play-name> situs web1 </play- <param-name> ContextConfigLocation </param-name> <param-value> classPath: config/springmvc-servlet.xml, classpath: config/applicationcontext.xml </param-value> </context-param> <! <nilter-class> org.springframework.web.filter.characterencodingfilter </filter-class> <Ilin-param> <param-name> encoding </param-name> <param-value> UTF-8 </param-value> </init-param> <init-in-param> <naram-n-n-n-n-nalue> </init-param> <init-param> <nit-naram> <param-n-n-n-n-nalue> </init-param> <init-param> <nit-naram> <param-n-n-n-n-nalue> </init-param> <init-param> <nit-naram> <param-n-n-n-n-nalue> <param-value> true </param-value> </init-param> </tilter> <nilter-mapping> <nilter-name> encodingfilter </filter-name> <ratl-pola>*. do </rull-pattern> </filter-mapping> <! <servlet-class> org.springframework.web.servlet.dispatcherServlet </servlet-class> <Ilin-param> <param-name> ContextConfigLocation </param-name> <param-value> CLASSPATH: config/springmvc-servlet.xml </param-value> </init-parpath/springmvc-servlet.xml </param-value> </init-param> ini </init-param>-loadamer>-ini configurure>-this contin-pared <! <Load-on-startup> 1 </boid-on-startup> </servlet> <servlet-Mapping> <servlet-Mapping> <servlet-name> springmvc </servlet-name> <!-permintaan intersep-> <road-pattern>*. do </url-pattern> </servlet-mapping> <creading-filter> </Url-Pattern> </Servlet-Mapping> <creading-file-list> <creend Welcome-File> index.htm </celcome-file> <creendak-file> index.jsp </celcome-file> <creendak-file> default.html </celcome-file> <creendak-file> default.htm </creendak-file> <creendak-file> default.jsp </celcome-file> </celcome-file-pile> <//webspile </welcome-file> </welcome-file> </web-pappe-pile
(2) Lapisan kontrol pemindaian, injeksi otomatis dan tampilan konfigurasi parser springmvc-servlet.xml
<? XML Versi = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmls xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: context = "http://www.springframework.org/schema/context" xmlns: mvc = "http:/schema/context" xmlns: mvc = "http:/schema" xmlns: util = "http://www.springframework.org/schema/util" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: jdbc = "http:gfor" xmlns: cache = "http://www.springframework.org/schema/cache" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jdb http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://wwww.spramework.org/schae/cache http://www.spramework.org/cache/cache/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd "> <!-driver anotasi-> <mvc: annotation-driven/> <!-<konteks: annotation-config/>->-Konteks: Komponen-scan memiliki fungsi The Annotation-Config/>-> Context: Component-scan memiliki fungsi The Annotation-Config/>-> Context: Component-Scan memiliki fungsi The Annotation-Config/>-> Context: Component-Scan memiliki fungsi The Annotation-Config/>-> Context: Component-Scan memiliki fungsi The Annotation-Config/> <! <konteks: komponen-scan-package = "com.website.controller"> </konteks: component-scan> <!-view parser-> <bean id = "viewResolver"> <name properti = "prefix" value = "/web-inf/view/"> </properti> <Property name = "suffix" value = ". Jsp".
(3) Sumber data, pemindaian dan injeksi otomatis layanan, pegas dikelola mybatissqlsessionfactory, proxy dinamis antarmuka lapisan dao, dan konfigurasi transaksi applicationContext.xml
Akan ada beberapa file konfigurasi di sini
1) Sumber data tunggal, proxy dinamis tidak menetapkan nilai dari dua sifat SQLSessionFactoryBeanName atau SQLSessionTemplateBeanName saat mengimplementasikan antarmuka lapisan DAO.
<? XML Versi = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmls xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemalocation = "http:/schema http://www.springframework.org/schema/beans/spring-weans-3.2.xsd http://www.springframework.org/schema/context http:/www.springframework.org/schema/context http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-load configuration JDBC-Konteks: Konteks: Lokasi Properti-Place-Place =" Classpath: ClassPath: ClassPath: DATEK "DATER"> <Context: Property-Place-Place ID = "DataSource"> <name properti = "driverclassName"> <value> $ {jdbc.driverclassName} </value> </properte> <Properti nama = "url"> <value> $ {jdbc.url} </value> </properties> <value name = "username"> <value> $ {JDBOCE. name="password"> <value>${jdbc.password}</value> </property> </bean> <!-- Turn on the annotation configuration, namely Autowried --> <!-- <context:annotation-config/> --> <!-- In fact, component-scan has the function of annotation-config, that is, register the required classes in the spring container --> <context:component-scan BASE-PACKAGE = "com.website.service" /> <!-Saat menggunakan mybatis, Spring menggunakan sqlSessionFactoryBean untuk mengelola mybatis 'sqlsessionfactory-> <bean id = "sqlSessionFactory"> <Properti nama = "dataSource" ref = "DatasSource" /<! value = ""/> <!-Path file peta kelas entitas, hanya satu dari mereka yang ditulis sampai akhir, dan beberapa yang dapat diganti dengan mybatis/*. xml-> <name properti = "mapperlocations" value = "classpath: mybatis/usermapper.xml"/> </bean> <!-<bean id = "SQ =" "SQ =" "" "" "" "" "" "" "" "" "" "" "" USERMAPPER.XML "/> </bean> <!-<bean id" SQ = "" SQ = "" "" "" "" "" "" SQOR "SUXICE" SUPREY/<!-<! bean = "sqlSessionFactory"/> </constructor-Arg> </bean>-> <!-Implementasi proxy dinamis tidak perlu menulis dao-> <bean id = "mapperscanner configurer"> <!-di sini Basepackage. value = "com.website.dao" /> <!-Jika hanya ada satu sumber data, Anda tidak dapat menentukannya, tetapi jika ada beberapa sumber data, Anda harus menentukannya-> <!-<nama properti = "SQLSessionFactoryBeanBeanTe-Nilai yang sama-SQULE SQLATEDEPLATE" /SQLSESSESTORTORY-ATIFITE-ATIFITE-SQULE, SEQUTIF THE SQLATE, SQLSESSESTORCORY " /-> <!-ATIFTIFET-LIRTAL-NILAI-SQLATE, ICLATE, MISTICEDEDEPLATE" <nama properti = "sqlSessionTemplateBeanName" value = "sqlsession" />-> </ bean> <!-transaction manager-> <bean id = "transactionManager"> <nama properti = "DataSource" Ref = "DataSource" /< /bean> <! </boy>2) Mengkonfigurasi nilai properti SQLSessionFactoryBeanName untuk satu sumber data
<? XML Versi = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmls xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemalocation = "http:/schema http://www.springframework.org/schema/beans/spring-weans-3.2.xsd http://www.springframework.org/schema/context http:/www.springframework.org/schema/context http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-load configuration JDBC-Konteks: Konteks: Lokasi Properti-Place-Place =" Classpath: ClassPath: ClassPath: DATEK "DATER"> <Context: Property-Place-Place ID = "DataSource"> <name properti = "driverclassName"> <value> $ {jdbc.driverclassName} </value> </properte> <Properti nama = "url"> <value> $ {jdbc.url} </value> </properties> <value name = "username"> <value> $ {JDBOCE. name="password"> <value>${jdbc.password}</value> </property> </bean> <!-- Turn on the annotation configuration, namely Autowried --> <!-- <context:annotation-config/> --> <!-- In fact, component-scan has the function of annotation-config, that is, register the required classes in the spring container --> <context:component-scan BASE-PACKAGE = "com.website.service" /> <!-Saat menggunakan mybatis, Spring menggunakan sqlSessionFactoryBean untuk mengelola mybatis 'sqlsessionfactory-> <bean id = "sqlSessionFactory"> <Properti nama = "dataSource" ref = "DatasSource" /<! value = ""/> <!-Path file peta kelas entitas, hanya satu dari mereka yang ditulis sampai akhir, dan beberapa yang dapat diganti dengan mybatis/*. xml-> <name properti = "mapperlocations" value = "classpath: mybatis/usermapper.xml"/> </bean> <!-<bean id = "SQ =" "SQ =" "" "" "" "" "" "" "" "" "" "" "" USERMAPPER.XML "/> </bean> <!-<bean id" SQ = "" SQ = "" "" "" "" "" "" SQOR "SUXICE" SUPREY/<!-<! bean = "sqlSessionFactory"/> </constructor-Arg> </bean>-> <!-Implementasi proxy dinamis tidak perlu menulis dao-> <bean id = "mapperscanner configurer"> <!-di sini Basepackage. value = "com.website.dao" /> <!-Jika hanya ada satu sumber data, Anda tidak dapat menentukannya, tetapi jika ada beberapa sumber data, Anda harus menentukannya-> <nama properti = "SQLSessionFactoryBeanName" Nilai SQLSECESTORY, SEBUTUH SQLSEFACTORY, SEBUTUH SQLSECESTORY, SEBUTUH SQLSECESTORY, SEBUTU SQLSETICE, SEBUTU SQLSETICE, AKUTICE, SEBUTUH SQLSECESTORY, SEBUTUH SQLSECESTORY LOUDSETICTICORY, name = "sqlSessionTemplateBeanName" value = "sqlsession" />-> </ bean> <!-Transaction Manager-> <bean id = "transactionManager"> <name properti = "DataSource" Ref = "DataSource" /< /bean> <!-Menggunakan annotasi penuh "transaksi-" transaksi "transaksi" transaction " /transaction" transaction-"transaction" /transaction "transaction-" transaksi " </boy>3) Mengkonfigurasi nilai properti SQLSessionTemplateBeanName untuk satu sumber data
<? XML Versi = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmls xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemalocation = "http:/schema http://www.springframework.org/schema/beans/spring-weans-3.2.xsd http://www.springframework.org/schema/context http:/www.springframework.org/schema/context http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-load configuration JDBC-Konteks: Konteks: Lokasi Properti-Place-Place =" Classpath: ClassPath: ClassPath: DATEK "DATER"> <Context: Property-Place-Place ID = "DataSource"> <name properti = "driverclassName"> <value> $ {jdbc.driverclassName} </value> </properte> <Properti nama = "url"> <value> $ {jdbc.url} </value> </properties> <value name = "username"> <value> $ {JDBOCE. name="password"> <value>${jdbc.password}</value> </property> </bean> <!-- Turn on the annotation configuration, namely Autowried --> <!-- <context:annotation-config/> --> <!-- In fact, component-scan has the function of annotation-config, that is, register the required classes in the spring container --> <context:component-scan BASE-PACKAGE = "com.website.service" /> <!-Saat menggunakan mybatis, Spring menggunakan sqlSessionFactoryBean untuk mengelola mybatis 'sqlsessionfactory-> <bean id = "sqlSessionFactory"> <Properti nama = "dataSource" ref = "DatasSource" /<! value = ""/> <!-Path file peta kelas entitas, hanya satu dari mereka yang ditulis sampai akhir, dan beberapa yang dapat diganti oleh mybatis/*. xml-> <name properti = "mapperlocations" value = "classpath: mybatis/usermapper.xml"/</bean> <bean id = "sq =" SQ = "SQ =" SQ = "" SQ = "SQ =" SQ = "SQ =" SQ = "SQ =" SQ = "SQ =" SQ = "SQ =" SQ = "SQ =" SQ = "SQ =" SQ = "SQ =" SQ = "SQ =" SQ = "SQ =" SQ = "SQ =" SQ = "SQ =" SQ = "SQ =" SQ = SQ = SQ = SQ = SQ = SQ = SQ = SQ = " bean="sqlSessionFactory" /> </constructor-arg> </bean> <!-- Dynamic proxy implementation does not require writing dao --> <bean id="MapperScannerConfigurer"> <!-- BasePackage here The dao layer interface power is specified, and the dao interface here does not need to be implemented by itself --> <property name="basePackage" value = "com.website.dao" /> <!-Jika hanya ada satu sumber data, Anda tidak dapat menentukannya, tetapi jika ada beberapa sumber data, Anda harus menentukannya-> <!-<nama properti = "SqlSessionFactoryBeanBean name yang sama, Nilai yang sama, SQLATE," SQLADE SQLATE, "SQLADETICE,> <!-NOVOLETITET" Nilai SQLATE, "SQLATE SQLASEPLATE" SQLSESSESTORY DITIFULASI "SQULATE SQULEDE,> <!-secara langsung-formulasikan sqle, name="sqlSessionTemplateBeanName" value="sqlSession" /> </bean> <!--Transaction Manager--> <bean id="transactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- Using full annotation transaction--> <tx:annotation-driven transaction-manager="transactionManager" /> </boy>4) Beberapa sumber data
Perhatikan bahwa jika itu adalah sumber multi-data, Anda harus menggunakan SQLSessionFactoryBeanName atau SQLSessionTemplateBeanName untuk menentukan sumber data tertentu. Saya tidak tahu apakah Anda menyadarinya dalam konfigurasi di atas. Jika Anda menggunakan SQLSessionTemplateBeanName, Anda harus
<bean id = "sqlsession"> <constructor-arg index = "0"> <ref bean = "sqlSessionFactory"/> </constructor-arg> </tac>
Untuk membuat contoh tertentu dan menetapkannya ke properti SQLSessionTemplateBeanName.
(4), file pemetaan 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" <!-value. Untuk demo ini, namespace adalah jalur lengkap userdao.java-> <mapper namespace = "com.website.dao.userdao"> <!-ID di sini adalah nama metode di antarmuka-> <insert id = "saveuser" parameTerType = "java.util.maap"> ke {parameTerpeRype = "java.util.maap"> ke {parameTerpeRype = "java.util.aba"> ke {parameterType = "java.util.maap" </sert> </mapper>Ok, mari kita pergi ke file konfigurasi dan menyelesaikannya. Mari kita lihat lapisan kontrol, lapisan logika bisnis, dan kode lapisan DAO.
3. Lapisan pengontrol
paket com.website.controller; impor java.util.hashmap; impor java.util.map; impor javax.servlet.http.httpservletRequest; impor javax.servlet.http.httpservletResponse; impor org.springframework.beans.factory.annotation.Autowired; impor org.springframework.stereotype.controller; impor org.springframework.web.bind.annotation.requestmapping; impor org.springframework.web.bind.annotation.requestmethod; impor com.website.service.userservice; @Controller @RequestMapping (value = "/user") kelas publik userController {// menyuntikkan objek Userservice @Autowired private UserService UserService; @RequestMapping (value = "/Save.do", Method = requestMethod.get) Public String SaveUser (permintaan httpservletRequest, respons httpservletResponse) {string id = request.getParameter ("id"); Name string = request.getParameter ("name"); Peta <string, string> peta = new HashMap <String, String> (); peta.put ("id", id); peta.put ("name", name); Userservice.SaveUser (MAP); mengembalikan "indeks"; }}4. Lapisan Layanan
paket com.website.service; impor java.util.map; impor org.springframework.beans.factory.annotation.Autowired; impor org.springframework.stereotype.service; impor org.springframework.transaction.annotation.transactional; impor com.website.dao.userdao; @Service ("Userservice") @Transactional Public Class UserserService {// Injeksi DAO Instementasi Kelas Implementasi Kelas Instance // Kedua metode injeksi dapat @Autowired private userdao userdao; public void saveUser (peta <string, string> peta) {int end = userdao.saveuser (peta); System.out.println ("end:" + end); }}5. Antarmuka Lapisan DAO
paket com.website.dao; impor java.util.map; //com.website.dao.userdao antarmuka publik userdao {int saveUser (peta <string, string> peta); } Jalur lengkap antarmuka DAO adalah namespace dari file pemetaan yang sesuai dengan antarmuka DAO ini, dan nama metode adalah nilai ID
OK, metode konfigurasi ini sudah berakhir, dan ada juga demo kecil yang lengkap. Mari kita ringkas secara singkat di bawah ini:
Dibandingkan dengan metode konfigurasi sebelumnya (yang akan ditulis di bawah ini) adalah menggunakan metode proxy dinamis dari antarmuka lapisan DAO. Sebelumnya, kami akan mengimplementasikan lapisan DAO secara manual di lapisan DAO dan kemudian secara otomatis menyuntikkan instance SQLSessionTemplate untuk memanggil metode spesifik seperti memasukkan ("", "") selecone ("", "") dan metode lainnya. Parameter pertama adalah alamat file pemetaan: namespace+id, dan parameter kedua adalah kondisi yang diteruskan. Dengan cara ini, MyBatis akan menemukan file pemetaan spesifik sesuai dengan dua parameter yang kami lewati untuk penguraian dan permintaan. Menggunakan proxy dinamis di sini menghilangkan langkah menerapkan antarmuka DAO, tetapi kami mengimplementasikannya pada musim semi. Ada masalah. Kami melewati parameter kondisi kueri, tetapi jalur spesifik dari file pemetaan adalah: namespce+id. Apa yang harus saya lakukan jika tidak lulus? Artinya, namespace dari file pemetaan Anda harus menjadi nama lengkap dari kelas antarmuka dan ID harus menjadi nama metode di antarmuka, sehingga proxy dinamis dapat menemukan jalur dan juga memiliki parameter. Apakah Anda pikir itu sama? Ha ha ha!
2. Implementasikan secara manual antarmuka lapisan DAO
Pertama mari kita lihat konfigurasi dan kode untuk mengimplementasikan lapisan DAO secara manual:
1. Diagram struktur topik
2. Tiga file konfigurasi dan file pemetaan
(1) Entri program, konfigurasi pengontrol front-end 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/xmlns =" http://java.com/xmlns = "http://java.com/xmlns = XSI: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/we-pappe." "" "" "" "" "" "" "" "" "" <play-name> Situs web2 </splay- ClassPath: config/springmvc-servlet.xml, classpath: config/applicationcontext.xml </param-value> </context-param> <!-filter pengkodean karakter-> <nilter> <nilter-name> encodingfilter </filter-name> <nilter-class> org.springframework.werfilter. <Ilin-Param> <Param-name> Encode </param-name> <param-value> UTF-8 </param-value> </init-param> <Ilin-param> <Ilin-param> <param-name> ForceEncoding </param-name> <param-value> True </param-value> </init-param> <param-value> </lue </param-value> </init-param> <param-value> <filter-name> encodingfilter </filter-name> <ritl-pola>*. do </rerl-pattern> </filterpapping> <!-controller front-end-> <servlet> <servlet-name> springmvc </servlet-name> <servlet-class> org.springframework.weB.servet.dasters> <servlet> </org.springframework.weB.servet. <Param-name> ContextConfigLocation </param-name> <param-value> classPath: config/springmvc-servlet.xml </param-value> </it-param> <!-File konfigurasi ini dimuat ketika wadah dimulai-> <doad-on-startup> 1 </load-on-startup> </servlet> SERVLET> <load-on-startup> 1 </load-on-startup> </Servlet <!-Permintaan Intercept-> <RURL-PATERS>*. Lakukan </RURL-PATERS> </SERVLET-Mapping> <dreades-file-file> <creendak-file> index.html </celcome-fils <creend Welcome-file> default.htm </celcome-file> <creendak-file> default.jsp </celcome-file> </celcome-file-list> </web-app>
(2) Lapisan kontrol pemindaian, injeksi otomatis dan tampilan konfigurasi parser springmvc-servlet.xml
<? XML Versi = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmls xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: context = "http://www.springframework.org/schema/context" xmlns: mvc = "http:/schema/context" xmlns: mvc = "http:/schema" xmlns: util = "http://www.springframework.org/schema/util" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: jdbc = "http:gfor" xmlns: cache = "http://www.springframework.org/schema/cache" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jdb http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://wwww.spramework.org/schae/cache http://www.spramework.org/cache/cache/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd "> <!-Driver anotasi-> <mvc: annotation-driven/> <!-<konteks: anotasi-config/>-> <!-scan-> <! Paket dasar = "com.website.controller"> </konteks: component-scan> <!-view parser-> <bean id = "viewResolver"> <name properti = "prefix" value = "/web-inf/view/"> </properti> <Properti nama = "Affix" value = ". Jsp"> </property> </properties> <Properti name = "Affix" value = ". Jsp"> </Property> </Property Name = "Suffix" value = ". Jsp"> </Property> </Property Name = "BEAANS =".
(3) Sumber data, pemindaian dan injeksi otomatis layanan, pegas dikelola mybatissqlsessionfactory dan konfigurasi transaksi applicationContext.xml
<? XML Versi = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmls xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemalocation = "http:/schema http://www.springframework.org/schema/beans/spring-weans-3.2.xsd http://www.springframework.org/schema/context http:/www.springframework.org/schema/context http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-load configuration JDBC-Konteks: Konteks: Lokasi Properti-Place-Place =" Classpath: ClassPath: ClassPath: DATEK "DATER"> <Context: Property-Place-Place ID = "DataSource"> <name properti = "driverclassName"> <value> $ {jdbc.driverclassName} </value> </properte> <Properti nama = "url"> <value> $ {jdbc.url} </value> </properties> <value name = "username"> <value> $ {JDBOCE. Name = "Password"> <value> $ {jdbc.password} </ value> </prop Property> </bean> <!-Nyalakan konfigurasi anotasi, yaitu Autowried-> <!-Component-scan memiliki fungsi anotasi,---CONFIG,---CON-CONFIG,---Konteksnya,---Konteks,--------Menyuntikkan Kelas yang Diperlukan ke dalam CONFENER SPRING-> <!-<! Untuk memindai kacang sebelum dapat digunakan-> <konteks: komponen-scan-package = "com.website.service, com.website.dao" /> <!-Saat menggunakan mybatis, spring menggunakan sqlsessionfactorybean untuk mengelola mybatis sqlsessionfactory-> <!-ketika menggunakan antarmuka. Ini menyediakan beberapa metode-> <bean id = "sqlSessionFactory"> <properti name = "DataSource" ref = "DataSource" /> <!-MyBatis Configuration File Path-> <nama properti = "configLocation" value = "" /> <!-Path file kelas entitas yang digunakan, sebaliknya. value = "classpath: mybatis/usermapping.xml"/> </ bean> <!-pada kenyataannya, contoh kelas ini adalah sqlsession di mybatis-> <bean id = "sqlsession"> <constructor-arg = "0"> <ref bean = "sqlsessionFactory"/</arger = </Bang = "Bean =" SQLSESSESSIONTORI "" <name properti = "DataSource" ref = "DataSource" /> </ bean> <!-Gunakan transaksi anotasi penuh-> <tx: transaksi-manager-manager yang digerakkan oleh anotasi = "transactionManager" /> </tac>(4) File Pemetaan MyBatis
<? 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">-ini napace. Setelah kami menulis jalur lengkap di lapisan DAO mybatis adalah pemetaan dari pernyataan SQL yang relevan dalam file ini-> <mapper namespace = "com.website.usermapper"> <!-parameterType adalah jenis parameter yang Anda terima,-> <!-tambahkan informasi pengguna-> <masukkan id id = "sisipus" paramerType = "paramerType =" parameryping = "paramerping =" paramerping = "paramerping =" JavePer "(ParamEtypeing =" JavePer "(ParamEtyping =" JavePer "(ParamEtyping =" JAVAP = "JAVAP =" PARAMETEPLEP = "PARAMETEPE (" ParamEtype. " values (#{id},#{name},#{password}) </insert> </mapper>Anda dapat melihat bahwa namespace +ID dari file pemetaan di sini disesuaikan alih -alih nama kelas penuh +ID dari antarmuka lapisan DAO.
3. Pengontrol
paket com.website.controller; impor java.util.hashmap; impor java.util.map; impor javax.servlet.http.httpservletRequest; impor javax.servlet.http.httpservletResponse; impor org.springframework.beans.factory.annotation.Autowired; impor org.springframework.stereotype.controller; impor org.springframework.web.bind.annotation.requestmapping; impor com.website.service.userservice; /*** @Author WHD Data 5 Juni 2016*/@Controller @RequestMapping (value = "/user") kelas publik usercontroller {@autowired private userserverServerService; @RequestMapping (value = "/Save.do") Public String SaveUser (permintaan httpservletRequest, respons httpservletResponse) {string id = request.getParameter ("id"); Name string = request.getParameter ("name"); String password = request.getParameter ("password"); Peta <string, string> peta = new HashMap <String, String> (); peta.put ("id", id); peta.put ("name", name); peta.put ("Kata sandi", kata sandi); Userservice.SaveUser (MAP); mengembalikan "indeks"; }}4. Layanan Lapisan Logika Bisnis
paket com.website.service; impor java.util.map; impor org.springframework.beans.factory.annotation.Autowired; impor org.springframework.stereotype.service; impor org.springframework.transaction.annotation.transactional; impor com.website.dao.userdao; / *** @Author WHD Data 5 Juni 2016*/ @Service ("Userservice") @transactional Public Class Userservice {@Autowired private userdao userdao; public void saveUser (peta <string, string> peta) {userdao.saveuser (peta); }}5. Lapisan Dao
paket com.website.dao; impor java.util.map; impor org.mybatis.spring.sqlsessionTemplate; impor org.springframework.beans.factory.annotation.Autowired; impor org.springframework.stereotype.repository; / *** @author whd data 5 Juni 2016*/ @repository ("userdao") kelas publik userdao {@autowired sqlSessionTemplate sqlsession sqlsession; public void saveUser (peta <string, string> peta) {int end = sqlSession.insert ("com.website.usermapper.insertuser", peta); System.out.println ("end" + end); }}Kita melihat sqlsessionTemplate dari lapisan DAO terbalik. Ini sebenarnya adalah objek SQLSession di Mybatis. Kami melihat bahwa itu dikonfigurasi di ApplicationContext.xml, jadi ketika kami menggunakannya, Spring akan secara otomatis menyuntikkannya. Kita dapat menggunakannya secara langsung tanpa membuatnya sendiri. Ini adalah inversi kontrol yang disebut. Ok metode konfigurasi kedua file ini sudah berakhir.
Meringkaskan
Di atas adalah penjelasan terperinci tentang metode konfigurasi pegas dan mybatis yang diperkenalkan kepada Anda (dua metode yang umum digunakan). Saya harap ini akan membantu Anda. Jika Anda memiliki pertanyaan, silakan tinggalkan saya pesan dan editor akan membalas Anda tepat waktu. Terima kasih banyak atas dukungan Anda ke situs web Wulin.com!