设为首页 收藏本站
查看: 680|回复: 0

[经验分享] 结合Spring使用Mybatis Generator生成的代码

[复制链接]

尚未签到

发表于 2016-11-26 09:11:13 | 显示全部楼层 |阅读模式
  本文将简要介绍怎样利用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">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:datasource.properties" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" 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"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- beware that mapper-config.xml is not needed if you use injected mappers -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--    <property name="configLocation" value="classpath:MapperConfig.xml" /> -->
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="petMapper1" class="org.mybatis.spring.MapperFactoryBean">
<!-- SqlSessionFactory property is autowired -->
<property name="mapperInterface" 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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-305708-1-1.html 上篇帖子: 【转】MyBatis+Spring整合详解教程 下篇帖子: springmvc与mybatis整合,log4j输出sql语句
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表