Configuring the Jdbc.properties under Resources
jdbc.driver=com.mysql.jdbc.driverjdbc.url=jdbc:mysql://localhost:3306/shop?useunicode=true& characterencoding=utf8jdbc.username=rootjdbc.password=1234
Continue to configure MyBatis related profiles under Resources mybatis-config.xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en" "http://mybatis.org/dtd/ Mybatis-3-config.dtd "><configuration> <!--Configure global Properties-- <settings> <!-- Get database self-increment primary key value using JDBC Getgeneratedkeys- <setting name= "Usegeneratedkeys" value= "true"/> <!-- Replace column names with column labels default: true-- <setting name= "Usecolumnlabel" value= "true"/> <!--turn on hump naming conversions: Table{create_ TIME} Bean{createtime}- <setting name= "Mapunderscoretocamelcase" value= "true"/> <!-- Print a query statement-- <setting name= "Logimpl" value= "stdout_logging"/> </settings></ Configuration>
Configure the spring configuration file under Resources/spring
1) Spring-dao.xml of the DAO layer
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" 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 "> <!--Configuration Integration MyBatis Process--<!--1. Configuring database-related Parameters properties: ${url}-<context:p Roperty-placeholder location= "Classpath:jdbc.properties"/> <!--2. Database connection pool--<bean id= "DataSource" class = "Com.mchange.v2.c3p0.ComboPooledDataSource" > <!--Configure connection pooling Properties--<property name= "Driverclass" Valu E= "${jdbc.driver}"/> <property name= "Jdbcurl" value= "${jdbc.url}"/> <property name= "user" Valu E= "${jdbc.username}"/> <property name="Password" value= "${jdbc.password}"/> <!--c3p0 Connection pool private Properties--<property name= ' maxpoolsize ' value= "/> <property name=" minpoolsize "value="/> <!--closed after connection not automatically commit---<propert Y name= "Autocommitonclose" value= "false"/> <!--get connection time-out time-<property name= "Checkouttimeout" V Alue= "10000"/> <!--when getting connection failure retries--<property name= "acquireretryattempts" value= "2"/> &L T;/bean> <!--3. Configuring Sqlsessionfactory Objects--<bean id= "sqlsessionfactory" class= "org.mybatis.spring.SqlSess Ionfactorybean "> <!--Inject database connection pool-<property name=" DataSource "ref=" DataSource "/> < ;! --Configure Mybaties Global profile: Mybatis-config.xml---<property name= "configlocation" value= "classpath:mybatis-config.xm L "/> <!--scan the entity package using aliases--<property name=" Typealiasespackage "value=" Com.shop.bean "/> <!--Scan SQL configuration file: Mapper Required XML file--<property name= "mapperlocations" value= "Classpath:mapper/*.xml"/> </bean> <!--4. Configure the scan DAO interface package, dynamically implement the DAO interface, inject into the spring container--<bean class= " Org.mybatis.spring.mapper.MapperScannerConfigurer "> <!--injected sqlsessionfactory-<property name= "Sqlsessionfactorybeanname" value= "Sqlsessionfactory"/> <!--given the need to scan the DAO Interface Pack--and <property name= "b Asepackage "value=" Com.shop.dao "/> </bean></beans>
2) Configuring the service layer Spring-service.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx" 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/tx http:/ /www.springframework.org/schema/tx/spring-tx.xsd "> <!--Scan the service pack for all classes using annotations--<context: Component-scan base-package= "Com.shop.service"/> <!--configuration transaction Manager--<bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" > <!--Inject database connection pool--<p Roperty name= "DataSource" ref= "DataSource"/> </bean> <!--Configuring annotation-based declarative transactions--<tx:annotation-driven transaction-manager= "TransactionManager"/>< /beans>
3) Controller Layer Spring-web.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" 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-3.2.xsd > <!--configuration Springmvc-<!--1. Turn on SPRI NGMVC Annotation Modes-<!--simplified configuration: (1) Autoenrollment Defaultanootationhandlermapping,anotationmethodhandleradapter (2) provides some columns: data binding Fixed, number and date format @NumberFormat, @DateTimeFormat, xml,json default read-write support--<mvc:annotation-driven/> <!--2. Static resources The servlet configuration (1) joins the processing of static resources: Js,gif,png (2) allows the use of "/" to do the overall mapping--> <mvc:resources mapping= "/resources/**" location= "/resources/"/> <mvc:default-servlet-handler/> <!--3. Defining the View resolver-<bean id= "Viewresolver" class= "Org.springframework.web.servlet.view.InternalResou Rceviewresolver "> <property name=" prefix "value="/web-inf/html/"></property> <property Nam e= "suffix" value= ". html" ></property> </bean> <!--4. Scan Web-related beans--<context:component-s Can base-package= "Com.shop.controller"/></beans>
Integrate Spring's configuration
Configure Web. xml
<!--configuration Dispatcherservlet-- <servlet> <servlet-name>spring-dispatcher</ Servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class > <!--configuration Springmvc need to load configuration files spring-dao.xml,spring-service.xml,spring-web.xml Mybatis-> Spring- > Springmvc <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </init-param> </servlet > <servlet-mapping> <servlet-name>spring-dispatcher</servlet-name> <!-- Match all requests by default- <url-pattern>/</url-pattern> </servlet-mapping>
shop--4. Configuring database connections Jdbc.properties,mybatis related, spring configuration