结合Spring使用Mybatis Generator生成的代码
本文将简要介绍怎样利用Spring 整合 Mybatis Generator自动生成的代码:
?
关于Mybatis Generator自动生成怎样自动生成代码,请参考这篇文章:使用Mybatis Generator自动生成Mybatis相关代码,本篇文章将接着上一篇文章的例子继续。
一、准备环境
1. 下载jar包:首先要在Mybatis网站中下载相应的 jar包mybatis-spring-1.0.0-RC2-bundle.zip http://code.google.com/p/mybatis/downloads/list?can=3&q=Product%3DSpring
另外当然还需要Spring的jar包,本文中用到版本的是3.0.4
?
2. 添加jar包:要使用mybatis-spring-1.0.0-RC1.jar,除了要在构建路径上添加jdbc包、Mybatis的包外,还需要添加Spring的asm, beans, context, core, expression, jdbc, transaction这几个包,当然还要包括apache-commons-logging。
?
二、运行 mybatis-generator
为了方便运行,在本示例中将Mybatis Generator的代码生成用ant脚本的方式给出:
<?xml version="1.0" encoding="UTF-8"?><project default="genfiles" basedir="."><target name="genfiles" description="Generate the files"><taskdef name="mbgenerator" classname="org.mybatis.generator.ant.GeneratorAntTask"classpath="mybatis-generator-core-1.3.0.jar" /><mbgenerator overwrite="true" configfile="src/main/resource/config.xml"verbose="false"></mbgenerator></target></project>?
不过在运行示例时请修改config.xml中jdbc jar包的位置,因为示例中使用的是绝对路径,而没有成功的转成相对路径,若有谁成功的转成相对路径的话,请留言相教,谢谢。
?
三、使用 mybatis-generator 生成的代码
在使用Mybatis Generator自动生成Mybatis相关代码的文章中,我还不明白为什么要手写一个关于所有映射的配置文件。使用Spring后我知道了,原来在Spring中使用mybatis-generator 生成的代码,根本就不需要这个配置文件,只要在Spring的context文件中配置相应的信息即可。
?
下面是context配置示例:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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" default-autowire="byName"><beanvalue="classpath:datasource.properties" /></bean><bean id="dataSource" value="${jdbc.driverClassName}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean> <bean id="transactionManager" ref="dataSource" /> </bean> <!-- beware that mapper-config.xml is not needed if you use injected mappers --> <bean id="sqlSessionFactory" value="classpath:MapperConfig.xml" /> --> <property name="dataSource" ref="dataSource" /> </bean> <bean id="petMapper1" value="test.dao.PetMapper" /> </bean></beans>?可以看到sqlSessionFactory bean中的configLocation属性被注释掉了,不过丝毫不影响对相应dao接口的使用。我们只需要编写下面的几行代码就已达到上篇文章示例中的相同效果:
package test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import test.dao.PetMapper;import test.model.PetExample;public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("context.xml"); PetExample pet = new PetExample(); pet.or().andDeathIsNotNull(); PetMapper map = (PetMapper) context.getBean("petMapper1"); System.out.println(map.selectByExample(pet)); }}?这里的给出的例子是结合Mybatis Generator自动生成的代码尽量少的手写代码的例子,但是灵活性上可能有不足。Mybatis的网站上提供了另外三种结合Spring使用的示例,详见 http://code.google.com/p/mybatis/source/browse/#svn/sub-projects/mybatis-spring/trunk/src/test 这里就不再赘述了。
?
四、小结
该示例的完整的Eclipse工程见附件mybatis-generator-usage2.zip,其中已经包含了示例需要使用的jar包。
?
1 楼 moment52 2011-09-29 您好,我想问你一个问题,我也是用MBG来生成代码的,我看英文的帮助文档说在你说MBG生成的代码后使用添加和更新方法完成后会返回你添加或修改的信息的ID,但我在使用添加方法的时候怎么都是返回1,这是怎么回事呢?是不是我用spring的AOP,数据虽然添加了,但还在内存中,所以拿不到ID或者拿到的都是1呢? 2 楼 tywo45 2011-10-22 是org.mybatis.spring.mapper.MapperFactoryBean,而不是org.mybatis.spring.MapperFactoryBean