您的位置:首页 > 编程语言 > Java开发

springBoot 整合mybatis

2018-07-06 14:59 781 查看
## springBoot 整合mybatis
s使用IDEA开发,为了提高效率,
推荐使用插件:



说明:上述插件可以在低版本的IDEA中找到破解版

项目结构



说明:

mapper/UserMapper.xml 是与接口 对应的mapper文件

UserMapper :是接口,封装 DAO 操作的,类似于hibernate的UserDao

环境搭建步骤

生成generator文件



生成文件名称为:MybatisGenerator.xml

内容:

<?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>

上述文件中,需要修改

java 接口路径

通过标签javaClientGenerator 指定;

xml mapper文件路径

通过标签sqlMapGenerator指定

表名通过table 指定

实体类通过javaModelGenerator 指定

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

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



将自动生成mapper文件:

<?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.mapper-locations:classpath:mapper/*.xml




注意事项

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

源代码

https://github.com/liuyu520/mybatis_demo

参考:

https://blog.csdn.net/qq_35981283/article/details/78590090
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息