Mybatis基本用法--下

第七部分 mybatis-spring-boot-starter

官网:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/index.html#

maven依赖:

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

优势:

  1. 减少样板式代码
  2. 减少xml配置文件
  3. 构建独立应用程序

MyBatis-Spring-Boot-Starter可以:

  • 自动检测已存在数据源.
  • 通过上面的数据源创建并注册一个SqlSessionFactory实例
  • 从SqlSessionFactory创建并注册SqlSessionTemplate实例.
  • 自动扫描映射文件,链接到SqlSessionTemplate,并注册他们到Spring上下文,从而可以被依赖注入到你的beans.

实例:

@Mapper
public interface CityMapper { @Select("SELECT * FROM CITY WHERE state = #{state}")
City findByState(@Param("state") String state); }
@SpringBootApplication
public class BlogApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(BlogApplication.class);
} public static void main(String[] args) {
SpringApplication.run(BlogApplication.class, args);
}
}

一般接口的扫描用@Mapper注解即可。但对于自定义注解或标记接口需要用@MapperScan注解。

配置

在application.properties或 application.yml中,使用mybatis作为前缀

mybatis.config-location=classpath:mybatis-config.xml

其他的配置我们放到mybatis-config.xml

依赖



所以mybatis-spring的使用方法在这里也基本适用。

第八部分 原理

参考:http://blog.csdn.net/luanlouis/article/details/40422941

http://blog.csdn.net/isea533/article/details/44002219

第九部分插件

9.1分页插件

https://github.com/pagehelper/pagehelper-spring-boot

http://blog.csdn.net/isea533/article/details/25505413

9.2 MyBatis Generator

http://blog.csdn.net/isea533/article/details/42102297

<?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="mysql-connector-java-5.1.40-bin.jar"/>
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/myblog"
userId="root"
password="123">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.blog.model" targetProject="src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="com.blog.mapper" targetProject="src\main\resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.blog.mapper" targetProject="src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成哪些表-->
<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="blog" domainObjectName="Blog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="project" domainObjectName="Project" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>

Mybatis基本用法--下的更多相关文章

  1. Mybatis基本用法--中

    Mybatis基本用法--中 第四部分 动态 SQL 动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处理器相似.MyBatis 采用功能强大的基于 OGNL 的表达式来消除其他元素 ...

  2. Mybatis基本用法--上

    Mybatis基本用法--上 本文只是为自己查漏补缺.全面的请看官方文档,支持中英文 原理参考:http://blog.csdn.net/luanlouis/article/details/40422 ...

  3. SpringBoot示例教程(一)MySQL与Mybatis基础用法

    示例需求 在Springboot2框架中,使用Mysql和Mybatis功能:1. Mysql+Datasource集成2. Mybatis+XML用法详解 数据库准备 采用了Oracle中的scot ...

  4. Mybatis 结果映射下划线转驼峰

    mybatis 结果映射下划线转驼峰 Spring Boot 配置: #下划线转驼峰 mybatis.configuration.map-underscore-to-camel-case=true m ...

  5. mybatis基础(下)

    mybatis和spring整合 需要spring通过单例方式管理SqlSessionFactory spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSe ...

  6. Mybatis基本用法

    搭建mybatis环境 1, 导入需要的jar包 mybatis-*.*.*.jar ojdbc6.jar 2, 配置mybatis的总配置文件: mybatis-config.xml 配置根标签 & ...

  7. 面试官:精通 Mybatis?请回答下这几个问题

    点关注,不迷路:持续更新Java架构相关技术及资讯热文!!! Mybatis是现在非常主流的持久层框架,虽然平时用的多,但是其中几个细节的问题,能说出个所以然来不? 一.最常见,参数中 #{} 和 $ ...

  8. mybatis整合spring下的的各种配置文件

    1.applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

  9. mybatis简单用法

    1.resultType 和 resultMap 引言: MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表 ...

随机推荐

  1. Windows下用Composer安装Laravel步骤(集成php环境用phpStudy2016版本)

    描述:到官方网站了解并根据自己所需要的版本号安装,所需要的php版本是多少. 中文网站:http://www.golaravel.com/ 官方网站:https://laravel.com/ 其实各自 ...

  2. firebug离线安装方法-拖入法

    这里介绍的是如何在Firefox中离线安装firebug插件. 1, 下载firebug离线包, 一般就是一个*.xpi文件; 2, 打开Firefox浏览器,直接将*.xpi文件拖入Firefox浏 ...

  3. 初入ubuntu

    登入root :su root 安装 vim: sudo apt-get install vim 安装 gcc(g++):sudo apt-get install gcc(g++) 非常实用的修改分辨 ...

  4. Java中的HTTP通信技术详解

     1.使用HTTP的Get方式读取网络数据       class ReadByGet extends Thread{ @Override public void run(){ URL url = n ...

  5. Java线程池详解

    一.线程池初探 所谓线程池,就是将多个线程放在一个池子里面(所谓池化技术),然后需要线程的时候不是创建一个线程,而是从线程池里面获取一个可用的线程,然后执行我们的任务.线程池的关键在于它为我们管理了多 ...

  6. UI自动化测试(四)AutoIT工具使用和robot对象模拟键盘按键操作

    AutoIT简介 AutoIt 目前最新是v3版本,这是一个使用类似BASIC脚本语言的免费软件,它设计用于Windows GUI(图形用户界面)中进行自动化操作.它利用模拟键盘按键,鼠标移动和窗口/ ...

  7. Power Sum 竟然用原根来求

    Power Sum Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitS ...

  8. http://codeforces.com/contest/612/problem/D

    D. The Union of k-Segments time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  9. Centos7 创建本地 docker 仓库极其遇到的问题

    环境安装: VirtualBox 安装 Centos7 安装 docker 1. 配置私有仓库和客户端地址 私有仓库:192.168.1.104 客户端:192.168.1.103 通过 Centos ...

  10. 如何在maven pom.xml文件中设置Java编译器版本

    今天遇到一个问题: 在Eclipse中用maven创建一个新的web项目,然后再用maven update一下,则JDK版本自动变为1.5. 通过查找资料,终于发现maven编译器插件(Maven C ...