1创建项目,在启动类上添加映射扫描注解

2导入依赖,添加mybatis generator自动生成代码插件

  1. <!-- mybatis generator 自动生成代码插件 -->
  2. <plugin>
  3. <groupId>org.mybatis.generator</groupId>
  4. <artifactId>mybatis-generator-maven-plugin</artifactId>
  5. <version>1.3.2</version>
  6. <configuration>
  7. <configurationFile>${project.basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
  8. <overwrite>true</overwrite>
  9. <verbose>true</verbose>
  10. </configuration>
  11. </plugin>

 3创建generatorConfig.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6. <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
  7. <classPathEntry location="D:\jar\mysql-connector-java-5.1.0-bin.jar"/>
  8. <context id="default" targetRuntime="MyBatis3">
  9. <commentGenerator>
  10. <property name="suppressDate" value="true"/>
  11. <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  12. <property name="suppressAllComments" value="true"/>
  13. </commentGenerator>
  14. <!--数据库链接URL,用户名、密码 -->
  15. <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://url:3306/database" userId="username" password="passwrord">
  16. </jdbcConnection>
  17. <javaTypeResolver>
  18. <property name="forceBigDecimals" value="false"/>
  19. </javaTypeResolver>
  20. <!-- 生成模型的包名和位置-->
  21. <javaModelGenerator targetPackage="com.domain" targetProject="src/main/java">
  22. <property name="enableSubPackages" value="true"/>
  23. <property name="trimStrings" value="true"/>
  24. </javaModelGenerator>
  25. <!-- 生成映射文件的包名和位置-->
  26. <sqlMapGenerator targetPackage="com.mapper" targetProject="src/main/resources">
  27. <property name="enableSubPackages" value="true"/>
  28. </sqlMapGenerator>
  29. <!-- 生成DAO的包名和位置-->
  30. <javaClientGenerator type="XMLMAPPER" targetPackage="com.mapper" targetProject="src/main/java">
  31. <property name="enableSubPackages" value="true"/>
  32. </javaClientGenerator>
  33. <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
  34. <!--<table tableName="invitationc" domainObjectName="Invitationc" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
  35. <!--<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
  36. <!--<table tableName="leaving_a_message" domainObjectName="LeavingAMessage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
  37. <!--<table tableName="order_payment" domainObjectName="OrderPayment" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
  38. <!--<table tableName="sign_up" domainObjectName="SignUp" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
  39. <!--<table tableName="photo_order" domainObjectName="PhotoOrder" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
  40. <table tableName="commission_order" domainObjectName="CommissionOrder" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  41. <!--<table tableName="invitationc" domainObjectName="Invitationc" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
  42. <!--<table tableName="dynamic" domainObjectName="Dynamic" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
  43. <!--<table tableName="sys_dict" domainObjectName="SysDict" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
  44. <!--<table tableName="tag" domainObjectName="Tag" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
  45. <!--<table tableName="attention" domainObjectName="Attention" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
  46. <!--<table tableName="tab_collection" domainObjectName="TabCollection" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
  47. <!--<table tableName="sign_in" domainObjectName="SignIn" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
  48. </context>
  49. </generatorConfiguration>

  根据generatorConfig.xml文件生成的

模型:

映射文件:

DAO文件:

4、自定义查询语句

mapper

dao:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.idailycar.mapper.TabCollectionMapper" >
  4. <resultMap id="BaseResultMap" type="com.idailycar.domain.TabCollection" >
  5. <id column="id" property="id" jdbcType="BIGINT" />
  6. <result column="host_id" property="hostId" jdbcType="BIGINT" />
  7. <result column="collector_id" property="collectorId" jdbcType="BIGINT" />
  8. <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
  9. <result column="update_tine" property="updateTine" jdbcType="TIMESTAMP" />
  10. <result column="state" property="state" jdbcType="BIGINT" />
  11. </resultMap>
  12. <sql id="Base_Column_List" >
  13. id, host_id, collector_id, create_time, update_tine, state
  14. </sql>
  15. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
  16. select
  17. <include refid="Base_Column_List" />
  18. from tab_collection
  19. where id = #{id,jdbcType=BIGINT}
  20. </select>
  21. <select id="findByCollectorIdAndHostId" resultType="com.idailycar.domain.TabCollection">
  22. SELECT * from tab_collection where collector_id = #{collectorId} and host_id = #{hostId} and state = 16
  23. </select>
  24. <select id="findByCollectorIdAndHostIdAndUNCollection" resultType="com.idailycar.domain.TabCollection">
  25. SELECT * from tab_collection where collector_id = #{collectorId} and host_id = #{hostId} and state = 17
  26. </select>
  27. <select id="findCollectionInfoByOpenId" resultType="com.idailycar.controller.response.DynamicInfo">
  28. select d.id as dynamicId,u.id as userId,u.nickname as nickname,u.gender as gender,u.avatar_file as avatarFile,u.occupation as occupation,d.location as location,d.content as content,d.create_time as createTime from dynamic d INNER JOIN user u on d.creator_id = u.id where d.id in (select c.host_id from tab_collection c where c.collector_id = #{collectorId} and c.state = 16)
  29. </select>
  30. <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
  31. delete from tab_collection
  32. where id = #{id,jdbcType=BIGINT}
  33. </delete>
  34. <insert id="insert" parameterType="com.idailycar.domain.TabCollection" >
  35. insert into tab_collection (id, host_id, collector_id,
  36. create_time, update_tine, state
  37. )
  38. values (#{id,jdbcType=BIGINT}, #{hostId,jdbcType=BIGINT}, #{collectorId,jdbcType=BIGINT},
  39. #{createTime,jdbcType=TIMESTAMP}, #{updateTine,jdbcType=TIMESTAMP}, #{state,jdbcType=BIGINT}
  40. )
  41. </insert>
  42. <insert id="insertSelective" parameterType="com.idailycar.domain.TabCollection" >
  43. insert into tab_collection
  44. <trim prefix="(" suffix=")" suffixOverrides="," >
  45. <if test="id != null" >
  46. id,
  47. </if>
  48. <if test="hostId != null" >
  49. host_id,
  50. </if>
  51. <if test="collectorId != null" >
  52. collector_id,
  53. </if>
  54. <if test="createTime != null" >
  55. create_time,
  56. </if>
  57. <if test="updateTine != null" >
  58. update_tine,
  59. </if>
  60. <if test="state != null" >
  61. state,
  62. </if>
  63. </trim>
  64. <trim prefix="values (" suffix=")" suffixOverrides="," >
  65. <if test="id != null" >
  66. #{id,jdbcType=BIGINT},
  67. </if>
  68. <if test="hostId != null" >
  69. #{hostId,jdbcType=BIGINT},
  70. </if>
  71. <if test="collectorId != null" >
  72. #{collectorId,jdbcType=BIGINT},
  73. </if>
  74. <if test="createTime != null" >
  75. #{createTime,jdbcType=TIMESTAMP},
  76. </if>
  77. <if test="updateTine != null" >
  78. #{updateTine,jdbcType=TIMESTAMP},
  79. </if>
  80. <if test="state != null" >
  81. #{state,jdbcType=BIGINT},
  82. </if>
  83. </trim>
  84. </insert>
  85. <update id="updateByPrimaryKeySelective" parameterType="com.idailycar.domain.TabCollection" >
  86. update tab_collection
  87. <set >
  88. <if test="hostId != null" >
  89. host_id = #{hostId,jdbcType=BIGINT},
  90. </if>
  91. <if test="collectorId != null" >
  92. collector_id = #{collectorId,jdbcType=BIGINT},
  93. </if>
  94. <if test="createTime != null" >
  95. create_time = #{createTime,jdbcType=TIMESTAMP},
  96. </if>
  97. <if test="updateTine != null" >
  98. update_tine = #{updateTine,jdbcType=TIMESTAMP},
  99. </if>
  100. <if test="state != null" >
  101. state = #{state,jdbcType=BIGINT},
  102. </if>
  103. </set>
  104. where id = #{id,jdbcType=BIGINT}
  105. </update>
  106. <update id="updateByPrimaryKey" parameterType="com.idailycar.domain.TabCollection" >
  107. update tab_collection
  108. set host_id = #{hostId,jdbcType=BIGINT},
  109. collector_id = #{collectorId,jdbcType=BIGINT},
  110. create_time = #{createTime,jdbcType=TIMESTAMP},
  111. update_tine = #{updateTine,jdbcType=TIMESTAMP},
  112. state = #{state,jdbcType=BIGINT}
  113. where id = #{id,jdbcType=BIGINT}
  114. </update>
  115. </mapper>

  

方便在Dao接口和Mappper XML文件之间进行切换,下载free mybatis plugins插件,安装步骤:

springboot整合mybatis开发的更多相关文章

  1. springboot整合mybatis(SSM开发环境搭建)

    0.项目结构: ---------------------方法一:使用mybatis官方提供的Spring Boot整合包实现--------------------- 1.application.p ...

  2. Java开发学习(三十九)----SpringBoot整合mybatis

    一.回顾Spring整合Mybatis Spring 整合 Mybatis 需要定义很多配置类 SpringConfig 配置类 导入 JdbcConfig 配置类 导入 MybatisConfig ...

  3. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  4. SpringBoot整合Mybatis【非注解版】

    接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ​ 选择Spring Initializr,配置JDK版本 ​ 输入项目名 ​ 选择构建web项目所需的state ...

  5. springBoot整合mybatis、jsp 或 HTML

    springBoot整合mybatis.jsp Spring Boot的主要优点: 1:  为所有Spring开发者更快的入门: 2:  开箱即用,提供各种默认配置来简化项目配置: 3:  内嵌式容器 ...

  6. SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)

    1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...

  7. SpringBoot整合Mybatis完整详细版

    记得刚接触SpringBoot时,大吃一惊,世界上居然还有这么省事的框架,立马感叹:SpringBoot是世界上最好的框架.哈哈! 当初跟着教程练习搭建了一个框架,传送门:spring boot + ...

  8. 【SpringBoot系列1】SpringBoot整合MyBatis

    前言: 一直看网上说SpringBoot是解锁你的配置烦恼,一种超级快速开发的框架.一直挺想学的,正好最近也有时间,就学了下 这个是SpringBoot整合MyBatis的一个教程,用了阿里的drui ...

  9. SpringBoot整合Mybatis之进门篇

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

随机推荐

  1. Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3) C. Playing Piano

    题意:给出一个数列 a1 a2......an  让你构造一个序列(该序列取值(1-5)) 如果a(i+1)>a(i) b(i+1)>b(i) 如果a(i+1)<a(i)  那么b( ...

  2. LOJ子序列

    题目描述 https://loj.ac/problem/6074 题解 对于子序列的dp,我们可以设置一个dp. 我们设dp[i]表示以i这个字符结尾的子序列个数,转移为dp[i]+=∑dp[k]-d ...

  3. 【学习笔记】python

    1.  len( s )  返回对象(字符.列表.元祖等)的长度或项目个数. >>>str = "runoob" >>> len(str) # ...

  4. mac下安装android-sdk

    安装sdk brew cask install android-sdk 配置Path环境 vim ~/.bash_profile 设置路径 #Setting PATH for android-sdk ...

  5. MySQL8.0.19主从环境搭建(CentOS7)

    默认情况下,复制是异步的,从站不需要永久连接以接收来自主站的更新.根据配置,您可以复制数据库中的所有数据库,所选数据库甚至选定的表. MySQL中复制的优点包括: 横向扩展解决方案 - 在多个从站之间 ...

  6. sharding-jdbc学习

    sharding-jdbc的全局id生成策略是通过雪花算法来实现的. sharding-jdbc也是一个数据的中间件,可实现读写分离和分库分表,比mycat要简单些. nginx与ribbon实现负载 ...

  7. html中设置锚点定位

    1.使用id定位: (这样的定位可以针对任何标签来定位. ) <a name="1F" href="#1F">锚点1</a> <d ...

  8. laravel安装nova 运行php artisan migrate出错

    报错一$ php artisan migrate Illuminate\Database\QueryException : could not find driver (SQL: select * f ...

  9. ES6-Babel-Browserify使用教程

    ES6-Babel-Browserify使用教程 定义package.json文件 { "name" : "es6-babel-browserify", &qu ...

  10. BootstrapTable-导出数据

    要导出的数据:https://examples.bootstrap-table.com/json/data1.json?order=asc 使用的插件(注意插件版本依赖):tableExport.jq ...