Mybatis基本用法--下

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="srcmainjava">  
            <property name="enableSubPackages" value="true"/>  
            <property name="trimStrings" value="true"/>  
        </javaModelGenerator>  
        <!-- 生成映射文件的包名和位置-->  
        <sqlMapGenerator targetPackage="com.blog.mapper" targetProject="srcmain
esources">  
            <property name="enableSubPackages" value="true"/>  
        </sqlMapGenerator>  
        <!-- 生成DAO的包名和位置-->  
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.blog.mapper" targetProject="srcmainjava">  
            <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>  
原文地址:https://www.cnblogs.com/xzwblog/p/6801160.html