1, Mybatis简介
MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
2, Mybatis安装
将下面的 dependency 代码置于 pom.xml 文件中:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
3, Mybatis配置
/**
* 构建Mybatis所需要的数据源,事务,SqlSessionFactory,sqlSessionTemplate
*
* @author
*/
@Configuration
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer
{
@Override
public PlatformTransactionManager annotationDrivenTransactionManager()
{
return new DataSourceTransactionManager(dataSource());
}
/**
* 设置数据库的数据源
*
* @return DataSource
*/
@Bean(name = "dataSource")
public DataSource dataSource()
{
DruidDataSource DruidDataSource = new DruidDataSource();
DruidDataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
DruidDataSource.setUrl(dataSourceProperties.getUrl());
DruidDataSource.setUsername(dataSourceProperties.getUsername());
DruidDataSource.setPassword(dataSourceProperties.getPassword());
return DruidDataSource;
}
/**
* 用于构建SqlSessionFactory
*
* @return SqlSessionFactory
* @throws Exception
*/
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactoryBean() throws Exception
{
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource());
bean.setMapperLocations(resolver.getResources("classpath*:com/eversec/**/*.xml"));
return bean.getObject();
}
/**
* 通过 sqlSessionFactory 构建 SqlSessionTemplate
*
* @param sqlSessionFactory
* @return SqlSessionTemplate
*/
@Bean(name = "sqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory)
{
return new SqlSessionTemplate(sqlSessionFactory);
}
@Resource(name = "dataSourceProperties")
private DataSourceProperties dataSourceProperties;
}
/**
* 描述:配置Mapper
*
* @author
*
*/
@Configuration
@AutoConfigureAfter(MyBatisConfig.class)
public class MyBatisMapperScannerConfig
{
@Bean
public MapperScannerConfigurer mapperScannerConfigurer()
{
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
mapperScannerConfigurer.setBasePackage("com.eversec.**.sqlmapper");
return mapperScannerConfigurer;
}
}
4, Mybatis 反向生成代码
4.1 将下面的 plugin 代码置于 pom.xml 文件中:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
4.2 将generatorConfig.xml置于src/main/resource文件夹中
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:mysql-connector-java-5.1.39.jar" />
<context id="context1" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://IP:3306/database?useUnicode=true&characterEncoding=UTF-8"
userId="root" password="123456" />
<javaModelGenerator targetPackage="com.entity"
targetProject="D:workspacexxxxsrcmainjava" />
<sqlMapGenerator targetPackage="com.sqlmapper"
targetProject="D:workspacexxxxsrcmainjava" />
<javaClientGenerator targetPackage="com.sqlmapper"
targetProject="D:workspacexxxxsrcmainjava" type="XMLMAPPER" />
<table tableName="table" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
4.3 执行
右键pom.xml, 依次选择Run As >> Maven build... ,然后输入Goals中输入mybatis-generator:generate,最好点击Run按钮。
5, MyBatis的框架图
看到Mybatis的框架图,可以清晰的看到Mybatis的整体核心对象,我更喜欢用自己的图来表达Mybatis的整个的执行流程。如下图所示:
原理详解:
MyBatis应用程序根据XML配置文件创建SqlSessionFactory,SqlSessionFactory在根据配置,配置来源于两个地方,一处是配置文件,一处是Java代码的注解,获取一个SqlSession。SqlSession包含了执行sql所需要的所有方法,可以通过SqlSession实例直接运行映射的sql语句,完成对数据的增删改查和事务提交等,用完之后关闭SqlSession。
官网地址:http://www.mybatis.org/mybatis-3/zh/index.html