Spring整合Mybatis

Mybatis回顾(核心步骤)

编写实体类

package com.kuang.pojo;

public class User {
   private int id;  //id
   private String name;   //姓名
   private String pwd;   //密码
}

编写核心配置文件

  • 注意mybatis的配置文件中各个标签是有顺序的
<?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>

    <properties resource="jdbc.properties" ></properties>

    <typeAliases>
        <typeAlias type="com.mybatis.POJO.User" alias="user" />
    </typeAliases>

    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>


    <mappers>
        <!--要用class注册的话,接口和mapper.xml文件必须在同一个包下,而且名字要一致-->
        <mapper resource="com.mybatis.mapper/UserMapper.xml" />
    </mappers>
</configuration>

编写接口类

public interface UserMapper {

     List<User> findAll() throws IOException;

     List<User> findByCondition( User user);

     List<User> findByIds( List<Integer> list);

     void save(User user);
}

编写Mapper.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="com.mybatis.Dao.UserMapper" >

    <select id="findAll" resultType="user">
    select * from user
    </select>

    <select id="findByCondition" resultType="user" parameterType="user">
    select * from user
    <where>
        <if test="id != 0">
        and id = #{id}
        </if>
        <if test="username != null and username != ''">
        and username = #{username}
        </if>
        <if test="password != null and password != ''">
        and password = #{password}
        </if>
    </where>
    </select>

    <select id="findByIds" parameterType="list" resultType="user">
    select * from user
    <where>
    <foreach collection="list" item="id" open="id in(" close=")" separator=",">
    #{id}
</foreach>
</where>
</select>

<select id="save" parameterType="user">
  insert into user values (#{id},#{username},#{password},#{birthday})
</select>
</mapper>

Spring整合Mybatis

什么是 MyBatis-Spring?

MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中。
对应的maven

<dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis-spring</artifactId>
   <version>2.0.2</version>
</dependency>
  • 要和 Spring 一起使用 MyBatis,需要在 Spring 应用上下文中定义至少两样东西:一个 SqlSessionFactory 和至少一个数据映射器类。
  1. 创建spring的核心配置文件
  2. 配置数据源代替mybatis的数据源
  3. 配置SqlSessionFactory,关联MyBatis
  4. 注册sqlSessionTemplate,关联sqlSessionFactory;
  • SqlSessionTemplate 是 MyBatis-Spring 的核心。作为 SqlSession 的一个实现,这意味着可以使用它无缝代替你代码中已经在使用的 SqlSession。
  1. 增加Dao接口的实现类;私有化sqlSessionTemplate
  2. 注册bean实现
<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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    <context:property-placeholder location="jdbc.properties" />

<!--    配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"   value="${jdbc.driver}"/>
        <property name="url"               value="${jdbc.url}" />
        <property name="username"          value="${jdbc.username}" />
        <property name="password"          value="${jdbc.password}" />
    </bean>

<!--    配置sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
<!--        关联mybatis-->
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
<!--        <property name="mapperLocations" value="com/mybatis/Dao/*.xml" />-->
    </bean>


    <!--注册sqlSessionTemplate , 关联sqlSessionFactory-->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <!--利用构造器注入-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    <bean id="userDao" class="com.mybatis.Dao.UserDaoImpl">
        <property name="sqlSession" ref="sqlSessionTemplate" />
    </bean>
</beans>
posted @ 2021-04-20 18:20  17_Xtreme  阅读(119)  评论(0编辑  收藏  举报