Spring整合Mybatis

  1. 导入jar包(mybatis所有jar包,Spring基本jar包,spring-jdbc,spring-tx,spring-aop,spring整合mybatis的包);
  2. 编写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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      	<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="123456"/>
      </bean>
        <!-- 创建SqlSessionFactory对象 -->
      <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
      	<!-- 数据库连接信息来源于dataSource -->
      	<property name="dataSource" ref="dataSource"></property>
      </bean>
      <!-- 扫描器相当于mybatis.xml中mappers下的package标签 -->
      <bean id="configurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      	<!-- 扫描哪个包 -->
      	<property name="basePackage" value="com.lee.mapper"></property>
      	<!-- 和factory产生关系 -->
      	<property name="sqlSessionFactory" ref="factory"/>
      </bean>
      <!-- 由spring管理service实现类 -->
      <bean id="airportService" class="com.lee.service.impl.AirportServiceImpl">
      	<property name="airportMapper" ref="airportMapper"></property>
      </bean>
      
</beans>
  1. 编写代码
    3.1 正常编写pojo
    3.2 编写mapper接口绑定方案或者注解
    3.3 正常编写Service接口与Service实现类
    3.3.1 需要在Service实现类中声明mapper接口对象,并生成get/set方法
    3.4 spring无法管理Servlet

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee                       
	http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
	<!-- 上下文参数 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<!-- 封装了一个监听器,磅数加载spring的配置文件 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
</web-app>

servlet

@WebServlet("/airport")
public class AirportServlet extends HttpServlet{
	private AirportService airportService;	
	
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		//对service实例化
		//ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		//spring和web整合后所有的信息都存放在webApplicationContext
		ServletContext servletContext = config.getServletContext();
		System.out.println(servletContext);
		ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		airportService = (AirportService) ac.getBean("airportService",AirportServiceImpl.class);
		
	}
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setAttribute("list", airportService.show());
		req.getRequestDispatcher("index.jsp").forward(req, resp);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值