欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Mybatis中常用的约束文件

程序员文章站 2022-07-06 08:18:19
sqlmapconfig.xml的约束,也就是mybatis主配置文件的约束

sqlmapconfig.xml的约束,也就是mybatis主配置文件的约束

<?xml version="1.0" encoding="utf-8"?>
<!doctype configuration
    public "-//mybatis.org//dtd config 3.0//en"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">

持久层接口映射文件的约束

<?xml version="1.0" encoding="utf-8"?>
<!doctype mapper
    public "-//mybatis.org//dtd mapper 3.0//en"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

日志文件的配置log4j.properties

```bash
# set root category priority to info and its only appender to console.
# log4j.rootcategory=info, console debug info warn error fatal
log4j.rootcategory=debug, console, logfile 
# set the enterprise logger category to fatal and its only appender to console.
log4j.logger.org.apache.axis.enterprise=fatal, console 
# console is set to be a consoleappender using a patternlayout.
log4j.appender.console=org.apache.log4j.consoleappender 
log4j.appender.console.layout=org.apache.log4j.patternlayout 
log4j.appender.console.layout.conversionpattern=%d{iso8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n 
# logfile is set to be a file appender using a patternlayout.
log4j.appender.logfile=org.apache.log4j.fileappender 
#生成的日志文件的位置和命名
log4j.appender.logfile.file=d:\axis.log 
log4j.appender.logfile.append=true 
log4j.appender.logfile.layout=org.apache.log4j.patternlayout 
log4j.appender.logfile.layout.conversionpattern=%d{iso8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

ps:mybatis xml约束文件的使用

一:准备.dtd约束文件

核心配置文件约束文件:mybatis-config.dtd

<?xml version="1.0" encoding="utf-8" ?>
<!--
    copyright 2009-2012 the mybatis team
    licensed under the apache license, version 2.0 (the "license");
    you may not use this file except in compliance with the license.
    you may obtain a copy of the license at
     http://www.apache.org/licenses/license-2.0
    unless required by applicable law or agreed to in writing, software
    distributed under the license is distributed on an "as is" basis,
    without warranties or conditions of any kind, either express or implied.
    see the license for the specific language governing permissions and
    limitations under the license.
-->

<!element configuration (properties?, settings?, typealiases?, typehandlers?, objectfactory?, objectwrapperfactory?, plugins?, environments?, databaseidprovider?, mappers?)>

<!element databaseidprovider (property*)>
<!attlist databaseidprovider
type cdata #required
>

<!element properties (property*)>
<!attlist properties
resource cdata #implied
url cdata #implied
>

<!element property empty>
<!attlist property
name cdata #required
value cdata #required
>

<!element settings (setting+)>

<!element setting empty>
<!attlist setting
name cdata #required
value cdata #required
>

<!element typealiases (typealias*,package*)>

<!element typealias empty>
<!attlist typealias
type cdata #required
alias cdata #implied
>

<!element typehandlers (typehandler*,package*)>

<!element typehandler empty>
<!attlist typehandler
javatype cdata #implied
jdbctype cdata #implied
handler cdata #required
>

<!element objectfactory (property*)>
<!attlist objectfactory
type cdata #required
>

<!element objectwrapperfactory (property*)>
<!attlist objectwrapperfactory
type cdata #required
>

<!element plugins (plugin+)>

<!element plugin (property*)>
<!attlist plugin
interceptor cdata #required
>

<!element environments (environment+)>
<!attlist environments
default cdata #required
>

<!element environment (transactionmanager,datasource)>
<!attlist environment
id cdata #required
>

<!element transactionmanager (property*)>
<!attlist transactionmanager
type cdata #required
>

<!element datasource (property*)>
<!attlist datasource
type cdata #required
>

<!element mappers (mapper*,package*)>

<!element mapper empty>
<!attlist mapper
resource cdata #implied
url cdata #implied
class cdata #implied
>

<!element package empty>
<!attlist package
name cdata #required
>

sql映射文件约束文件:mybatis-mapper.dtd

<?xml version="1.0" encoding="utf-8" ?>
<!--

    copyright 2009-2011 the mybatis team

    licensed under the apache license, version 2.0 (the "license");
    you may not use this file except in compliance with the license.
    you may obtain a copy of the license at

     http://www.apache.org/licenses/license-2.0

    unless required by applicable law or agreed to in writing, software
    distributed under the license is distributed on an "as is" basis,
    without warranties or conditions of any kind, either express or implied.
    see the license for the specific language governing permissions and
    limitations under the license.

-->

<!element mapper (cache-ref | cache | resultmap* | parametermap* | sql* | insert* | update* | delete* | select* )+>
<!attlist mapper
xmlns:fo cdata #implied
namespace cdata #implied
>

<!element cache-ref empty>
<!attlist cache-ref
namespace cdata #required
>

<!element cache (property*)>
<!attlist cache
type cdata #implied
eviction cdata #implied
flushinterval cdata #implied
size cdata #implied
readonly cdata #implied
>

<!element parametermap (parameter+)?>
<!attlist parametermap
id cdata #required
type cdata #required
>

<!element parameter empty>
<!attlist parameter
property cdata #required
javatype cdata #implied
jdbctype cdata #implied
mode (in | out | inout) #implied
resultmap cdata #implied
scale cdata #implied
typehandler cdata #implied
>

<!element resultmap (constructor?,id*,result*,association*,collection*, discriminator?)>
<!attlist resultmap
id cdata #required
type cdata #required
extends cdata #implied
automapping (true|false) #implied
>

<!element constructor (idarg*,arg*)>

<!element id empty>
<!attlist id
property cdata #implied
javatype cdata #implied
column cdata #implied
jdbctype cdata #implied
typehandler cdata #implied
>

<!element result empty>
<!attlist result
property cdata #implied
javatype cdata #implied
column cdata #implied
jdbctype cdata #implied
typehandler cdata #implied
>

<!element idarg empty>
<!attlist idarg
javatype cdata #implied
column cdata #implied
jdbctype cdata #implied
typehandler cdata #implied
select cdata #implied
resultmap cdata #implied
>

<!element arg empty>
<!attlist arg
javatype cdata #implied
column cdata #implied
jdbctype cdata #implied
typehandler cdata #implied
select cdata #implied
resultmap cdata #implied
>

<!element collection (constructor?,id*,result*,association*,collection*, discriminator?)>
<!attlist collection
property cdata #required
column cdata #implied
javatype cdata #implied
oftype cdata #implied
jdbctype cdata #implied
select cdata #implied
resultmap cdata #implied
typehandler cdata #implied
notnullcolumn cdata #implied
columnprefix cdata #implied
>

<!element association (constructor?,id*,result*,association*,collection*, discriminator?)>
<!attlist association
property cdata #required
column cdata #implied
javatype cdata #implied
jdbctype cdata #implied
select cdata #implied
resultmap cdata #implied
typehandler cdata #implied
notnullcolumn cdata #implied
columnprefix cdata #implied
>

<!element discriminator (case+)>
<!attlist discriminator
column cdata #implied
javatype cdata #required
jdbctype cdata #implied
typehandler cdata #implied
>

<!element case (constructor?,id*,result*,association*,collection*, discriminator?)>
<!attlist case
value cdata #required
resultmap cdata #implied 
resulttype cdata #implied
>

<!element property empty>
<!attlist property
name cdata #required
value cdata #required
>

<!element typealias empty>
<!attlist typealias
alias cdata #required
type cdata #required
>

<!element select (#pcdata | include | trim | where | set | foreach | choose | if)*>
<!attlist select
id cdata #required
parametermap cdata #implied
parametertype cdata #implied
resultmap cdata #implied
resulttype cdata #implied
resultsettype (forward_only | scroll_insensitive | scroll_sensitive) #implied
statementtype (statement|prepared|callable) #implied
fetchsize cdata #implied
timeout cdata #implied
flushcache (true|false) #implied
usecache (true|false) #implied
databaseid cdata #implied
>

<!element insert (#pcdata | selectkey | include | trim | where | set | foreach | choose | if)*>
<!attlist insert
id cdata #required
parametermap cdata #implied
parametertype cdata #implied
timeout cdata #implied
flushcache (true|false) #implied
statementtype (statement|prepared|callable) #implied
keyproperty cdata #implied
usegeneratedkeys (true|false) #implied
keycolumn cdata #implied
databaseid cdata #implied
>

<!element selectkey (#pcdata | include | trim | where | set | foreach | choose | if)*>
<!attlist selectkey
resulttype cdata #implied
statementtype (statement|prepared|callable) #implied
keyproperty cdata #implied
order (before|after) #implied
databaseid cdata #implied
>

<!element update (#pcdata | include | trim | where | set | foreach | choose | if)*>
<!attlist update
id cdata #required
parametermap cdata #implied
parametertype cdata #implied
timeout cdata #implied
flushcache (true|false) #implied
statementtype (statement|prepared|callable) #implied
databaseid cdata #implied
>

<!element delete (#pcdata | include | trim | where | set | foreach | choose | if)*>
<!attlist delete
id cdata #required
parametermap cdata #implied
parametertype cdata #implied
timeout cdata #implied
flushcache (true|false) #implied
statementtype (statement|prepared|callable) #implied
databaseid cdata #implied
>

<!-- dynamic -->

<!element include empty>
<!attlist include
refid cdata #required
>

<!element sql (#pcdata | include | trim | where | set | foreach | choose | if)*>
<!attlist sql
id cdata #required
>

<!element trim (#pcdata | include | trim | where | set | foreach | choose | if)*>
<!attlist trim
prefix cdata #implied
prefixoverrides cdata #implied
suffix cdata #implied
suffixoverrides cdata #implied
>
<!element where (#pcdata | include | trim | where | set | foreach | choose | if)*>
<!element set (#pcdata | include | trim | where | set | foreach | choose | if)*>

<!element foreach (#pcdata | include | trim | where | set | foreach | choose | if)*>
<!attlist foreach
collection cdata #required
item cdata #implied
index cdata #implied
open cdata #implied
close cdata #implied
separator cdata #implied
>

<!element choose (when* , otherwise?)>
<!element when (#pcdata | include | trim | where | set | foreach | choose | if)*>
<!attlist when
test cdata #required
>
<!element otherwise (#pcdata | include | trim | where | set | foreach | choose | if)*>

<!element if (#pcdata | include | trim | where | set | foreach | choose | if)*>
<!attlist if
test cdata #required
>

二:在eclipse中使用xml约束文件
将约束文件,插入到xml文件中,两种约束文件的插入位置是相同的

核心配置文件:

<?xml version="1.0" encoding="utf-8"?>
<!-- .dtd文件是xml格式文件的约束文件,它约束了该xml文件使用的标签以及标签之间的关系 -->
<!doctype configuration
public "-//mybatis.org//dtd config 3.0//en"
"http://mybatis.org/dtd/mybatis-config.dtd">

sql映射文件:

<?xml version="1.0" encoding="utf-8"?>
<!-- 该文件代表的是类和表的映射关系,关于表的sql语句等等写在这里 -->
<!doctype mapper
public "-//mybatis.org//dtd mapper 3.0//en"
"http://mybatis.org/dtd/mybatis-mapper.dtd">

三:集成到eclipse开发工具中
window-> preferences->xml->xml catalog->add

然后

key:写入 xml文件中 public中的值

location:选择dtd文件的实际物理位置

总结

到此这篇关于mybatis中常用的约束文件的文章就介绍到这了,更多相关mybatis约束文件内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!