锁定老帖子 主题:MyBatis3新手上路
精华帖 (0) :: 良好帖 (2) :: 新手帖 (7) :: 隐藏帖 (4)
|
|
---|---|
作者 | 正文 |
发表时间:2011-05-17
chat 写道 from user where user_id=#userId# limit 1;
一看就是没有好好看mybatis文档,能跑起来才怪。 #userId#是ibatis2.×写法 #{userId}才是mybatis的写法 好吧 学到了 但是这么用这是OK的 不信你试试 |
|
返回顶楼 | |
发表时间:2011-05-17
我这里有个以前写的小笔记,或许对楼主有用哦:http://www.blogjava.net/javaren/archive/2010/09/27/mybatis_3_x_example.html
|
|
返回顶楼 | |
发表时间:2011-05-18
Mr.Chris 写道 andy20050125 写道 楼主有没有配置过myBatis 的log4j 文件?
纠结了很长时间,没弄出来,报错时不显示SQL XML code # Rules reminder: # DEBUG < INFO < WARN < ERROR < FATAL # Global logging configuration log4j.rootLogger=INFO, stdout ## Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} %5p %C: %m%n log4j.logger.org.apache.ibatis=DEBUG log4j.logger.java.sql=DEBUG log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG MyBatis的还没用过哦,但是你可以看看这个帖子—— 如何输出mybatis里的SQL?,或许对你有帮助哦……我明天去公司也试一下 纠结好几天了,还没弄出来,期待您的答复。 |
|
返回顶楼 | |
发表时间:2011-05-18
andy20050125 写道 Mr.Chris 写道 andy20050125 写道 楼主有没有配置过myBatis 的log4j 文件?
纠结了很长时间,没弄出来,报错时不显示SQL XML code # Rules reminder: # DEBUG < INFO < WARN < ERROR < FATAL # Global logging configuration log4j.rootLogger=INFO, stdout ## Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} %5p %C: %m%n log4j.logger.org.apache.ibatis=DEBUG log4j.logger.java.sql=DEBUG log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG MyBatis的还没用过哦,但是你可以看看这个帖子—— 如何输出mybatis里的SQL?,或许对你有帮助哦……我明天去公司也试一下 纠结好几天了,还没弄出来,期待您的答复。 log4j-1.2.13.jar commons-logging-1.1.1.jar commons-logging-api-1.0.4.jar slf4j-api-1.5.8.jar slf4j-log4j12-1.5.8.jar 这些jar 包都回进去了还是不行。 不报错时显示SQL,一报错就不显示SQL了, 而且报错时JSP 页面输出异常信息,控制台不输出异常信息。 同一个MyEclipse, 同一个Tomcat, 别的项目正常,就这个项目有问题, applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <!-- Copyright 2010 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. --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@**"/> <property name="username" value="**"/> <property name="password" value="**"/> <property name="maxActive" value="150"/> </bean> <!-- transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- enable component scanning (beware that this does not enable mapper scanning!) --> <context:component-scan base-package="com.apeksys.qms.service" /> <context:component-scan base-package="com.apeksys.qms.action" /> <!-- enable autowire <context:annotation-config /> --> <!-- enable transaction demarcation with annotations --> <tx:annotation-driven /> <aop:aspectj-autoproxy/> <!-- propagration 表示事务传播特性, 使用required 时表示:当检测到以create开关的方法时先看有没有 开启事务,如果开启事务则放进事务中去,如果没有则新建一个事务放进去; read-only="true" 数据库的只读属性,当该方法在读时,其它方法不能再去写,保证一个事务的完整性; --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="create*" propagation="REQUIRED" read-only="true" rollback-for="Throwable"/> <tx:method name="update*" propagation="REQUIRED" read-only="true" rollback-for="Throwable"/> <tx:method name="delete*" propagation="REQUIRED" read-only="true" rollback-for="Throwable"/> <tx:method name="import*" propagation="REQUIRED" read-only="true" rollback-for="Throwable"/> <!-- 除了上面的方法,其它方法全部是只读 --> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <aop:config proxy-target-class="true"> <!-- service 切入点 --> <aop:pointcut id="serviceMethods" expression="execution(* com.apeksys.qms.service.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/> </aop:config> <!-- define the SqlSessionFactory, notice that configLocation is not needed when you use MapperFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <!-- scan for mappers and let them be autowired --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.apeksys.qms.mapper.dao" /> </bean> <!-- <bean id="rootService" class="com.apeksys.qms.service.RootService"> <constructor-arg index="0" ref="sysUserInfoService"/> </bean> --> </beans> web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 节点加载顺序 context-param > listener > filter > servlet --> <display-name>Quanlity Management System</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- <listener> <listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class> </listener> --> <filter> <filter-name>loginFilter</filter-name> <filter-class>com.apeksys.qms.support.LoginFilter</filter-class> </filter> <filter-mapping> <filter-name>loginFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <servlet> <servlet-name>JSONRPCServlet</servlet-name> <servlet-class>com.metaparadigm.jsonrpc.JSONRPCServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>JSONRPCServlet</servlet-name> <url-pattern>/JSON-RPC</url-pattern> </servlet-mapping> <taglib> <taglib-uri>/page</taglib-uri> <taglib-location>/WEB-INF/pageTag.tld</taglib-location> </taglib> <!-- <error-page> <exception-type>java.lang.Exception</exception-type> <location>/system/error.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/system/error.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/system/error.jsp</location> </error-page> --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> 用到的Jar 包 aopalliance-1.0.jar asm-3.1.jar aspectjweaver-1.5.4.jar cglib-2.2.jar commons-beanutils-1.8.0.jar commons-cli-2.0-SNAPSHOT.jar commons-codec-1.3.jar commons-collections-3.2.1.jar commons-dbcp-1.3.jar commons-fileupload-1.2.1.jar commons-httpclient-3.0.1.jar commons-io-1.3.2.jar commons-lang-2.3.jar commons-logging-api-1.0.4.jar commons-pool-1.5.5.jar ezmorph-1.0.3.jar freemarker-2.3.16.jar javassist-3.4.ga.jar json-lib-2.1-jdk15.jar jsonrpc-1.0.jar jstl-1.2.jar junit-3.8.1.jar log4j-1.2.13.jar mybatis-3.0.4.jar mybatis-spring-1.0.0.jar ognl-3.0.jar org.springframework.aop-3.0.4.RELEASE.jar org.springframework.asm-3.0.4.RELEASE.jar org.springframework.aspects-3.0.4.RELEASE.jar org.springframework.beans-3.0.4.RELEASE.jar org.springframework.context-3.0.4.RELEASE.jar org.springframework.context.support-3.0.4.RELEASE.jar org.springframework.core-3.0.4.RELEASE.jar org.springframework.expression-3.0.4.RELEASE.jar org.springframework.jdbc-3.0.4.RELEASE.jar org.springframework.test-3.0.4.RELEASE.jar org.springframework.transaction-3.0.4.RELEASE.jar org.springframework.web-3.0.4.RELEASE.jar spring-tx-3.0.5.RELEASE.jar struts2-convention-plugin-2.2.1.jar struts2-core-2.2.1.jar struts2-json-plugin-2.2.1.jar struts2-junit-plugin-2.2.1.jar struts2-spring-plugin-2.2.1.jar xwork-core-2.2.1.jar ojdbc15.jar commons-logging-1.1.1.jar slf4j-api-1.5.8.jar slf4j-log4j12-1.5.8.jar |
|
返回顶楼 | |
发表时间:2011-05-18
最后修改:2011-05-18
andy20050125 写道 楼主有没有配置过myBatis 的log4j 文件?
纠结了很长时间,没弄出来,报错时不显示SQL 刚才试了一下,是OK的,我的依赖包是: <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.15</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.5.6</version> </dependency> log4j.properties # Rules reminder: # DEBUG < INFO < WARN < ERROR < FATAL # Global logging configuration log4j.rootLogger=INFO, stdout ## Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} %5p %C: %m%n log4j.logger.org.apache.ibatis=DEBUG log4j.logger.java.sql=DEBUG log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG |
|
返回顶楼 | |
发表时间:2011-05-18
要想出日志,应该这么配置。
#mybatis log4j.logger.com.ibatis=debug #log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug log4j.logger.java.sql.Connection=debug log4j.logger.java.sql.Statement=debug log4j.logger.java.sql.PreparedStatement=debug,Stdout |
|
返回顶楼 | |
发表时间:2011-05-18
我在做的时候,没有单独为Mybatis配置log4j,只是在我自己的文件中需要加入log输出,所以配置了log4j,但是在将log4j的log级别设为debug的时候,一样可以输出Mybatis的debug信息,当然包括sql
|
|
返回顶楼 | |
发表时间:2011-05-18
各位,麻烦帮忙解决下此问题:
http://topic.csdn.net/u/20110516/20/6eeb1cd8-ca01-4539-baf8-ddb1b45434f6.html |
|
返回顶楼 | |
发表时间:2011-05-18
Mr.Chris 写道 chat 写道 from user where user_id=#userId# limit 1;
一看就是没有好好看mybatis文档,能跑起来才怪。 #userId#是ibatis2.×写法 #{userId}才是mybatis的写法 好吧 学到了 但是这么用这是OK的 不信你试试 这还用试?我用了半年了 |
|
返回顶楼 | |
发表时间:2011-05-18
chat 写道 Mr.Chris 写道 chat 写道 from user where user_id=#userId# limit 1;
一看就是没有好好看mybatis文档,能跑起来才怪。 #userId#是ibatis2.×写法 #{userId}才是mybatis的写法 好吧 学到了 但是这么用这是OK的 不信你试试 这还用试?我用了半年了 谢谢chat的指点,我刚看了mybatis的源码,确实不再支持#name#的写法了。 在SqlSourceBuilder中,parse方法如下: public SqlSource parse(String originalSql, Class parameterType) { ParameterMappingTokenHandler handler = new ParameterMappingTokenHandler(configuration, parameterType); GenericTokenParser parser = new GenericTokenParser("#{", "}", handler); String sql = parser.parse(originalSql); return new StaticSqlSource(configuration, sql, handler.getParameterMappings()); } |
|
返回顶楼 | |