Spring4- Spring简易整合Mybatis -导入jar包/ 正常编写pojo/ 编写spring 配置文件

笔记要点&出错分析与总结

  POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,
工程组织  

(AirportService为机场服务站接口,定义了 List<Airport> show();

package com03.service;

import com03.bean.Airport;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface AirportService {

    List<Airport> show();
}
View Code

  AirportserviceImpl 是该接口的提供具体的实现类,重写接口的方法,并有get/set 方法

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

package com03.service;

import com03.bean.Airport;
import com03.mapper.AirportMapper;

import java.util.List;

public class AirportServiceImpl implements AirportService{
    private AirportMapper airportMapper;
    @Override
    public List<Airport> show() {
        return airportMapper.selectAll();
    }

    public AirportMapper getAirportMapper() {
        return airportMapper;
    }

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

  AirportMapper接口  实现从数据库查询信息返回List<Airport> )

package com03.mapper;
import com03.bean.Airport;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface AirportMapper {
    @Select("select * from airport")
    public List<Airport> selectAll();
}

具体结构图,并1.导入mybatis所有jar 包 ,spring基本包spring-jdbc,spring-tx,spring-aop,spring-web,spring整合mybatis 的包等.

数据库组织

    id  airplane_no    time   price  takeoff_id  land_id  
------  -----------  ------  ------  ----------  ---------
     1  波音747           123     100           1          3
     2  波音858            56     300           3          2


0.定义Bean类  Airport

package com03.bean;

public class Airport {
    private int id;
    private String portName;
    private String cityName;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getPortName() {
        return portName;
    }
    public void setPortName(String portName) {
        this.portName = portName;
    }
    public String getCityName() {
        return cityName;
    }
    public void setCityName(String cityName) {
        this.cityName = cityName;
    }
    @Override
    public String toString() {
        return "Airport [id=" + id + ", portName=" + portName + ", cityName=" + cityName + "]";
    }
}
View Code

1.定义接口 (见上面)
2.定义Spring的XML映射文件  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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/beans/spring-beans.xsd">
   <!--配置Mybatis的基本登陆信息,连接信息;除了事务方面的-->
    <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"></property>
    </bean>
    <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--扫描相当于mybatis.xml中的mapper的package标签; 扫描指定文件夹下的全部配置文件/接口,会自动为接口创建对象-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--要扫描那个包-->
        <property name="basePackage" value="com03.mapper"></property>
        <!--和factory 产生关系-->
        <property name="sqlSessionFactory" ref="factory"></property>
    </bean>
    <!--ref="airportMapper" ,上一步扫描完全局配置和接口文件后,会自动创建该bean-->
    <!--由Spring -->
    <bean id="airportService" class="com03.service.AirportServiceImpl">
        <property name="airportMapper" ref="airportMapper"></property>
    </bean>
</beans>

3.编写测试代码 test01   (配置文件不在默认的SRC下, 在 com03/conf/applicationContext.xml)

package com03.test;

import com03.bean.Airport;
import com03.service.AirportServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class test01 {
    public static void main(String[] args) {
        //创建Spring 容器 ,默认去根目录开始寻找
        ApplicationContext ac=new ClassPathXmlApplicationContext("com03/conf/applicationContext.xml");
        String[] names = ac.getBeanDefinitionNames();
        for (String name:names
             ) {
            System.out.println("★Spring自动创建了:"+"  "+name);
        }
        AirportServiceImpl bean = ac.getBean("airportService", AirportServiceImpl.class);
        List<Airport> list = bean.show();
        System.out.println("★"+list);
    }
}

测试结果

★Spring自动创建了:  dataSource
★Spring自动创建了:  factory
★Spring自动创建了:  org.mybatis.spring.mapper.MapperScannerConfigurer#0
★Spring自动创建了:  airportService
★Spring自动创建了:  airportMapper
★Spring自动创建了:  org.springframework.context.annotation.internalConfigurationAnnotationProcessor
★Spring自动创建了:  org.springframework.context.annotation.internalAutowiredAnnotationProcessor
★Spring自动创建了:  org.springframework.context.annotation.internalRequiredAnnotationProcessor
★Spring自动创建了:  org.springframework.context.annotation.internalCommonAnnotationProcessor
★Spring自动创建了:  org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
★Spring自动创建了:  org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor
DEBUG 12-08 10:44:15,527 JDBC Connection [com.mysql.jdbc.JDBC4Connection@399f45b1] will not be managed by Spring  (SpringManagedTransaction.java:87) 
DEBUG 12-08 10:44:15,532 ==>  Preparing: select * from airport   (BaseJdbcLogger.java:139) 
DEBUG 12-08 10:44:15,556 ==> Parameters:   (BaseJdbcLogger.java:139) 
DEBUG 12-08 10:44:15,572 <==      Total: 3  (BaseJdbcLogger.java:139) 
DEBUG 12-08 10:44:15,572 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3eb7fc54]  (SqlSessionUtils.java:193) 
DEBUG 12-08 10:44:15,572 Returning JDBC Connection to DataSource  (DataSourceUtils.java:327) 
★[Airport [id=1, portName=首都机场, cityName=北京], Airport [id=2, portName=南阳机场, cityName=南阳], 

Airport [id=3, portName=虹桥机场, cityName=上海]]

猜你喜欢

转载自www.cnblogs.com/zhazhaacmer/p/10087969.html