作为Bean容器,Spring框架提供了IOC机制,可以接管所有组件的创建工作并进行依赖管理
spring整合MyBatis的准备工作
1.在项目中加入相关jar包
<!--Mybatis+Spring整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.1</version>
</dependency>
<!-- Spring整合JavaWeb的包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<!--javaee jar-->
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
</dependency>
2.创建开发目录结构
3.配置相关映射/配置文件
<?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"> <!--</bean>--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:Mybatis-Config.xml"></property> <property name="dataSource" ref="dataSource"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.dome07.dao"></property> </bean> <bean id="bookService" class="cn.dome07.service.BookServiceImpl"> <property name="bookDao" ref="IBookDao"></property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> </beans>
servlet中
package cn.dome07.servlet; import cn.dome07.entity.Book; import cn.dome07.service.IBookService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.support.WebApplicationContextUtils; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Transactional() public class BookServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String bookName = request.getParameter("bookName"); String bookAuthor = request.getParameter("bookAuthor"); Integer bookPrice =Integer.parseInt(request.getParameter("bookPrice")); Book book = new Book(); book.setBookName(bookName); book.setBookAuthor(bookAuthor); book.setBookPrice(bookPrice); ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); IBookService bs = (IBookService)ac.getBean("bookService"); try { int count = bs.addBook(book); if(count>0){ request.getRequestDispatcher("/book.jsp").forward(request,response); }else{ response.sendRedirect("../index.jsp"); } } catch (Exception e) { e.printStackTrace(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
。。。结束