美文网首页
spring集成mybatis

spring集成mybatis

作者: 两分与桥 | 来源:发表于2018-09-04 07:27 被阅读28次

spring集成mybatis

1.编程步骤
    a.导包,spring-webmvc,mybatis,mybatis-spring
            mybatis-spring,spring-jdbc,dbcp,junit
    b.添加spring的配置文件,mybatis的配置信息可以添加到
    spring的配置文件当中。只需要配置SqlSessionFactoryBean.
    c.实体类
    d.映射文件
    e.在spring的配置文件当中,添加MapperScannerConfigurer
    该bean负责调用SqlSession的getMapper方法,创建符合Mapper映射器要求的对象。
    该bean会将这个对象添加到spring容器里面

2.只扫描特定的Mapper映射器(有时候我们不一定只使用mybatis连接数据库的情况)
    a.开发一个注解,比如@MyBatisRepository
    b.将该注解添加到需要扫描的映射器
    c.配置MapperScannerConfigurer,设置annotationClass,指定注解类。

整个项目文件路径


1.png

导包

  <dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.46</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.2.8</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.2.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.2.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.2.2</version>
    </dependency>
  </dependencies>

在entity下的实体类Emp

package entity;

public class Emp {
    private Integer id;
    private String name;
    private Double age;
}
省略一堆的get/set方法

在entity下的EmpMapper.xml文件(与在mybatis中的配置一模一样)

<?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">
<!-- namespace 的值是 DeptMapper 接口
  每个Mapper 接口对应一个配置文件  -->
<mapper namespace="dao.EmpDAO">
    <insert id="save" parameterType="entity.Emp">
        INSERT INTO emp(name, age) VALUES(#{name},#{age})
    </insert>
    !--
        id:要求唯一
        resultType:返回结果的类型
        parameterType:参数类型
     -->
    <select id="findAll" resultType="entity.Emp">
        SELECT * FROM emp
    </select>
    
    <select id="findById" parameterType="int" resultType="entity.Emp">
        SELECT * FROM emp WHERE id=#{id}
    </select>
    
    <update id="modify" parameterType="entity.Emp">
        UPDATE emp SET name=#{name}, age=#{age} WHERE id=#{id}
    </update>
    
    <delete id="delete" parameterType="int">
        DELETE FROM emp WHERE id=#{id}
    </delete>
    
    !-- 返回Map类型的结果 -->
    <select id="findById2" parameterType="int" resultType="map">
        SELECT * FROM emp WHERE id=#{id}
    </select>
    
    !-- 使用ResultMap解决表的字段名与实体类的属性名不一致的情况 -->
    <select id="findById3" parameterType="int" resultMap="emp2Map">
        SELECT * FROM emp WHERE id=#{id}
    </select>
    
    !-- 处理表的字段名与实体类的属性名的对应关系,列出不一样的即可 -->
    <resultMap type="entity.Emp2" id="emp2Map">
        <result property="eno" column="id"></result>
        <result property="ename" column="name"></result>
    </resultMap>
</mapper>

resource文件夹下的数据库连接参数db.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/test
jdbc.username=root
jdbc.password=root

initialSize=2
maxActive=2

dao下的EmpDAO文件

package dao;

import java.util.List;
import org.springframework.stereotype.Repository;
import annotation.MyBatisRepository;
import entity.Emp;

 * Mapper映射器
@Repository("empDAO")
@MyBatisRepository
public interface EmpDAO {
    public void save(Emp emp);
    
    public List<Emp> findAll();
    
    public Emp findById(int id);
    
    public void modify(Emp emp);
    
    public void delete(int id); 
}

annotation文件夹下的注解文件MyBatisRepository

package annotation;

public @interface MyBatisRepository {}

最后的就是整个配置文件了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:beans="http://www.springframework.org/schema/beans" 
    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:jms="http://www.springframework.org/schema/jms"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    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/jms http://www.springframework.org/schema/tx/spring-jms-3.2.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/tx/spring-lang-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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

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

另一种集成方式 - 了解

1.导包
2.添加spring的配置文件
3.实体类
4.映射文件
5.DAO接口
6.DAO实现类
注入SqlSessionTemplate,调用该对象的方法。
配置SqlSessionTemplate
不要忘记添加组件扫描

相关文章

网友评论

      本文标题:spring集成mybatis

      本文链接:https://www.haomeiwen.com/subject/mqbwwftx.html