springBoot 整合mybatis

## springBoot 整合mybatis s使用IDEA开发,为了提升效率, 推荐使用插件: mybatis插件java

说明:上述插件能够在低版本的IDEA中找到破解版mysql

项目结构

说明:
mapper/UserMapper.xml 是与接口 对应的mapper文件
UserMapper :是接口,封装 DAO 操做的,相似于hibernate的UserDaogit

环境搭建步骤

生成generator文件

生成文件名称为:MybatisGenerator.xml
内容:github

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
    PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <!--<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />-->

    <context id="Mybatis3Context" targetRuntime="MyBatis3">
        <property name="javaFileEncoding" value="UTF-8"/>
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test"
                        userId="root"
                        password="222222">
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <javaModelGenerator targetPackage="model" targetProject="/Users/whuanghkl/code/mygit/mybatis/mybatis_demo/src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
 <!-- mapper xml文件 -->
        <sqlMapGenerator targetPackage="mapperxml" targetProject="/Users/whuanghkl/code/mygit/mybatis/mybatis_demo/src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

<!-- java 接口,将使用注解@Mapper -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="mapper" targetProject="/Users/whuanghkl/code/mygit/mybatis/mybatis_demo/src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <table tableName="Customer" domainObjectName="Customer" enableCountByExample="false"
               enableDeleteByExample="false" enableUpdateByExample="false" enableSelectByExample="false">
        </table>

    </context>
</generatorConfiguration>

上述文件中,须要修改spring

  1. java 接口路径
    经过标签javaClientGenerator 指定;
  2. xml mapper文件路径
    经过标签sqlMapGenerator指定
  3. 表名经过table 指定
  4. 实体类经过javaModelGenerator 指定

经过MybatisGenerator.xml 文件生成 mapper文件

编辑 MybatisGenerator.xml文件,填入真实的用户名,密码等,实体类package路径

将自动生成mapper文件:sql

<?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.didispace.domain.UserXmlMapper">
    <sql id="Base_Column_List">
	id,
	name,
	age
</sql>
    <resultMap id="BaseResultMap" type="com.didispace.domain.User">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
          This element was generated on Fri Jul 06 11:09:12 CST 2018.
        -->
        <result column="id" jdbcType="INTEGER" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="age" jdbcType="TINYINT" property="age"/>
    </resultMap>
    <insert id="insert">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
          This element was generated on Fri Jul 06 11:09:12 CST 2018.
        -->
        insert into USER (name, age
        )
        values ( #{name,jdbcType=VARCHAR}, #{age,jdbcType=TINYINT}
        )
    </insert>
    <insert id="insertSelective" parameterType="com.didispace.domain.User">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
          This element was generated on Fri Jul 06 11:09:12 CST 2018.
        -->
        insert into USER
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="age != null">
                age,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
            </if>
            <if test="age != null">
                #{age,jdbcType=TINYINT},
            </if>
        </trim>
    </insert>
    <select id="queryByName" resultType="com.didispace.domain.User">

    </select>

    <!--auto generated by codehelper on 2018-07-06 11:18:07-->
    <select id="findByName" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from USER
        where name=#{name,jdbcType=VARCHAR}
    </select>

    <!--auto generated by codehelper on 2018-07-06 11:57:36-->
    <delete id="deleteByNameAndAge">
	delete from  USER
	where name=#{name,jdbcType=VARCHAR} and age=#{age,jdbcType=TINYINT}</delete>

</mapper>

手动建立Service,与 UserMapper.xml对应

编辑spring boot全局配置文件application.properties

设置扫描mapper xml文件的路径:mybatis

mybatis.mapper-locations:classpath:mapper/*.xml

注意事项

mybatis 使用xml方式时,必定要在application.properties 中指定扫描 mapper文件的路径app

源代码

https://github.com/liuyu520/mybatis_demodom

参考:

https://blog.csdn.net/qq_35981283/article/details/78590090ide