本文将简要介绍怎样利用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包。
分享到:
相关推荐
下载后,放到eclipse安装目录下的dropins文件夹中,重启eclipse。可以看到小丸子图标,或者在项目搭建菜单下...使用方法是,先选中项目,再点击插件,填写数据库信息,点击确定后,再刷新项目就可以看到生成的代码了。
这是结合了mybatis generator逆向工程的springboot项目,可以直接根据数据库表名来生成对应的dao和mapper类,具体的介绍可以参考这篇文章https://blog.csdn.net/yeyazhishang/article/details/80816130
mybatis springmvc 框架 微信小例子 mybatisgenerator生成类
找到一个挺好用的代码生成工具,不依赖于项目,简单配置一下就可以使用。直接点击 generator.bat 就可以用
今天将表建好了,但是一个一个的建实体类、Mapper接口、Mapper.xml文件就十分的麻烦,所以我就想到了MyBatis逆向,今天就操作一把!
1、设计数据库:设计好表结构,最好符合3NF,使用Generator自动生成Mybatis相关表信息 2、创建Maven项目,按需映入Maven包依赖-->配置文件:pom.xml 3、搭建Spring配置:spring.xml(扫描service),config....
1.Mybatis Plus代码生成器 1.1.自定义配置模板 1.2.自定义表-实体映射信息 1.3.多数据源管理 1.4.操作记忆功能 1.5.操作化繁为简 2.工程化(maven,spring boot) 2.1.生成maven工程 2.2.生成...
MVC框架springmvc+spring+mybatis附带有MySQL的代码文件,还有mybatis的生成插件generator及生成方法,代码的联表的增删改查
SpringBoot2.1集成Mybatis Generator 自动生成DAO 代码 使用之前需要在本地建立测试数据库 使用教程 https://blog.csdn.net/sw69366/article/details/88713392
MyBatis generator配合我其他资源框架使用。欢迎下载。
使用myeclipse搭建的spring+mybatis 使用mysql数据库数据库比较简单自己就可以写!包含了整合时所需的jar包;自己测试过能出现代码模块和实体类是使用mybatis-generator-tool工具自动生成的!有兴趣的可以下载参考!
可自动生成sql的工具,包括xml(mapper), dao层接口,bean实体;含盖通用的增删改查方法; 本工具的优势:不用集成在...压缩包中有详细的使用说明,配上数据库连接,表名,待生成的代码包路径;使用方便,傻瓜式操作!
本工具基于mybatis-generator-1.3.2进行修改,修改内容如下: 1、增加Dao配套的Service接口和实现类。 2、去掉Example相关的类,只保留基础的增删改查方法个代码,减少代码量更清爽。 3、常用配置项外置到config....
Dubbo-Spring-Mybatis-Generator Ability (quickly build Dubbo-Spring-Mybatis rpc project in one minute) - 快速生成spring-boot-duboo-rpc 服务框架模板代码 拒绝copy代码带来的坑 提高开发效率 - 生成的代码...
包内提供 spring + mybatis 所需要的先关jar 和 xml 生成插件 包括mybatis-3.1.1-bundle.zip mybatis-generator-core-1.3.2-bundle.zip mybatis-spring-1.1.1-bundle.zip
Spring-generator是基于javafx8开发的图形界面Spring代码生成器,使用 Apache FreeMarker 作为代码文件的模板,用户可以一键将数据库中的表生成为任意风格的.java代码文件。该工具支持所有实现JDBC规范的数据库;默认...
spring boot快速上手示例,集成了swagger+knife4j接口文档,集成mybatis-plus持久化,统一的数据返回封装,集成mybatis-plus-generator代码生成器,加入了lombok框架,一个完成的sping boot 示例项目,简单易上手
Spring boot +Mybatis generator 自动生成实体类。 XXX
里面有springjdbc的使用,在配置文件中还包含了一份详细的mybatis-generator配置以及自动生成的mybatis代码,加快开发的效率