Integration mit Spring3
Als grundlegender Framework kann der Frühling Back-End-Frameworks wie Hibernate, MyBatis usw. integrieren.
Der vorherige Artikel führt die Verwendung von MyBatis allein vor, und die allgemeine Logik lautet:
SQLSessionFactory <- Konfigurationsdatei (einschließlich Datenbankverbindungskonfiguration)
Ixxxmapper <- sqlSession <- sqlSessionFactory
<- Mapper-Schnittstelle <- Mapper XML
Nachdem Sie IxxMapper erhalten haben, können Sie seine Methode für die Dateninteraktion aufrufen.
Bei der Integration in die Feder müssen die oben genannten Objekte als Bohnen verwaltet werden:
DataSource Bean <- Datenbankverbindungskonfiguration
SQLSessionFactory Bean <- DataSource
<- Konfigurationsdatei
Usermapper Bean <- sqlSessionFakktor
<- Mapper-Schnittstelle
1. Fügen Sie Abhängigkeiten in pom.xml hinzu:
<Properties> <mybatis.spring.version> 1.2.1 </mybatis.spring.version> <dbcp.version> 1.4 </dbcp.version> <spring.version> 3.1.2.Release </spring.version> </properties> <abhängigkeit> <abhängigkeit> <! <gruppeID> org.mybatis </GroupId> <artifactId> myBatis-pring </artifactId> <version> $ {mybatis.spring.version} </Version> </abhängig> <depeopcy> <!-Spring Context verwendet Spring-Jdbc, um sich mit der Datenbank zu verbinden-> <gruppe org. <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency><!-- dataSource is an instance of BasicDataSource --> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>${dbcp.version}</version> </abhängig> <abhängigkeit> <gruppe> org.springFramework </Groupid> <artifactId> Spring-Test </artifactId> <version> $ {Spring.version} </Version> </abhängig> </abhängig 2. Erstellen Sie die Beans-da.xml-Datei unter dem Klassenpfad:
<? XSI: Schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = "dataSource"> <!-Database Connection Bean-> <asy name = "dataSource"> <!-"-Database Connection Bean-< value = "com.mysql.jdbc.driver" /> <Eigenschaft name = "url" value = "jdbc: mysql: // localhost: 3306 /hbatis? CharakterCodierung = utf8" /> <Eigenschaft Name = "Benutzername" Value = "Root" /> <property = "Passwort" value = "123456" /< /> <boans "-Kennbuch". id = "sqlSessionFactory"> <!- name = "sqlSessionFactory" ref = "
3. Testunterricht:
@ContextConfiguration (Standorte = {"ClassPath: beans-da.xml"}) öffentliche Klasse SpringInteGeTest erweitert AbstractTestNgspringContextTests {private statische endgültige Logger log = loggerfactory.getLogger (SpringIntegrationTest.clas); @Resource iUSerMapper Mapper; @Test public void queryTest () {user user = mappper.getUserById (1); log.info ("Name: {}, Adresse: {}", user.getName (), user.getAddress ()); }} Integration mit SpringMVC
Hier bauen wir auf der Integration mit Spring3 auf:
1. Fügen Sie SpringMVC- und Freemarker -Abhängigkeiten zu pom.xml hinzu:
<properties> <freemarker.version>2.3.19</freemarker.version> <servlet.version>2.5</servlet.version> </properties> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>${freemarker.version}</version> </dependency> <Depopenty> <gruppe> javax.servlet </Groupid> <artifactId> servlet-api </artifactId> <version> $ {Servlet.version} </Version> <scope> bereitgestellt </scope> </abhängig>2. Fügen Sie Spring's Listener und SpringMVCs Servlet in web.xml hinzu:
<Hörer> <Hörer-Klasse> org.springframework.web.context.contextloaderListener </Listener-Klasse> <!-Hören Sie sich Containerereignisse an, initialisieren und schließen Sie den Kontext von Webanwendungen und rufen Sie ContextcleanUplistener an, um Ressourcen zu reinigen-> </Hörer> <Hörer> <Hörer> <Hörer> <Hörer> <Hörer> <Hörer> <Hörer-Klasse> org.springframework.web.context.contextCleanUplistener </Listener-Klasse> <!-Die destrukturierbaren Federressourcen in servletContext aufräumen, wenn die Webanwendung geschlossen ist-> </Hörer> <servlet> <servlet-name> Hbatis </servlet-name> <Servlet-Class> org.springFramework.web.servlet.DispatcherServlet </Servlet-Class> <!-<init-param> <param-name> contextconFigLocation </param-name> <PARAM-VALUE>/WEB-INF/HBATISISIVLET.xml </param-value> </init-param> </param-value> </init-param> </param-value> </iNit-param> </param-value> </</init-param> </</param-value> </</init-param> </param-value- Suchen Sie nach $ {project.name} -servlet.xml im Web-inf-Verzeichnis-> <load-on-startup> 1 </load-on-Startup> </servlet> <Servlet-Mapping> <Servlet-name> hbatis </servlet-name> <url-patter>*.3. Erstellen Sie eine neue unter Web-Inf:
Spring -Konfigurationsdatei applicationContext.xml:
<? xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns: context = "http://wwww.springframework.org/schema/context" xsi: schemalocation http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/sping/sping/sping/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling.-Pring/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING: location="classpath:/database.properties" /><!-- Database Configuration File--> <bean id="dataSource" p:driverClassName="${driverClassName}" p:url="${url}" p:username="${user_name}" p:password="${password}" /><!-- Data source configuration --> <bean id = "sqlSessionFactory"> <!- value = "classPath*: com/John/hbatis/modell/*. xml"/>-> <! value = "com.john.hbatis.mapper"/> </bean> </beans>Datenbank.Properties unter dem Klassenpfad:
DriverClassName = com.mysql.jdbc.driver url = jdbc: mysql: // localhost: 3306/mybatis? CharakterCodierung = utf8 user_name = root password = 123456
HINWEIS: Da MapperscannerConfiger den Benutzernamen zum Abrufen des Benutzerkontos bewirken kann, wodurch die Datenbankverbindung fehlschlägt, wird er in einen anderen Wert geändert: user_name.
SpringMVC-Konfigurationsdatei hbatis-servlet.xml:
<? xmlns: context = "http://www.springframework.org/schema/context" xmlns: mvc = "http://www.springframework.org/schema/mvc" xsi: schemalocation http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/sping/sping/sping/sping/sping/sping/sping/sping/sping/sping/sping/sping.-contexte.xsd http://www.springframework.org/schema/mvc http://www.springFramework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <mvc: Annotation /- /-mvc.xsd"; RequestMappingHandlermaping, RequestMappingHandlerAdapter und ExceptionHandRerexceptionResolver, um Annotationen wie @RequestMapping, @ExceptionHandler usw. zu unterstützen-> <Context: Komponenten-scan-Basis-Package = "Com.john.hbatis.Controller-Infoction- und Abhängigkeit. Freemarker View Processor-> <bean id = "viewResolverftl"> <Eigenschaft name = "value" value = "org.springFramework.Web.Servlet.View.Freemarker.Freemarkerview. <Eigenschaft name = "cache" value = "false"/> <Eigenschaft name = "viewNames"> <array> <wert>*. id = "freemarkerconfig"> <Eigenschaft name = "templateloaderPaths"> <list> <wert>/webinf/ftl/</value> <!-Vorlage Ladepfad-> </list> </property> </bean> </beans>
4. MVC:
Steuerungsschicht: UserController.java
@Controller @RequestMapping ("/article") Public Class UserController {@autowired iUSerMapper Mapper; @RequestMapping ("/list") public String showAll (modelMap modelMap) {list <Aktion> articles = mappper.getArticlesByUserid (1); modelMap.addattribute ("Artikel", Artikel); zurück "main.ftl"; }}Schicht anzeigen: main.ftl:
<#list Artikel als Artikel> <div> $ {article.id}. $ {article.title}: $ {artik.Content} </div> </#list>5. Starten Sie das Projekt und geben Sie den Browser ein: http: // localhost: 8080/hbatis/article/list.htm, um die Ergebnisse anzuzeigen.