## springBoot 整合mybatis s使用IDEA開發,爲了提升效率, 推薦使用插件: java
說明:上述插件能夠在低版本的IDEA中找到破解版mysql
說明:
mapper/UserMapper.xml 是與接口 對應的mapper文件
UserMapper :是接口,封裝 DAO 操做的,相似於hibernate的UserDaogit
生成文件名稱爲: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
編輯 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>
設置掃描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