MyBatisMybatis基本用法–下

第八有些 原理

参考: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

配置

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

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

另的布我们坐mybatis-config.xml

第七片段 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注解。

Mybatis基本用法–下

第九片插件

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-spring的动方法以此间呢基本适用。

网站地图xml地图