第一步:创建实体类---CarTypeDict

package cn.cscm.domain;
public class CarTypeDict {
    private int id;
    private String coding;
    private String brand;
    private String trait;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getCoding() {
        return coding;
    }
    public void setCoding(String coding) {
        this.coding = coding;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public String getTrait() {
        return trait;
    }
    public void setTrait(String trait) {
        this.trait = trait;
    }
    public String toString(){
        return "coding="+coding+"\tbrand="+brand+"\ttrait="+trait;
    }
}

第二步:创建映射器接口---CarTypeDictMapper

package cn.cscm.mapper;
import java.util.List;
import org.springframework.stereotype.Repository;
import cn.cscm.domain.CarTypeDict;
@Repository("carTypeDictMapper")
public interface CarTypeDictMapper {
    void addCarType(CarTypeDict ctd);
    List<CarTypeDict> selectAllCarTypeDicts();
}

第三步:创建映射器---CarTypeDictMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.cscm.mapper.CarTypeDictMapper">
    <parameterMap type="cn.cscm.domain.CarTypeDict" id="paramMapCarType">
        <parameter property="id"/>
        <parameter property="coding"/>
        <parameter property="brand"/>
        <parameter property="trait"/>
    </parameterMap>
    <resultMap type="cn.cscm.domain.CarTypeDict" id="resultMapCarType">
        <result property="id" column="id"/>
        <result property="coding" column="coding"/>
        <result property="brand" column="brand"/>
        <result property="trait" column="trait"/>
    </resultMap>
    <insert id="addCarType" parameterMap="paramMapCarType">
        <selectKey resultType="int" keyProperty="id">
            select @@identity as columnId
        </selectKey>
        insert into CarTypeDict(id,coding,brand,trait)
        values                 (?,?,?,?)
    </insert>
    <select id="selectAllCarTypeDicts" resultMap="resultMapCarType">
        SELECT id,coding,brand,trait
        FROM carTypeDict
    </select>
</mapper>

第四步:创建业务层接口---ICarTypeDictService

package cn.cscm.service;
import java.util.List;
import cn.cscm.domain.CarTypeDict;
public interface ICarTypeDictService {
    public final static String SERVICE_NAME="cn.cscm.service.impl.ICarTypeDictDaoImpl";
                                                                                                                                                                      
    public void add(CarTypeDict ctd);
    public List<CarTypeDict> selectAllCarTypeDicts();
}

第五步:实现业务层类---CarTypeDictServiceImpl

package cn.cscm.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import cn.cscm.domain.CarTypeDict;
import cn.cscm.mapper.CarTypeDictMapper;
import cn.cscm.service.ICarTypeDictService;
@Service(ICarTypeDictService.SERVICE_NAME)
public class CarTypeDictServiceImpl implements ICarTypeDictService {
    //在此处 注入一个CarTypeDictMapper,    Spring自动生成
    @Autowired
    private CarTypeDictMapper carTypeDictMapper;
    public void add(CarTypeDict ctd) {
        this.carTypeDictMapper.addCarType(ctd);
    }
    @Override
    public List<CarTypeDict> selectAllCarTypeDicts() {
        return this.carTypeDictMapper.selectAllCarTypeDicts();
    }
}

第六步:配置mybatis-configuration.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>

第七步:配置spring配置文件---beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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-2.5.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-2.5.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <!--1、开启注解-->
    <context:annotation-config />
    <!--2、注解的自动扫描-->
    <context:component-scan base-package="cn.cscm" />
    <!--3、数据源-->
    <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
        <property name="driver" value="com.mysql.jdbc.Driver"></property>
        <property name="driverUrl"
            value="jdbc:mysql://localhost:3306/carscm?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeconvertToNull">< /property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <!--4、配置本地化代理工程bean,创建SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:MyBatis-Configuration.xml"></property>
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 5、开启事务注解驱动 -->
    <tx:annotation-driven />
    <!-- 6、事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 7、自动扫描映射器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.cscm.mapper" />
    </bean>
</beans>

第八步:编写ServiceProviderCore

package cn.cscm.container;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServiceProviderCore {
    protected ApplicationContext ctx;
    public void load(String fileName){
        ctx=new ClassPathXmlApplicationContext(fileName);
    }
}

第九步:编写ServiceProvider

package cn.cscm.container;
import org.apache.commons.lang3.StringUtils;
public class ServiceProvider {
    private static ServiceProviderCore sc;
    static{
        sc=new ServiceProviderCore();
        sc.load("beans.xml");
    }
                                           
    public static Object getService(String beanName){
        if (StringUtils.isBlank(beanName)) {
            throw new RuntimeException("您要访问的服务名称不能为空!");
        }
        Object bean = null;
        if (sc.ctx.containsBean(beanName)) {
            bean = sc.ctx.getBean(beanName);
        }
        if (bean == null) {
            throw new RuntimeException("您要访问的服务名称[" + beanName + "]不存在!");
        }
        return bean;
    }
}

最后单元测试:

package junit;
import org.junit.Test;
import cn.cscm.container.ServiceProvider;
import cn.cscm.domain.CarTypeDict;
import cn.cscm.service.ICarTypeDictService;
import cn.cscm.web.action.CarTypeDictAction;
public class Test {
                                     
    @Test
    public void test(){
        CarTypeDictService carTypeDictService=(CarTypeDictService)ServiceProvider.getService(CarTypeDictService.SERVICE_NAME);
        for(CarTypeDict carTypeDict:carTypeDictService.selectAllCarTypeDicts()){
            System.out.println(carTypeDict);
        }
    }
}

相关的jar包---struts2+spring3+mybatis:

mybatis集成spring示例_beans.xml