Integration with Spring3
Spring, as the basic framework, can integrate back-end frameworks such as Hibernate,mybatis.
The preceding is an introduction to using MyBatis alone, and the general logic is:
Sqlsessionfactory <-configuration file (including database connection configuration)
Ixxxmapper <-sqlsession <-sqlsessionfactory
<-Mapper Interface <-Mapper XML
Once you get the Ixxmapper, you can call its methods for data interaction.
When
and spring are integrated, these objects need to be managed as beans:
DataSource Bean <-Database connection configuration
Sqlsessionfactory Bean <-dataSource
&nb sp; <-configuration file
Usermapper bean <-sqlsessionfactory
& nbsp; <-Mapper Interface
1. Add Dependencies in Pom.xml:
<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> < Several objects of the dependencies> <dependency><!--MyBatis are packaged into spring bean--> <groupid>org.mybatis</ Groupid> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</ Version> </dependency> <dependency><!--The spring context connects the database with Spring-jdbc--> <GROUPID>ORG.SPRI Ngframework</groupid> <artifactId>spring-jdbc</artifactId> <version>${spring.version} </version> </dependency> <dependency><!--DataSource is an example of Basicdatasource--> <groupId> Commons-dbcp</groupid> <artifactId>commons-dbcp</artifactId> <version>${dbcp.version} </version> </dependency> <dependency> <groupiD>org.springframework</groupid> <artifactId>spring-test</artifactId> <version>${
Spring.version}</version> </dependency> </dependencies>
2. Create a Beans-da.xml file under the classpath:
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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.xsd "> <bean id=" DataSource "class=" Org.apache.commons.dbcp.BasicDataSource "><!--database connection bean--> <property name=" Driverclassname "value=" Com.mysql.jdbc.Driver "/> <property name=" url "value=" jdbc:mysql://localhost:3306/hbatis?characterencoding= UTF8 "/> <property name=" username "value=" root "/> <property name=" password "value=" 123456 "/> < /bean> <bean id= "sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" ><!-- Sqlsessionfactory Bean--> <property name= "DataSource" ref= "DataSource"/><!--data source--> <property N Ame= "Configlocation" value= "classpath:Configuration.xml"/><!--config file--> </bean> <bean id= "Usermapper" class= "Org.mybatis.spring.mapper.MapperFactoryBean" ><!--user mapping bean--&
Gt <property name= "Sqlsessionfactory" ref= "sqlsessionfactory"/> <property name= "MapperInterface"
Com.john.hbatis.mapper.IUserMapper "/><!--Mapping interface--> </bean> </beans>
3. Test class:
@ContextConfiguration (locations = {"Classpath:beans-da.xml"}) public
class Springintegrationtest extends abstracttestngspringcontexttests {
private static final Logger log = Loggerfactory.getlogger ( Springintegrationtest.class);
@Resource
iusermapper mapper;
@Test public
void Querytest () {
User user = Mapper.getuserbyid (1);
Log.info ("Name: {}, Address: {}", User.getname (), user.getaddress ());
}
Integration with SPRINGMVC
here we build on the basis of integration with SPRING3:
1. Add Springmvc and Freemarker dependencies to Pom.xml:
<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>
<dependency>
<groupid>javax.servlet</groupid >
<artifactId>servlet-api</artifactId>
<version>${servlet.version}</version >
<scope>provided</scope>
</dependency>
2. Add the spring Listener and Springmvc servlet to the Web.xml:
<listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class ><!--Monitor container events, initialize and close Web application contexts and invoke Contextcleanuplistener cleanup resources--> </listener> <listener> < listener-class>org.springframework.web.context.contextcleanuplistener</listener-class><!--Web application is closed To clean up ServletContext--> </listener> <servlet> <servlet-name>hbatis</in spring-related destruction resources
Servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--<init-param> <param-name>contextConfigLocation</param-name> <param-value>/web-inf /hbatis-servlet.xml</param-value> </init-param>--><!--is not configured, SPRINGMVC will go to the Web-inf directory to find ${ Project.name}-servlet.xml--> <load-on-startup>1</load-on-startup> </servlet> < Servlet-mapping> <servlet-name>hbatis</servlet-name> <url-pattern>*.htm</url-patTern> </servlet-mapping>
3. Under Web-inf NEW:
Spring configuration file Applicationcontext.xml:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:p= "http://www.springframework.org/schema/p" 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.xsd "> <context:property-placeholder location=" Classpath:/database.properties "/><!--database configuration file--> <bean id=" DataSource "class=" Org.apache.commons.dbcp.BasicDataSource "P:driverclassname=" ${driverclassname} "p:url=" ${url} "P:username=" ${user _name} "p:password=" ${password} "/><!--data Source configuration--> <bean id=" sqlsessionfactory "class=" Org.mybatis.spri Ng. Sqlsessionfactorybean "><!--sqlsessionfactory object--> <property name=" DAtasource "ref=" DataSource "/><!--data source--> <property name=" configlocation "value=" . xml "/><!--mybatis configuration file--> <!--<property name=" mapperlocations "value=" classpath*:com/john/hbatis/ Model/*.xml "/>--><!--can configure the mapping file Configuration.xml or here, but it cannot have the same ID parametermap, resultmap, or SQL--> </ bean> <bean id= "Mapperconfigurer" class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" ><!--
Scans for the specified package to get the mapper--> <property name= "basepackage" value= "Com.john.hbatis.mapper"/> </bean> </beans>
Database.properties under the classpath:
Driverclassname=com.mysql.jdbc.driver
Url=jdbc:mysql://localhost:3306/mybatis?characterencoding=utf8
User_name=root
password=123456
Note: Because Mapperscannerconfigurer may cause username to take the account of the system user and cause the database connection to fail, it is changed to another value: user_name.
SPRINGMVC configuration file Hbatis-servlet.xml:
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns:
Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/ MVC http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <mvc:annotation-driven/><!-- Register Requestmappinghandlermapping, Requestmappinghandleradapter and Exceptionhandlerexceptionresolver to provide support for annotations such as @requestmapping, @ExceptionHandler- > <context:component-scan base-package= "Com.john.hbatis.controller"/><!--a class with specific annotations under the scan controller package, and instantiate and dependency injection--&
Gt <!--freemarker View processor--> <bean id= "ViewresolverfTL "class=" Org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver "> <property name=" Viewclass "value=" Org.springframework.web.servlet.view.freemarker.FreeMarkerView "/> <property name=" ContentType "value=" Text/html;charset=utf-8 "/> <property name=" prefix "value=" "/> <property name=" CAC
He "value=" false "/> <property name=" Viewnames "> <array> <value>*.ftl</value> </array> </property> <!--<property name= "suffix" value= "FTL"/>--> <property Name= "Order" value= "0"/><!--priority, the smaller the number priority the higher the--> </bean> <bean id= "Freemarkerconfig" class= Ingframework.web.servlet.view.freemarker.FreeMarkerConfigurer "> <property name=" templateloaderpaths "> & lt;list> <value>/WEB-INF/ftl/</value><!--Template loading path--> </list> </property>
;
</bean> </beans>
4. MVC:
Control layer: Usercontroller.java
@Controller
@RequestMapping ("/article") public
class Usercontroller {
@Autowired
iusermapper Mapper;
@RequestMapping ("/list") public
String ShowAll (Modelmap modelmap) {
list<article> articles = Mapper.getarticlesbyuserid (1);
Modelmap.addattribute ("articles", articles);
return "MAIN.FTL";
}
View Layer: MAIN.FTL:
< #list articles as article>
<div>${article.id}. ${article.title}: ${article.content}</div>
</#list >
5. Start Project, browser input: http://localhost:8080/hbatis/article/list.htm to view the results.