结合Spring使用Mybatis Generator生成的代码

本文介绍如何使用Spring框架整合MybatisGenerator自动生成的代码,包括环境搭建、Ant脚本运行、Spring配置等步骤。

本文将简要介绍怎样利用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包。

 

【EI复现】基于深度强化学习的微能源网能量管理与优化策略研究(Python代码实现)内容概要:本文围绕“基于深度强化学习的微能源网能量管理与优化策略”展开研究,重点利用深度Q网络(DQN)等深度强化学习算法对微能源网中的能量调度进行建模与优化,旨在应对可再生能源出力波动、负荷变化及运行成本等问题。文中结合Python代码实现,构建了包含光伏、储能、负荷等元素的微能源网模型,通过强化学习智能体动态决策能量分配策略,实现经济性、稳定性和能效的多重优化目标,并可能与其他优化算法进行对比分析以验证有效性。研究属于电力系统与人工智能交叉领域,具有较强的工程应用背景和学术参考价值。; 适合人群:具备一定Python编程基础和机器学习基础知识,从事电力系统、能源互联网、智能优化等相关方向的研究生、科研人员及工程技术人员。; 使用场景及目标:①学习如何将深度强化学习应用于微能源网的能量管理;②掌握DQN等算法在实际能源系统调度中的建模与实现方法;③为相关课题研究或项目开发提供代码参考和技术思路。; 阅读建议:建议读者结合提供的Python代码进行实践操作,理解环境建模、状态空间、动作空间及奖励函数的设计逻辑,同时可扩展学习其他强化学习算法在能源系统中的应用。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值