使用 Spring 簡化 MyBatis
-
導 入mybatis所有jar和spring基本包,spring-jdbc,spring-tx,spring-aop,spring-web,spring 整合 mybatis 的包(mybaits-spring)等
-
先配置 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- 上下文參數 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- spring 配置文件 -->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 封裝了一個監聽器,幫助加載 Spring 的配置文件 -->
<listener>
<listener-class>
org.springframework.web.context.Con textLoaderListener
</listener-class>
</listener>
</web-app>
- 編寫 spring 配置文件 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 數據源封裝類 .數據源:獲取數據庫連 接,spring-jdbc.jar 中-->
<bean id="dataSouce"
class="org.springframework.jdbc.datasource.DriverMana gerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"></property>
<property name="username" value="root"></property>
<property name="password" value="smallming"></property>
</bean>
<!-- 創建 SqlSessionFactory 對象 -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 數據庫連接信息來源於 dataSource -->
<property name="dataSource" ref="dataSouce"></property>
</bean>
<!-- 掃描器相當於mybatis.xml中mappers下package標籤,掃描mapper包後會給對應接口創建對象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigu rer">
<!-- 要掃描哪個包 -->
<property name="basePackage" value="com.bjsxt.mapper"></property>
<!-- 和 factory 產生關係 -->
<property name="sqlSessionFactory" ref="factory"></property>
</bean>
<!-- 由 spring 管理 service 實現類 -->
<bean id="airportService" class="com.bjsxt.service.impl.AirportServiceImpl">
<property name="airportMapper" ref="airportMapper"></property>
</bean>
</beans>
- 編寫代碼
- 正常編寫 pojo
- 編寫 mapper 包下時必須使用接口綁定方案或註解方案(必須有接口)
- 正常編寫 Service 接口和 Service 實現類
- 需要在 Service 實現類中聲明 Mapper 接口對象,並生成get/set 方法
- spring 無法管理 Servlet,在 service 中取出 Servie 對象
@WebServlet("/airport")
public class AirportServlet extends HttpServlet{
private AirportService airportService;
@Override
public void init() throws ServletException {
//對 service 實例化
// ApplicationContext ac =
// new ClassPathXmlApplicationContext("applicationContext.xml");
//spring 和 web 整合後所有信息都存放在 webApplicationContext
ApplicationContext ac =
WebApplicationContextUtils.getRequiredWebApplicationC ontext(getServletContext());
airportService=ac.getBean("airportService",AirportS erviceImpl.class);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("list", airportService.show());
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}
));
req.getRequestDispatcher(“index.jsp”).forward(req, resp);
}
}