在maven工程中需要的pom.xm配置如下:
4.0.0
com.music
mybatishomework
war
0.0.1-SNAPSHOT
mybatishomework Maven Webapp
"="http://maven.apache.org;
<spring.version>4.0.8.RELEASE</spring.version>
<jdk.version>1.7</jdk.version>
<tomcat.version>2.2</tomcat.version>
<jetty.version>8.1.16.v20140903</jetty.version>
<webserver.port>8181</webserver.port>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
若是web项目,需要在web.xml配置:
<?xml version="1.0" encoding="UTF-8"?> mybatishomework index.html index.jsp default.html default.htm default.jsp contextConfigLocation classpath*:/Application.xml org.springframework.web.context.ContextLoaderListener org.springframework.web.context.ContextCleanupListener org.springframework.web.context.request.RequestContextListener example org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath*:/spring-mvc.xml 1 example /spring-mvc配置文件如下:
<?xml version="1.0" encoding="UTF-8"?><context:component-scan base-package=“com.dao”>
<context:include-filter type=“annotation” expression=“org.springframework.stereotype.Controller”/>
</context:component-scan>
mvc:annotation-driven</mvc:annotation-driven>
mvc:default-servlet-handler/
Application.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<context:component-scan base-package=“com.dao”>
<context:exclude-filter type=“annotation”
expression=“org.springframework.stereotype.Controller” />
</context:component-scan>
aop:aspectj-autoproxy</aop:aspectj-autoproxy>
<context:property-placeholder location=“classpath:myjdbc.properties” />
<tx:annotation-driven transaction-manager=“txtManager” />
在mybatis框架中有个默认xml配置文件,为了尊重前贤,我们不做任何处理,只是建立此xml文件(mybatis-config.xml):
<?xml version="1.0" encoding="UTF-8" ?>对应的pojo可写如下配置文件,里面主要为动态sql:
<?xml version="1.0" encoding="UTF-8" ?>id,name,pwd,account
select count(*) from employee where name=#{name} and pwd=#{pwd}
insert into employee() values(#{id},#{name},#{pwd},#{account})
当然,此外还有些小配置,如数据库连接,log4j配置输出,如下:
myjdbc.properties:
driver:oracle.jdbc.driver.OracleDriver
url:jdbc:oracle:thin:@172.16.110.53:1521:orcl
user:wanwujiedao
password:1221xu
log4j.properties:
log4j.rootLogger=INFO,CONSOLE,FILE
#DEBUG,CONSOLE,FILE,ROLLING_FILE,MAIL,DATABASE
log4j.addivity.org.apache=true
###################
Console Appender
###################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=INFO
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
#ä¸æ–‡è¦å…ˆè¿›è¡Œunicodeç¼–ç
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} -%-4r [%t] %-5p %x - %m%n
#log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n
#####################
File Appender
#####################
log4j.appender.FILE.Threshold=INFO
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=E:/log/spring.log
log4j.appender.FILE.Append=true
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d - %c -%-4r [%t] %-5p %x - %m%n
Use this layout for LogFactor 5 analysis
########################
Rolling File
########################
log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLING_FILE.Threshold=ERROR
log4j.appender.ROLLING_FILE.File=rolling.log
log4j.appender.ROLLING_FILE.Append=true
log4j.appender.ROLLING_FILE.MaxFileSize=10KB
log4j.appender.ROLLING_FILE.MaxBackupIndex=1
log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
####################
Socket Appender
####################
log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender
log4j.appender.SOCKET.RemoteHost=localhost
log4j.appender.SOCKET.Port=5001
log4j.appender.SOCKET.LocationInfo=true
Set up for Log Facter 5
log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout
log4j.appender.SOCET.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%
t[THREAD]%n%c[CATEGORY]%n%m[MESSAGE]%n%n
########################
Log Factor 5 Appender
########################
log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender
log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000
########################
SMTP Appender
#######################
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.Threshold=FATAL
log4j.appender.MAIL.BufferSize=10
log4j.appender.MAIL.From=chenyl@hollycrm.com
log4j.appender.MAIL.SMTPHost=mail.hollycrm.com
log4j.appender.MAIL.Subject=Log4J Message
log4j.appender.MAIL.To=chenyl@hollycrm.com
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
########################
JDBC Appender
#######################
#log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender
#log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test
#log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver
#log4j.appender.DATABASE.user=root
#log4j.appender.DATABASE.password=
#log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES (’[framework] %d - %c -%-4r
#[%t] %-5p %c %x - %m%n’)
#log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
#log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File=SampleMessages.log4j
log4j.appender.A1.DatePattern=yyyyMMdd-HH’.log4j’
log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout