Configure jdbc.properties under resources
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=1234
Continue to configure the mybatis related configuration file mybatis-config.xml under resources
<?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> <!-- Use jdbc's getGeneratedKeys to get the database self-incrementing primary key value --> <setting name="useGeneratedKeys" value="true" /> <!-- Replace column names with column labels Default: true --> <setting name="useColumnLabel" value="true" /> <!-- Turn on camelCase naming conversion: Table{create_time} -> bean{createTime} --> <setting name="mapUnderscoreToCamelCase" value="true" /> <!-- print query statement--> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings> </configuration>
Configure Spring's configuration file under resources/spring
1) dao layered spring-dao.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" 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"> <!-- Configure the process of integrating mybatis--> <!-- 1. Configure the properties of database-related parameters properties: ${url} --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 2. Database connection pool --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- Configure connection pool properties--> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- Private properties of c3p0 connection pool --> <property name="maxPoolSize" value="30" /> <property name="minPoolSize" value="10" /> <!-- Do not automatically commit after closing the connection --> <property name="autoCommitOnClose" value="false" /> <!-- Get connection timeout time--> <property name="checkoutTimeout" value="10000" /> <!-- When the connection fails to get the number of retries--> <property name="acquireRetryAttempts" value="2" /> </bean> <!-- 3. Configure the SqlSessionFactory object --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- Inject database connection pool--> <property name="dataSource" ref="dataSource" /> <!-- Configure MyBaties global configuration file: mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-config.xml" /> <!-- Scan entity packages using aliases --> <property name="typeAliasesPackage" value="com.shop.bean" /> <!-- Scan sql configuration file: xml file required by mapper--> <property name="mapperLocations" value="classpath:mapper/*.xml" /> </bean> <!-- 4. Configure and scan the Dao interface package, dynamically implement the Dao interface, and inject it into the spring container --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- Inject sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <!-- Give the Dao interface package that needs to be scanned --> <property name="basePackage" value="com.shop.dao" /> </bean> </beans>
2) Configure spring-service.xml of the service 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" 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 all classes using annotations under the service package --> <context:component-scan base-package="com.shop.service" /> <!-- Configure Transaction Manager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- Inject database connection pool--> <property name="dataSource" ref="dataSource" /> </bean> <!-- Configure annotation-based declarative transactions --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
3)controller层 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"> <!-Deployment Spring MVC-> <!-- 1. Enable SpringMVC annotation mode--> <!-- Simplified configuration: (1) Automatically register DefaultAnootationHandlerMapping, AnotationMethodHandlerAdapter (2) Provide some columns: data binding, format of numbers and dates @NumberFormat, @DateTimeFormat, xml, json default read and write support --> <mvc:annotation-driven /> <!-- 2. Default servlet configuration for static resources (1) Add processing of static resources: js, gif, png (2) Allow the use of "/" for overall mapping --> <mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:default-servlet-handler /> <!-- 3. Define the view resolver --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/html/"></property> <property name="suffix" value=".html"></property> </bean> <!-- 4. Scan web-related beans --> <context:component-scan base-package="com.shop.controller" /> </beans>
Integrate spring configuration together
configure web.xml
<!-- 配置DispatcherServlet --> <servlet> <servlet-name>spring-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Configure the configuration files spring-dao.xml, spring-service.xml, spring-web.xml that springMVC needs to load 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> <!-- matches all requests by default --> <url-pattern>/</url-pattern> </servlet-mapping>