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;
    }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM