1. 程式人生 > >Spring4- Spring簡易整合Mybatis -匯入jar包/ 正常編寫pojo/ 編寫spring 配置檔案

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 [[email protected]] 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 [[email protected]]  (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=上海]]