mybatis是一款优秀的持久层框架,支持定制SQL语句,避免了几乎所有JDBC代码和手动设置参数,结果集获取
SqlSessionFactory build(InputStream inputStream)
SqlSessionFactory build(InputStream inputStream, String environment)
SqlSessionFactory build(InputStream inputStream, Properties properties)
SqlSessionFactory build(InputStream inputStream, String env, Properties props)
SqlSessionFactory build(Configuration config)
复制代码
//默认获取的方式,不自动提交(开启事务)
SqlSession openSession()
//是否自动提交
SqlSession openSession(boolean autoCommit)
SqlSession openSession(Connection connection)
//事务的隔离级别:None,RU,RC,RR,Serial
SqlSession openSession(TransactionIsolationLevel level)
//查询类型:simple,batch,reuse
SqlSession openSession(ExecutorType execType,TransactionIsolationLevel level)
SqlSession openSession(ExecutorType execType)
SqlSession openSession(ExecutorType execType, boolean autoCommit)
SqlSession openSession(ExecutorType execType, Connection connection)
//获取mybatis配置信息
Configuration getConfiguration();
复制代码
//带参数的增删改查方法
T selectOne(String statement, Object parameter)
List selectList(String statement, Object parameter)
Map selectMap(String statement, Object parameter, String mapKey)
int insert(String statement, Object parameter)
int update(String statement, Object parameter)
int delete(String statement, Object parameter)
//不带参数的增删改查方法
T selectOne(String statement)
List selectList(String statement)
Map selectMap(String statement, String mapKey)
int insert(String statement)
int update(String statement)
int delete(String statement)
//高级版本的增删该查方法,支持自定义返回行数和结果控制
List selectList (String statement, Object parameter, RowBounds rowBounds)
Map selectMap(String statement, Object parameter, String mapKey, RowBounds rowbounds)
void select (String statement, Object parameter, ResultHandler handler)
void select (String statement, Object parameter, RowBounds rowBounds, ResultHandler handler)
//事务控制相关方法
void commit()
void commit(boolean force)
void rollback()
void rollback(boolean force)
//清除缓存。mybatis提供了本地缓存和二级缓存
void clearCache()
//关闭session
void close()
//获得configuration实例
Configuration getConfiguration()
//获得映射器
T getMapper(Class type)
复制代码
所有支持的配置项介绍文档:mybatis配置文件说明,最外层标签为configuration,子标签有:
如果标准处理器满足不了需求,可自定义处理器选择性映射到某个JDBC类型,具体步骤:
映射文件定义了接口要执行的sql语句,所有支持的映射文件介绍文档:mybatis映射文件说明
动态SQL:用于根据条件包含where字句的一部分。动态SQL包括if,choose,trim,foreach
<select/insert id="xx">
select id, name, status
from table_name
where status = #{status}
<if test="name != null">
and name like #{name}
if>
<choose>
<when test="name != null">
when>
<otherwise>
otherwise>
choose>
<where>
where>
<trim prefix="WHERE" prefixOverrides="AND|OR">
trim>
<set>
<if>
set>
<foreach item="" index="" collection="">
foreach>
select>
复制代码
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
bean>
复制代码
"userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
"mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" />
"sqlSessionFactory" ref="sqlSessionFactory" />
复制代码
"org.mybatis.spring.mapper.MapperScannerConfigurer">
"basePackage" value="org.mybatis.spring.sample.mapper" />
复制代码
# application.properties
# 指定mybatis-config.xml文件的位置
mybatis.config-location=classpath:mybatis-config.xml
# 指定mapper的xml文件路径
mybatis.mapper-locations=classpath:com/xx
mybatis.executor-type=SIMPLE
# 指定别名的包,多个用逗号分开
mybatis.type-aliases-package=com.example.domain.model
mybatis.type-handlers-package=com.example.typehandler
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=30
Dao层都是是一些接口 它并没有实现类,为什么接口可以直接使用呢? 那是因为MyBbatis使用了JDK动态代理机制动态生成了代理类,那么代理类又是如何对SqlSession进行封装的呢?
java -jar mybatis-generator-core-x.x.x.jar -configfile \temp\generatorConfig.xml -overwrite
xml version="1.0" encoding="UTF-8"?>
<generatorConfiguration>
<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true" />
<property name="suppressAllComments" value="true"/>
commentGenerator>
<jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver"
connectionURL="jdbc:db2:TEST"
userId="db2admin"
password="db2admin">
jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
javaTypeResolver>
<javaModelGenerator targetPackage="test.model" targetProject="\MBGTestProject\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
javaModelGenerator>
<sqlMapGenerator targetPackage="test.xml" targetProject="\MBGTestProject\src">
<property name="enableSubPackages" value="true" />
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetPackage="xx" targetProject="\MBGTestProject\src">
<property name="enableSubPackages" value="true" />
javaClientGenerator>
<table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
table>
context>
generatorConfiguration>
复制代码