<!-- resource 表示 properties 文件路径,引用该文件的 property --> <properties resource="org/mybatis/example/config.properties"> <property name="username" value="dev_user"/> <property name="password" value="F2Fa3!33TYyg"/> </properties>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
<!-- 启用默认值 --> <property name="org.apache.ibatis.parsing.PropertyParser.enable-default-value" value="true"/>
<!-- 设置默认值分隔符 --> <property name="org.apache.ibatis.parsing.PropertyParser.default-value-separator" value=":"/>
<settings> <!-- 该配置影响的所有映射器中配置的缓存的全局开关 --> <setting name="cacheEnabled" value="true" /> <!-- 延迟加载的全局开关 --> <setting name="lazyLoadingEnabled" value="true" /> <!-- 是否允许单一语句返回多结果集(需要兼容驱动) --> <setting name="multipleResultSetsEnabled" value="true" /> <!-- 使用列标签代替列名 --> <setting name="useColumnLabel" value="true" /> <!-- 允许 JDBC 支持自动生成主键,需要驱动兼容 --> <setting name="useGeneratedKeys" value="false" /> <!-- 当检测出未知列(或未知属性)时,如何处理 --> <setting name="autoMappingUnknownColumnBehavior" value="WARNING" /> <!-- 配置默认的执行器 --> <setting name="defaultExecutorType" value="SIMPLE" /> <!-- 设置超时时间 --> <setting name="defaultStatementTimeout" value="25" /> <!-- 为驱动的结果集获取数量 --> <setting name="defaultFetchSize" value="100" /> <!-- 允许在嵌套语句中使用分页 --> <setting name="safeRowBoundsEnabled" value="false" /> <!-- 是否开启自动驼峰命名规则(camel case)映射,即从数据库列名 A_COLUMN 到经典 Java 属性名 aColumn 的类似映射 --> <setting name="mapUnderscoreToCamelCase" value="false" /> <!-- 利用本地缓存机制 --> <setting name="localCacheScope" value="SESSION" /> <!-- 当没有为参数提供特定的 JDBC 类型时,为空值指定 JDBC 类型 --> <setting name="jdbcTypeForNull" value="OTHER" /> <!-- 指定哪个对象的方法触发一次延迟加载 --> <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" /> </settings>
<typeAliases> <typeAlias alias="Author" type="domain.blog.Author"/> <typeAlias alias="Blog" type="domain.blog.Blog"/> <typeAlias alias="Comment" type="domain.blog.Comment"/> <typeAlias alias="Post" type="domain.blog.Post"/> <typeAlias alias="Section" type="domain.blog.Section"/> <typeAlias alias="Tag" type="domain.blog.Tag"/> </typeAliases>
<typeAliases> <package name="domain.blog"/> </typeAliases>
@Alias("author")
public class Author {
...
}
package org.mybatisExamples.simple;
import java.sql.*;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
// 继承基类重写方法,通过类型处理器的泛型,MyBatis 可以得知该类型处理器处理的 Java 类型
public class StringTypeHandler extends BaseTypeHandler<String> {
@Override
public String getNullableResult(ResultSet arg0, String arg1) throws SQLException {
System.out.printf("getNullableResult arg1=%s%n", arg1);
return arg0.getString(arg1);
}
@Override
public String getNullableResult(ResultSet arg0, int arg1) throws SQLException {
System.out.printf("getNullableResult arg1=%d%n", arg1);
return arg0.getString(arg1);
}
@Override
public String getNullableResult(CallableStatement arg0, int arg1) throws SQLException {
System.out.printf("getNullableResult arg1=%d%n", arg1);
return arg0.getString(arg1);
}
@Override
public void setNonNullParameter(PreparedStatement arg0, int arg1, String arg2, JdbcType arg3) throws SQLException {
System.out.printf("Index=%d Value=%s JdbcType=%s%n", arg1, arg2, arg3);
arg0.setString(arg1, arg2);
}
}
<typeHandlers> <typeHandler handler="org.mybatisExamples.simple.StringTypeHandler"/> </typeHandlers>
<typeHandlers> <typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="java.math.RoundingMode"/> </typeHandlers>
package org.mybatisExamples.simple;
import java.util.List;
import java.util.Properties;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
@SuppressWarnings("serial")
public class ExampleObjectFactory extends DefaultObjectFactory {
@Override
public <T> T create(Class<T> type) {
System.out.println("create is type=" + type.getName());
return super.create(type);
}
@Override
public <T> T create(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
System.out.println("create is type=" + type.getName());
return super.create(type, constructorArgTypes, constructorArgs);
}
@Override
public void setProperties(Properties properties) {
System.out.println("setProperties is properties=" + properties.toString());
super.setProperties(properties);
}
}
<objectFactory type=" org.mybatisExamples.simple.ExampleObjectFactory "> <property name="someProperty" value="100"/> </objectFactory>
@Intercepts({@Signature(
// type:表示需要拦截的上面列出的接口
type= Executor.class,
// method:表示拦截接口的方法
method = "update",
// args:表示拦截方法的参数
args = {MappedStatement.class,Object.class})})
public class ExamplePlugin implements Interceptor {
// 执行拦截对象的方法 invocation.proceed 表示执行原始方法
public Object intercept(Invocation invocation) throws Throwable {
return invocation.proceed();
}
// 将目标对象增加拦截器
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
// 设置拦截器的属性
public void setProperties(Properties properties) {
}
}
<plugins>
<plugin interceptor="org.mybatis.example.ExamplePlugin">
<property name="someProperty" value="100"/>
</plugin>
</plugins>
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environment); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environment,properties);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader,properties);
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="..." value="..."/>
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<transactionManager type="MANAGED"> <property name="closeConnection" value="false"/> </transactionManager>
public interface TransactionFactory {
void setProperties(Properties props);
Transaction newTransaction(Connection conn);
Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit);
}
public interface Transaction {
Connection getConnection() throws SQLException;
void commit() throws SQLException;
void rollback() throws SQLException;
void close() throws SQLException;
Integer getTimeout() throws SQLException;
}
public interface DataSourceFactory {
void setProperties(Properties props);
DataSource getDataSource();
}
import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0DataSourceFactory extends UnpooledDataSourceFactory {
public C3P0DataSourceFactory() {
this.dataSource = new ComboPooledDataSource();
}
}
<dataSource type="org.myproject.C3P0DataSourceFactory"> <property name="driverClass" value="com.mysql.jdbc.driver"/> <property name="jdbcUrl" value="jdbc:mysql//localhost:3306/mydb"/> <property name="user" value="dev"/> <property name="password" value="liyong"/> </dataSource>
<!-- Using classpath relative resources --> <mappers> <mapper resource="org/mybatis/builder/AuthorMapper.xml"/> <mapper resource="org/mybatis/builder/BlogMapper.xml"/> <mapper resource="org/mybatis/builder/PostMapper.xml"/> </mappers> <!-- Using url fully qualified paths --> <mappers> <mapper url="file:///var/mappers/AuthorMapper.xml"/> <mapper url="file:///var/mappers/BlogMapper.xml"/> <mapper url="file:///var/mappers/PostMapper.xml"/> </mappers> <!-- Using mapper interface classes --> <mappers> <mapper class="org.mybatis.builder.AuthorMapper"/> <mapper class="org.mybatis.builder.BlogMapper"/> <mapper class="org.mybatis.builder.PostMapper"/> </mappers> <!-- Register all interfaces in a package as mappers --> <mappers> <package name="org.mybatis.builder"/> </mappers>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有