spring集成mybatis

(1)编程步骤

1.导包。spring-webmvc,mybatis,mybatis-spring,spring-jdbc,dbcp,ojdbc.
2.配置文件 添加spring的配置文件。
注:mybatis的配置信息可以添加到spring的配置文件当中(只需要配置SqlSessionFactoryBean)。
3.实体类
4.映射文件
5.Mapper映射器。
6.在spring的配置文件中,添加MapperScannerConfigurer.
该bean负责调用SqlSession的getMapper方法,创建符合Mapper映射器要求的对象。
注:该bean会将这个对象添加到spring容器里面。

<?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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <util:properties id="jdbc"
        location="classpath:db.properties" />
    <!-- 配置连接池 -->
    <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="#{jdbc.driverclass}" />
        <property name="url" value="#{jdbc.url}" />
        <property name="username" value="#{jdbc.user}" />
        <property name="password" value="#{jdbc.password}" />
        <property name="maxActive" value="#{jdbc.maxActive}" />
    </bean>
    <!-- 配置SqlSessionFactoryBean -->
    <!-- spring集成mybatis,不再需要mybatis的配置文件,使用SqlSessionFactoryBean来代替mybatis的配置文件。 -->
    <bean id="ssfb" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 不再使用mybatis自带的连接池,而是使用spring的连接池 -->
        <property name="dataSource" ref="ds"></property>
        <!-- 映射文件的位置 -->
        <property name="mapperLocations"
            value="classpath:entity/*.xml" />
    </bean>
    <!-- 配置MapperScannerConfigurer -->
    <!-- MapperScannerConfigurer负责扫描指定包下面的所有的Mapper映射器,然后生成符合这些映射器要求的对象 (其实就是调用SqlSession的getMapper方法)。 
        另外,还会将这些对象添加到spring容器里面(默认的id是首字母小写之后的接口名,
        也可以使用@Respository来设置id)。 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- Mapper映射器所在的包 -->
        <property name="basePackage" value="dao"></property>
    </bean>

</beans>
package entity;

public class Emp {
    private Integer id;
    private String name;
    private Double age;
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Double getAge() {
        return age;
    }
    public void setAge(Double age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Emp [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
    
    
}
<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"      
 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="dao.EmpDAO">
    <insert id="save" parameterType="entity.Emp">
        insert into emps values(seq_emps.nextval,#{name},#{age})
    </insert>
    
    <!-- id:要求唯一。resultType:返回的数据类型。parameterType:参数类型 -->
    <select id="findAll" resultType="entity.Emp">
        select * from emps
    </select>
    
    <select id="findById" parameterType="int" resultType="entity.Emp">
        select * from emps where id = #{id1}
    </select>
    
    <!-- 使用ResultMap解决表的字段名和实体类属性名不一致的情况 -->
    <select id="findById3" parameterType="int" resultMap="emp2Map">
        select * from emps where id = #{id1}
    </select>
    <!-- 处理表的字段名与实体类的属性名的对应关系。 -->
    <resultMap type="entity.Emp2" id="emp2Map">
        <result property="empId" column="id"/>
    </resultMap>

</mapper>
package dao;

import entity.Emp;

/**
 * Mapper映射器。
 *
 */
public interface EmpDAO {
    public void save(Emp emp);
    public Emp findById(int id);
}
package test;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dao.EmpDAO;
import entity.Emp;

public class TestCase {
    private EmpDAO dao;
    @Before
    public void init() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("springmvc.xml");
        dao = ac.getBean("empDAO",EmpDAO.class);
    }
    
    @Test
    public void test1() {
        Emp emp = new Emp();
        emp.setName("Sally");
        emp.setAge((double) 22);
        dao.save(emp);
    }
    
    @Test
    public void test2() {
        Emp emp = dao.findById(3);
        System.out.println(emp);
    }
}

---------------------------------------------------------------------------------------------

小知识点

2122

(3)另一个集成方式(了解)

1.导包
2.添加spring的配置文件。
注:删除MapperScannerConfigurer配置。
3.实体类
4.映射文件
5.Mapper映射器(dao类)。
6.写dao实现类。

<?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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <util:properties id="jdbc"
        location="classpath:db.properties" />
    <!-- 配置连接池 -->
    <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="#{jdbc.driverclass}" />
        <property name="url" value="#{jdbc.url}" />
        <property name="username" value="#{jdbc.user}" />
        <property name="password" value="#{jdbc.password}" />
        <property name="maxActive" value="#{jdbc.maxActive}" />
    </bean>
    <!-- 配置SqlSessionFactoryBean -->
    <!-- spring集成mybatis,不再需要mybatis的配置文件,使用SqlSessionFactoryBean来代替mybatis的配置文件。 -->
    <bean id="ssfb" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 不再使用mybatis自带的连接池,而是使用spring的连接池 -->
        <property name="dataSource" ref="ds"></property>
        <!-- 映射文件的位置 -->
        <property name="mapperLocations"
            value="classpath:entity/*.xml" />
    </bean>
    <!-- 配置SqlSessionTemplate -->
    <bean id="sst" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="ssfb"></constructor-arg>
    </bean>
    <!-- 配置组件扫描 -->
    <context:component-scan base-package="dao"></context:component-scan>
    
</beans>
<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"      
 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="dao.EmpDAO">
    <insert id="save" parameterType="entity.Emp">
        insert into emps values(seq_emps.nextval,#{name},#{age})
    </insert>
    
    <!-- id:要求唯一。resultType:返回的数据类型。parameterType:参数类型 -->
    <select id="findAll" resultType="entity.Emp">
        select * from emps
    </select>
    
    <select id="findById" parameterType="int" resultType="entity.Emp">
        select * from emps where id = #{id1}
    </select>
    
    <!-- 使用ResultMap解决表的字段名和实体类属性名不一致的情况 -->
    <select id="findById3" parameterType="int" resultMap="emp2Map">
        select * from emps where id = #{id1}
    </select>
    <!-- 处理表的字段名与实体类的属性名的对应关系。 -->
    <resultMap type="entity.Emp2" id="emp2Map">
        <result property="empId" column="id"/>
    </resultMap>

</mapper>
package dao;

import entity.Emp;

/**
 * Mapper映射器。
 *
 */
public interface EmpDAO {
    public void save(Emp emp);
    public Emp findById(int id);
}
package dao;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import entity.Emp;

@Repository("eDAO")
public class EmpDAOMybatisImpl implements EmpDAO {
    
    @Resource(name="sst")
    private SqlSessionTemplate sst;
    
    public void save(Emp emp) {
        sst.insert("dao.EmpDAO.save", emp);
    }

    public Emp findById(int id) {
        Emp selectOne = sst.selectOne("dao.EmpDAO.findById");
        return selectOne;
    }

}
posted @ 2019-09-09 23:40  menbozg  阅读(703)  评论(0编辑  收藏  举报