`
sillycat
  • 浏览: 2566970 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Java Generate/Merge Files(3)DAO Layer of Mybatis

    博客分类:
  • JAVA
 
阅读更多
Java Generate/Merge Files(3)DAO Layer of Mybatis

I used Ibatis for years, I used hibernate for years as well. But my friend recommend mybatis to me sometime ago. So this time I tried to use Mybatis for my ORM tool.

pom.xml provide us the dependencies
<!-- myIbatis -->
<dependency>
     <groupId>org.mybatis</groupId>
     <artifactId>mybatis</artifactId>
     <version>3.4.2</version>
</dependency>
<dependency>
     <groupId>org.mybatis</groupId>
     <artifactId>mybatis-spring</artifactId>
     <version>1.3.1</version>
</dependency>
<dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>6.0.6</version>
</dependency>
<dependency>
     <groupId>c3p0</groupId>
     <artifactId>c3p0</artifactId>
     <version>0.9.1.2</version>
</dependency>

Database related XML configuration and properties
<!-- DATABASE -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     <property name="dataSource" ref="dataSource" />
     <property name="configLocation" value="classpath:mybatis-config.xml" />
     <property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>

<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource" />
</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
     <!-- access -->
     <property name="driverClass" value="${db.driver}" />
     <property name="jdbcUrl" value="${db.url}" />
     <property name="user" value="${db.user}" />
     <property name="password" value="${db.password}" />
     <!-- pool sizing -->
     <property name="initialPoolSize" value="2" />
     <property name="minPoolSize" value="2" />
     <property name="maxPoolSize" value="20" />
     <property name="acquireIncrement" value="3" />
     <property name="maxStatements" value="0" />
     <!-- retries -->
     <property name="acquireRetryAttempts" value="30" />
     <property name="acquireRetryDelay" value="1000" /> <!-- 1s -->
     <property name="breakAfterAcquireFailure" value="false" />
     <!-- refreshing connections -->
     <property name="maxIdleTime" value="180" /> <!-- 3min -->
     <property name="maxConnectionAge" value="10" /> <!-- 1h -->
     <!-- timeouts and testing -->
     <property name="checkoutTimeout" value="5000" /> <!-- 5s -->
     <property name="idleConnectionTestPeriod" value="60" /> <!-- 60 -->
     <property name="testConnectionOnCheckout" value="true" />
     <property name="preferredTestQuery" value="SELECT 1" />
     <property name="testConnectionOnCheckin" value="true" />
</bean>

<bean id="sourceDAO" class="org.mybatis.spring.mapper.MapperFactoryBean" >
        <property name="mapperInterface" value="com.j2c.feeds2g.daos.SourceDAO" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="campaignDAO" class="org.mybatis.spring.mapper.MapperFactoryBean" >
        <property name="mapperInterface" value="com.j2c.feeds2g.daos.CampaignDAO" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

mybatis-config.xml file
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration  PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- namespace -->
<!--
<typeAliases>
<typeAlias alias="Source" type="com.j2c.feeds2g.models.Source" />
</typeAliases>
-->
<typeAliases>
  <package name="com.j2c.feeds2g.models"/>
</typeAliases>
</configuration>


Disable other logging
com.mchange.v2.log.MLog=com.mchange.v2.log.FallbackMLog
com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL=WARNING

Configuration logging
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://jobs-stage.xxxxx.amazonaws.com:3306/jobs
db.user=xxxxxx
db.password=xxxxxxx

Actually, I only defined a XML mapping which will map our java objects to our database column. And I defined a java interface which will give use the handler to call the DAO layer.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.j2c.feeds2g.daos.SourceDAO">
<resultMap type="com.j2c.feeds2g.models.Source" id="Source">
        <id column="id" property="id"  />
        <result property="status" column="status"/>
    </resultMap>

    <select id="loadActiveSources" resultMap="Source">
        SELECT id, status FROM job_sources
    </select>

    <!--
    <insert id="insertUser" parameterType="User">
        INSERT INTO users (firstName, lastName, email)
        VALUES (#{firstName}, #{lastName}, #{email})
    </insert>

    <update id="updateUser" parameterType="User">
        UPDATE users SET
            firstName = #{firstName},
            lastName = #{lastName},
            email = #{email}
        WHERE ID = #{id}
    </update>
    -->
</mapper>

package com.j2c.feeds2g.daos;

import java.util.List;

import com.j2c.feeds2g.models.Source;

public interface SourceDAO {

    public List<Source> loadActiveSources();

}

Then we only need to unit tests the class and ENV
package com.j2c.feeds2g.daos;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.j2c.feeds2g.models.Source;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:test-context.xml" })
public class SourceDAOTest {

    @Autowired
    @Qualifier("sourceDAO")
    private SourceDAO sourceDAO;

    @Test
    public void dummy() {
        Assert.assertTrue(true);
    }

    @Test
    public void loadActiveSources(){
        List<Source> results = sourceDAO.loadActiveSources();
        Assert.assertNotNull(results);
    }

}

References:
http://limingnihao.iteye.com/blog/781671

http://www.mybatis.org/spring/

https://blog.lanyonm.org/articles/2014/04/21/spring-4-mybatis-java-config.html

old sample
https://github.com/LTWangxi/spring-mb/tree/master/kdxr/src/main/resources/conf

c3p0 pool
https://gist.github.com/chrissom/2351568
alibaba pool
https://github.com/alibaba/druid
分享到:
评论

相关推荐

    generate mybatis ibatis artifacts

    MyBatis和iBatis是两个著名的Java持久层框架,它们简化了数据库操作与Java对象之间的映射。本文将深入探讨“generate mybatis ibatis artifacts”这个主题,它是一款为Eclipse开发的插件,旨在帮助开发者自动化创建...

    mybatis generator Java类方法生成

    mybatis generator Java类方法生成方法。附件是源码,可以直接运行,属于maven工程。 使用依赖: &lt;groupId&gt;mysql&lt;/groupId&gt; &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt; &lt;version&gt;5.1.45&lt;/version&gt; &lt;/...

    MybatisGenerate_代码生成_tkMybatis_mybatisgenerate_mybatis_

    3. **运行生成**:配置完成后,运行MybatisGenerate,它会根据你的配置自动扫描数据库中的表,并生成相应的代码文件。这一步骤通常是自动化的,避免了手动创建和编辑大量文件的工作。 4. **自定义扩展**:如果你有...

    MyBatis Generator

    mybatis也能方向生成代码,能方向生成实体类(po)、mapper接口和Mapper接口映射文件,能减少...4、右击generatorConfig.xml 点击Generate MyBatis/iBATIS Artifacts 生成对应接口、接口映射文件、实体类 5、查看结果

    IDEA逆向生成POJO/DAO/MAPPER 《Generate POJOs.groovy》

    dao和mapper 抛弃了mybatis插件生成的一个POJO一个mapper 很冗余的数据,这里直接生成增删改查3个dao/mapper对照,依托于IDEA-DataBase-Tools能够直接选取需要生成POJOS 表模型的选择器。可以很方便的生成pojo/dao/...

    Mybatis Generator自动生成Dao,Mapping,Pojo插件

    Mybatis Generator是一款强大的自动化工具,它能够帮助Java开发者自动生成DAO层、Mapper映射文件以及POJO对象,极大地提高了开发效率。通过配置XML文件,我们可以指定数据库连接信息、表名及字段,Generator会根据...

    jeecg-mybatis-generate 源码

    Jeecg-Mybatis-Generate的核心理念是“自动化”,它基于Mybatis的XML配置和Mapper接口,能够自动生成Service、DAO、Mapper、Model以及Controller等常见模块的代码。这个工具使得开发者可以快速搭建项目结构,节省...

    mybatis generator生成java实体类,带中文注释

    MyBatis Generator是一款强大的工具,它能够自动生成Java实体类、Mapper接口以及XML配置文件,大大简化了开发人员的工作量,特别是在处理大量的数据库表时。本教程将详细讲解如何使用MyBatis Generator来生成带有...

    mybatis 自动生成工具

    MyBatis Generator(MBG)是一款强大的自动化代码生成工具,专为MyBatis框架设计,极大地简化了开发过程中的DAO层编码工作。它可以根据数据库表信息自动生成Java实体类、Mapper接口及其XML配置文件,以及对应的DAO...

    mybatis-generate

    标题 "mybatis-generate" 指的是 MyBatis Generator,这是一个强大且高效的代码生成工具,用于自动生成 MyBatis 框架的 XML 映射文件、Java 模型类以及 DAO 接口。这个工具能够显著提高开发效率,减少手动编写重复...

    mybatis/ibatis自动生成SQLMapper脚本

    MyBatis/iBatis 是两个流行的数据访问框架,它们简化了Java开发中的数据库操作。iBatis 是早期的版本,而 MyBatis 是其后继者,提供了更加强大和灵活的功能。本教程将深入探讨如何使用MyBatis/iBatis自动生成SQL...

    mvn mybatis-generator:generate配置文件

    MyBatis Generator(MBG)是一个强大的工具,用于自动生成MyBatis的SQL映射文件、Java模型类和DAO接口。在使用MBG时,一个关键步骤是配置`mybatis-generator.xml`文件,这个文件包含了所有必要的信息来指导MBG如何...

    mybatisGenerate

    Mybatis Generate 是一个非常实用的工具,用于自动生成MyBatis框架中的DAO层接口和Mapper文件,极大地提高了开发效率。这个工具通过配置文件模板和指定的数据库表信息,能够自动化地创建与数据库表对应的Java实体类...

    mybatis自动生成代码和命令

    在Java中,可以创建`org.mybatis.generator.api.MyBatisGenerator`对象并调用`generate()`方法: ```java import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration...

    eclipse的maven项目中,通过mybatisgenerator工具自动生成实体类和Mapper

    在Java Web开发中,特别是使用MyBatis框架时,频繁地编写DAO层接口(Mapper)以及对应的实体类(Entity)是一项既繁琐又容易出错的工作。为了提高开发效率并减少错误,可以使用MyBatis Generator工具来自动生成这些...

    IDE mybatis自动生成配置和类信息(generateConfig)

    本主题将深入探讨如何在IDEA中实现MyBatis的自动代码生成(generateConfig),并介绍所需的相关工具和步骤。 一、MyBatis简介 MyBatis是一个优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。与传统的...

    mybatis generator使用方法

    MyBatis Generator是一款强大的代码生成工具,它可以自动根据数据库表生成对应的JavaBean、Mapper接口、以及XML映射文件。它极大地简化了MyBatis的开发工作,使得开发者不必再手写繁琐的映射文件和数据访问层代码。...

    demo1.rar springboot整合mybatis框架自动生成对数据库增删改查

    MyBatis Generator(MBG)是一个强大的工具,它可以自动生成Mapper接口、Mapper XML文件、实体类和DAO层的实现类。首先,创建一个`generatorConfig.xml`配置文件,如下: ```xml &lt;!DOCTYPE generatorConfiguration...

    MyBatis-Generate

    It will generate code for all versions of MyBatis, and versions of iBATIS after version 2.2.0. It will introspect a database table (or many tables) and will generate artifacts that can be used to ...

    mybatis-generator.zip(生成代码模板)

    MyBatis Generator(MBG)是一个强大的工具,用于自动生成Java源代码和XML配置文件,大大简化了开发人员的工作,特别是在处理与数据库交互时。它根据数据库中的表信息,能够生成DAO层、Model层以及Mapper XML文件,...

Global site tag (gtag.js) - Google Analytics