通用mapper
1.业务需求
问题:如论是做什么操作,所有的sql必须自己手写,如果每天都编辑大量的重复的sql开发的效率太低
2.通用mapper实现
导入mapper插件
<!--
配置文件的顺序
Content Model : (properties?, settings?, typeAliases?, typeHandlers?, objectFactory?,
objectWrapperFactory?, reflectorFactory?, plugins?, environments?, databaseIdProvider?,
mappers?)
-->
<plugins>
<!-- 分页插件:com.github.pagehelper为PageHelper类所在包名 -->
<!-- <plugin interceptor="com.github.pagehelper.PageHelper">
方言
<property name="dialect" value="mysql" />
该参数默认为false
设置为true时,使用RowBounds分页会进行count查询,查询数据总条数
<property name="rowBoundsWithCount" value="true" />
</plugin> -->
<!-- 通用Mapper插件 -->
<plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
<!--主键自增回写方法,默认值MYSQL,详细说明请看文档 -->
<property name="IDENTITY" value="MYSQL" />
<!--通用Mapper接口该接口是扩展方法 -->
<property name="mappers" value="com.jt.common.mapper.SysMapper" />
</plugin>
</plugins>
2.关于mybatis中注解
/**
* Mybatis中的接口的注解形式,在开发中很少遇到
* 简单的单表的sql可以使用注解形式完成
* 要求:
* 1.注解形式和配置文件只能写一个
* @return
*/
//查询全部数据
/*
@Select("sql")
@Insert("sql")
@Update("sql")
@Delete("sql")
*/
List<User> findAll();
对象与表映射关系
问题:如何实现通用Mapper根据特定的业务查询指定的表?
接口扩展
接口文件继承SysMapper对接口方法进行扩展,实现自动化的对象关系映射.
三、关于log4j的日志
说明:因为导入了log4gjar包文件,在源码中有一段静态代码块,中标识了log4j的加载路径就在根目录下进行导入.并且名称为log4j.properties.
源码:
static public final String DEFAULT_CONFIGURATION_FILE = "log4j.properties";
static {
// By default we use a DefaultRepositorySelector which always returns 'h'.
Hierarchy h = new Hierarchy(new RootLogger((Level) Level.DEBUG));
repositorySelector = new DefaultRepositorySelector(h);
/** Search for the properties file log4j.properties in the CLASSPATH. */
String override =OptionConverter.getSystemProperty(DEFAULT_INIT_OVERRIDE_KEY,
null);
// if there is no default init override, then get the resource
// specified by the user or the default config file.
if(override == null || "false".equalsIgnoreCase(override)) {
String configurationOptionStr = OptionConverter.getSystemProperty(
DEFAULT_CONFIGURATION_KEY,
null);
String configuratorClassName = OptionConverter.getSystemProperty(
CONFIGURATOR_CLASS_KEY,
null);
URL url = null;
// if the user has not specified the log4j.configuration
// property, we search first for the file "log4j.xml" and then
// "log4j.properties"
if(configurationOptionStr == null) {
url = Loader.getResource(DEFAULT_XML_CONFIGURATION_FILE);
if(url == null) {
url = Loader.getResource(DEFAULT_CONFIGURATION_FILE);
}
} else {
try {
url = new URL(configurationOptionStr);
} catch (MalformedURLException ex) {
// so, resource is not a URL:
// attempt to get the resource from the class path
url = Loader.getResource(configurationOptionStr);
}
}
// If we have a non-null url, then delegate the rest of the
// configuration to the OptionConverter.selectAndConfigure
// method.
if(url != null) {
LogLog.debug("Using URL ["+url+"] for automatic log4j configuration.");
try {
OptionConverter.selectAndConfigure(url, configuratorClassName,
LogManager.getLoggerRepository());
} catch (NoClassDefFoundError e) {
LogLog.warn("Error during default initialization", e);
}
} else {
LogLog.debug("Could not find resource: ["+configurationOptionStr+"].");
}
} else {
LogLog.debug("Default initialization of overridden by " +
DEFAULT_INIT_OVERRIDE_KEY + "property.");
}
}