当前位置: 首页 > news >正文

springboot整合mybatis开发

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

 

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

<!-- mybatis generator 自动生成代码插件 -->
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.2</version>
				<configuration>
					<configurationFile>${project.basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
					<overwrite>true</overwrite>
					<verbose>true</verbose>
				</configuration>
			</plugin>

 3创建generatorConfig.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="D:\jar\mysql-connector-java-5.1.0-bin.jar"/>
    <context id="default"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://url:3306/database" userId="username" password="passwrord">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="com.domain" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="com.mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <!--<table tableName="invitationc" domainObjectName="Invitationc" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="leaving_a_message" domainObjectName="LeavingAMessage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="order_payment" domainObjectName="OrderPayment" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="sign_up" domainObjectName="SignUp" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="photo_order" domainObjectName="PhotoOrder" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <table tableName="commission_order" domainObjectName="CommissionOrder" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <!--<table tableName="invitationc" domainObjectName="Invitationc" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="dynamic" domainObjectName="Dynamic" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="sys_dict" domainObjectName="SysDict" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="tag" domainObjectName="Tag" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="attention" domainObjectName="Attention" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="tab_collection" domainObjectName="TabCollection" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="sign_in" domainObjectName="SignIn" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
    </context>
</generatorConfiguration>

  根据generatorConfig.xml文件生成的

模型:

映射文件:

DAO文件:

 4、自定义查询语句

 mapper

dao:

<?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.idailycar.mapper.TabCollectionMapper" >
  <resultMap id="BaseResultMap" type="com.idailycar.domain.TabCollection" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="host_id" property="hostId" jdbcType="BIGINT" />
    <result column="collector_id" property="collectorId" jdbcType="BIGINT" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_tine" property="updateTine" jdbcType="TIMESTAMP" />
    <result column="state" property="state" jdbcType="BIGINT" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, host_id, collector_id, create_time, update_tine, state
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select 
    <include refid="Base_Column_List" />
    from tab_collection
    where id = #{id,jdbcType=BIGINT}
  </select>
    <select id="findByCollectorIdAndHostId" resultType="com.idailycar.domain.TabCollection">
      SELECT * from  tab_collection where collector_id = #{collectorId} and host_id = #{hostId} and state = 16
    </select>
  <select id="findByCollectorIdAndHostIdAndUNCollection" resultType="com.idailycar.domain.TabCollection">
    SELECT * from  tab_collection where collector_id = #{collectorId} and host_id = #{hostId} and state = 17
  </select>
  <select id="findCollectionInfoByOpenId" resultType="com.idailycar.controller.response.DynamicInfo">
    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)
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
    delete from tab_collection
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" parameterType="com.idailycar.domain.TabCollection" >
    insert into tab_collection (id, host_id, collector_id, 
      create_time, update_tine, state
      )
    values (#{id,jdbcType=BIGINT}, #{hostId,jdbcType=BIGINT}, #{collectorId,jdbcType=BIGINT}, 
      #{createTime,jdbcType=TIMESTAMP}, #{updateTine,jdbcType=TIMESTAMP}, #{state,jdbcType=BIGINT}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.idailycar.domain.TabCollection" >
    insert into tab_collection
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="hostId != null" >
        host_id,
      </if>
      <if test="collectorId != null" >
        collector_id,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
      <if test="updateTine != null" >
        update_tine,
      </if>
      <if test="state != null" >
        state,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=BIGINT},
      </if>
      <if test="hostId != null" >
        #{hostId,jdbcType=BIGINT},
      </if>
      <if test="collectorId != null" >
        #{collectorId,jdbcType=BIGINT},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTine != null" >
        #{updateTine,jdbcType=TIMESTAMP},
      </if>
      <if test="state != null" >
        #{state,jdbcType=BIGINT},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.idailycar.domain.TabCollection" >
    update tab_collection
    <set >
      <if test="hostId != null" >
        host_id = #{hostId,jdbcType=BIGINT},
      </if>
      <if test="collectorId != null" >
        collector_id = #{collectorId,jdbcType=BIGINT},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTine != null" >
        update_tine = #{updateTine,jdbcType=TIMESTAMP},
      </if>
      <if test="state != null" >
        state = #{state,jdbcType=BIGINT},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.idailycar.domain.TabCollection" >
    update tab_collection
    set host_id = #{hostId,jdbcType=BIGINT},
      collector_id = #{collectorId,jdbcType=BIGINT},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_tine = #{updateTine,jdbcType=TIMESTAMP},
      state = #{state,jdbcType=BIGINT}
    where id = #{id,jdbcType=BIGINT}
  </update>
</mapper>

  

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

 

 

 

转载于:https://www.cnblogs.com/SongG-blogs/p/10784070.html

相关文章:

  • TypeScript+Webpack+React组件库开发采坑实记
  • gitlab的使用(待书写)
  • ssh免密码登陆设置时bad ownership or modes for file 报错的解决办法
  • ****** 二十三 ******、软设笔记【数据库】-数据操作-常用关系操作、关系运算
  • 模块讲解
  • 真·APIO2018滚粗记
  • 面试-线程池的成长之路
  • mysql建数据库的字符集与排序规则
  • “互联网+教育”时代 亚信安全助力江汉大学“安全”云化
  • Java多线程处理某个线程超时的问题
  • 学习网站
  • 下单快发货慢:一个 JOIN SQL 引起 SqlClient 读取数据慢的奇特问题
  • ES6之路之模块详解
  • [十二省联考]字符串问题
  • FFmpeg 硬件加速方案概览 (下)
  • Gradle 5.0 正式版发布
  • Hexo+码云+git快速搭建免费的静态Blog
  • Material Design
  • October CMS - 快速入门 9 Images And Galleries
  • Redis的resp协议
  • 闭包--闭包之tab栏切换(四)
  • 从地狱到天堂,Node 回调向 async/await 转变
  • 利用DataURL技术在网页上显示图片
  • 如何选择开源的机器学习框架?
  • 一些css基础学习笔记
  • 硬币翻转问题,区间操作
  • 用quicker-worker.js轻松跑一个大数据遍历
  • Java数据解析之JSON
  • 策略 : 一文教你成为人工智能(AI)领域专家
  • 没有任何编程基础可以直接学习python语言吗?学会后能够做什么? ...
  • 我们雇佣了一只大猴子...
  • ​ ​Redis(五)主从复制:主从模式介绍、配置、拓扑(一主一从结构、一主多从结构、树形主从结构)、原理(复制过程、​​​​​​​数据同步psync)、总结
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • #includecmath
  • #在 README.md 中生成项目目录结构
  • $().each和$.each的区别
  • (07)Hive——窗口函数详解
  • (floyd+补集) poj 3275
  • (Mirage系列之二)VMware Horizon Mirage的经典用户用例及真实案例分析
  • (备忘)Java Map 遍历
  • (多级缓存)缓存同步
  • (附源码)计算机毕业设计ssm基于Internet快递柜管理系统
  • (附源码)计算机毕业设计大学生兼职系统
  • (十五)Flask覆写wsgi_app函数实现自定义中间件
  • (四)docker:为mysql和java jar运行环境创建同一网络,容器互联
  • (学习日记)2024.03.12:UCOSIII第十四节:时基列表
  • (一)spring cloud微服务分布式云架构 - Spring Cloud简介
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (转)3D模板阴影原理
  • (转)linux自定义开机启动服务和chkconfig使用方法
  • (转)机器学习的数学基础(1)--Dirichlet分布
  • **Java有哪些悲观锁的实现_乐观锁、悲观锁、Redis分布式锁和Zookeeper分布式锁的实现以及流程原理...
  • .net 8 发布了,试下微软最近强推的MAUI
  • .net core 依赖注入的基本用发
  • .Net Core和.Net Standard直观理解