一、環境搭建
1.創建動態web工程
2.引入jar包
3.創建目錄結構
4.創建數據庫的配置文件(.properties)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///***
jdbc.password=**
jdbc.user=**
二.配置mybatis環境
1.創建實體類bean和dao類(代碼略)
2.創建dao(mybatis的配置文件)
三.配置SpringMVC(在web.xml中)
1.配置ContextConfigListener
2.同時創建applicationContext.xml、spring-servlet.xml文件
3.配置spring-servlet.xml文件
四.配置spring環境(其中包含配置mybatis)
五.前段及service層和controller層設計
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!-- Spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- Bootstraps the root web application context before servlet initialization --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- SpringMVC的配置文件 --> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
applicationContext.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" xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring" xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- Spring配置文件,希望管理所有的邏輯組件等 --> <!-- 除了Controller之外都掃描 --> <context:component-scan base-package="mybatis"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 引述數據庫的配置文件 --> <context:property-placeholder location="classpath:dbcpfig.properties"/> <!-- Spring用來控制業務邏輯、數據源、事務控制、aop --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="driverClass" value="${jdbc.driver}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- spring事務管理 --> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 控制住數據裏裏面的所有鏈接,連接的提交、關閉都是管理器控制的 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 開啓基於註解的事務 --> <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/> <!-- 整合mybatis --> <!-- 目的:1.spring管理所有組件。mapper的實現類 service==>Dao @Autowired:自動關注入mapper; 2.spring用來管理實務,spring聲明事務 --> <!-- 會創建出SqlsessionFactory對象 --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- configLocation指定全局配置文件的位置 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!-- mapperLocations:指定mapper文件的位置 --> <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property> </bean> <!-- 掃描所有的mapper接口的實現,讓這些mapper能夠自動注入; base-package:指定mapper接口的包名 --> <mybatis-spring:scan base-package="mybatis.dao"/> </beans>
spring-servlet.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- SpringMVC配置文件,只負責網站的跳轉邏輯--> <!-- 只掃描控制器 --> <context:component-scan base-package="mybatis" use-default-filters="true"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 正確處理動態資源 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 正確處理靜資源 --> <mvc:default-servlet-handler/> <!-- 視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
mybatis.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> <settings> <!-- 延遲加載 --> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> <!-- 啓用駝峯命名 --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration>
EmployeeMapper.java public interface EmployeeMapper { public List<Employee> getEmps(); }
EmployeeService.java @Service public class EmployeeService { @Autowired private EmployeeMapper employeeMapper; public List<Employee> getEmps(){ return employeeMapper.getEmps(); } }
index.jsp <body> <a href="getemps">查詢所有員工</a> </body>
list.jsp <%@ taglib prefix="c" uri=" <body> <h4>員工列表</h4> <table> <tr> <td>Id</td> <td>LastName</td> <td>Email</td> <td>Gender</td> </tr> <c:forEach items="${allEmps }" var="emp"> <tr> <td>${emp.id }</td> <td>${emp.lastName }</td> <td>${emp.email }</td> <td>${emp.gender }</td> </tr> </c:forEach> </table> </body>