Spring整合MyBatis

1. 导 入 mybatis 所 有 jar 和 spring 基 本 包,spring-jdbc,spring-tx,spring-aop,spring-web,spring 整合 mybatis(mybatis-spring) 的包 等

2. 先配置 web.xml(为了在Web项目中生效)

<?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-instanc
e"
xsi:schemaLocation="http://java.sun.com/xml/ns/java
ee
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.ContextLoaderListener</listener-class>
</listener>
</web-app>

3. 编写 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.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="smallming"></property>
    </bean>
    <!-- 创建SqlSessionFactory对象 -->
    <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
    	<!-- 数据库连接信息来源于dataSource -->
    	<property name="dataSource" ref="dataSouce"></property>
    </bean>
    <!-- 扫描器相当于mybatis.xml中 mappers下package标签,扫描com.bjsxt.mapper包后会给对应接口创建对象-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    	<!-- 要扫描哪个包 -->
    	<property name="basePackage" value="com.bjsxt.mapper"></property>
    	<!-- 和factory产生关系 -->
        <!-- 新版本使用sqlSessionFactoryBeanName 和value-->
    	<property name="sqlSessionFactory" ref="factory"></property>
    </bean>
    <!-- 由spring管理service实现类 -->
    <!-- 当mapper包被扫描时,Spring就接管了AirportMapper接口,并产生了首字母小写的对象 -->
    <bean id="airportService" class="com.bjsxt.service.impl.AirportServiceImpl">
    	<property name="airportMapper" ref="airportMapper"></property>
    </bean>
</beans>

4. 编写代码

    4.1 正常编写 pojo

    4.2 编写 mapper 包下时必须使用接口绑定方案或注解方案(必须 有接口)

public interface AirportMapper {
    @Select({"select * from airport"})
    List<Airport> selAll();
}

    4.3 正常编写 Service 接口和 Service 实现类

        4.3.1 需要在 Service 实现类中声明 Mapper 接口对象,并生成 get/set 方法

public class AirportServiceImpl implements AirportService {
    private AirportMapper airportMapper;

    public AirportServiceImpl() {
    }

    public AirportMapper getAirportMapper() {
        return this.airportMapper;
    }

    public void setAirportMapper(AirportMapper airportMapper) {
        this.airportMapper = airportMapper;
    }

    public List<Airport> show() {
        return this.airportMapper.selAll();
    }
}

    4.4 spring 无法管理 Servlet,在 service 中取出 Servie 对象

public class Test {
    public Test() {
    }

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        AirportServiceImpl bean = (AirportServiceImpl)ac.getBean("airportService", AirportServiceImpl.class);
        List<Airport> list = bean.show();
        System.out.println(list);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值