发表于: 2020-06-27 22:00:21
1 1736
今天完成的事情:今天听小课堂分享学习到日志的门面模式,自己配置dao和pojo自己生成的mybatis-generator工具。
明天计划的事情:明天一定要结束任务四
遇到的问题:问题还是有的。
1.IDEA中xml文件头报错:URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)
步骤如下:file-->settings...-->languages & frameworks --> Schemas and DTDs中增加报错的地址,就是把报错的地址复制进来就行。
2.Mybatis-generator运行报错,XML Parser Error on line 148: 元素类型为 "context" 的内容必须匹配 ",(property*,plugin*,commentGenerator?)
就是自己在xml配置文件中有错,多写个逗号,配置的属性放错位置等低级的书写错误造成。 idea对xml检测这种错误的机制比较弱,不能告诉你具体哪个位置报错了。只有自己对比正确的配置一个一个找。
收获:
日志门面模式(外观模式)
外观模式
意图:为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
主要解决:降低访问复杂系统的内部子系统时的复杂度,简化客户端与之的接口。
何时使用: 1、客户端不需要知道系统内部的复杂联系,整个系统只需提供一个"接待员"即可。 2、定义系统的入口。
如何解决:客户端不与系统耦合,外观类与系统耦合。
关键代码:在客户端和复杂系统之间再加一层,这一层将调用顺序、依赖关系等处理好。
应用实例: 1、去医院看病,可能要去挂号、门诊、划价、取药,让患者或患者家属觉得很复杂,如果有提供接待人员,只让接待人员来处理,就很方便。 2、JAVA 的三层开发模式。
优点: 1、减少系统相互依赖,解耦。 2、提高灵活性。 3、提高了安全性。
缺点:不符合开闭原则,如果要改东西很麻烦,继承重写都不合适。
日志门面
SLF4J其实只是一个门面服务而已,他并不是真正的日志框架,真正的日志的输出相关的实现还是要依赖Log4j、logback等日志框架的。
对于Java工程师来说,关于日志工具的使用,最佳实践就是在应用中使用如Log4j + SLF4J 这样的组合来进行日志输出。
这样做的最大好处,就是业务层的开发不需要关心底层日志框架的实现及细节,在编码的时候也不需要考虑日后更换框架所带来的成本。这也是门面模式所带来的好处。
对应的日志引用应该是这样
private Logger logger = LoggerFactory.getLogger(ArchivesLogAspect.class);
MyBatis Generator 详细配置-代码实践
我的目录结构,选中的都是生成的文件或者需要配置的文件。
1.数据库相关依赖配置,必须先配好。再配pom.xml中的MyBatis Generator 插件。
<!--mybatis-generator插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<!--mybatis的代码生成器的配置文件路径-->
<configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
<!--允许覆盖生成的文件-->
<overwrite>true</overwrite>
<!--将当前pom的依赖项添加到生成器的类路径中-->
<includeCompileDependencies>true</includeCompileDependencies>
</configuration>
</plugin>
2.配置mybatis-generator-config.xml的xml配置文件,就这是最重要的,而且注意千万别写错单词或者添加一个符号。
<?xml version="1.0" encoding="UTF-8" ?>
<!--mybatis的代码生成器相关配置-->
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<properties resource="db.properties"></properties>
<context id="myContext" targetRuntime="MyBatis3" defaultModelType="flat">
<commentGenerator>
<!-- 不希望生成的注释中包含时间戳 -->
<property name="suppressDate" value="true"/>
<!-- 添加 db 表中字段的注释,只有suppressAllComments为false时才生效-->
<property name="addRemarkComments" value="true"/>
</commentGenerator>
<!-- 添加jdbc连接 -->
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}">
<!--高版本的 mysql-connector-java 需要设置 nullCatalogMeansCurrent=true-->
<!-- <property name="nullCatalogMeansCurrent" value="true"/>-->
</jdbcConnection>
<!--生成实体类地址-->
<javaModelGenerator targetPackage="com.hyx.pojo" targetProject="src/main/java">
<!-- 是否让schema作为包的后缀,默认为false -->
<!--<property name="enableSubPackages" value="false"/>-->
<!-- 是否针对string类型的字段在set方法中进行修剪,默认false -->
<!-- <property name="trimStrings" value="true"/>-->
</javaModelGenerator>
<!--生成Mapper.xml文件-->
<sqlMapGenerator targetPackage="com.hyx.dao.mapper" targetProject="src/main/resources">
<!--<property name="enableSubPackages" value="false"/>-->
</sqlMapGenerator>
<!--生成mapper接口-->
<javaClientGenerator targetPackage="com.hyx.dao" targetProject="src/main/java" type="XMLMAPPER">
<!--<property name="enableSubPackages" value="false"/>-->
</javaClientGenerator>
<!-- schema为数据库名,oracle需要配置,mysql不需要配置。
tableName为对应的数据库表名
domainObjectName 是要生成的实体类名(可以不指定)
enableXXXByExample 默认为 true, 为 true 会生成一个对应Example帮助类,帮助你进行条件查询,不想要可以设为false
-->
<table schema="" tableName="student" domainObjectName="Student"
enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false">
<!--是否使用实际列名,默认为false-->
<!--<property name="useActualColumnNames" value="false" />-->
</table>
<table schema="test" tableName="profession" domainObjectName="Profession"
enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false">
<!--是否使用实际列名,默认为false-->
<!--<property name="useActualColumnNames" value="false" />-->
</table>
<table schema="" tableName="pro_catalog" domainObjectName="ProCatalog"
enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false">
<!--是否使用实际列名,默认为false-->
<!--<property name="useActualColumnNames" value="false" />-->
</table>
</context>
</generatorConfiguration>
配置好后,双击 maven 中的 MyBatis Generator 运行
力推这篇博客https://juejin.im/post/5db694e3e51d452a2e25ba45#heading-15
评论