- 浏览: 2566970 次
- 性别:
- 来自: 成都
-
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
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
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
发表评论
-
Update Site will come soon
2021-06-02 04:10 1694I am still keep notes my tech n ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 439Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 449Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 392Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 475VPN Server 2020(2)Docker on Cen ... -
Nginx Deal with OPTIONS in HTTP Protocol
2020-02-15 01:33 373Nginx Deal with OPTIONS in HTTP ... -
PDF to HTML 2020(1)pdftohtml Linux tool or PDFBox
2020-01-29 07:37 435PDF to HTML 2020(1)pdftohtml Li ... -
Elasticsearch Cluster 2019(2)Kibana Issue or Upgrade
2020-01-12 03:25 743Elasticsearch Cluster 2019(2)Ki ... -
Spark Streaming 2020(1)Investigation
2020-01-08 07:19 307Spark Streaming 2020(1)Investig ... -
Hadoop Docker 2019 Version 3.2.1
2019-12-10 07:39 310Hadoop Docker 2019 Version 3.2. ... -
MongoDB 2019(3)Security and Auth
2019-11-16 06:48 254MongoDB 2019(3)Security and Aut ... -
MongoDB 2019(1)Install 4.2.1 Single and Cluster
2019-11-11 05:07 303MongoDB 2019(1) Follow this ht ... -
Monitor Tool 2019(1)Monit Installation and Usage
2019-10-17 08:22 336Monitor Tool 2019(1)Monit Insta ... -
Ansible 2019(1)Introduction and Installation on Ubuntu and CentOS
2019-10-12 06:15 326Ansible 2019(1)Introduction and ... -
Timezone and Time on All Servers and Docker Containers
2019-10-10 11:18 348Timezone and Time on All Server ... -
Kafka Cluster 2019(6) 3 Nodes Cluster on CentOS7
2019-10-05 23:28 298Kafka Cluster 2019(6) 3 Nodes C ... -
K8S Helm(1)Understand YAML and Kubectl Pod and Deployment
2019-10-01 01:21 343K8S Helm(1)Understand YAML and ... -
Rancher and k8s 2019(5)Private Registry
2019-09-27 03:25 382Rancher and k8s 2019(5)Private ... -
Jenkins 2019 Cluster(1)Version 2.194
2019-09-12 02:53 461Jenkins 2019 Cluster(1)Version ... -
Redis Cluster 2019(3)Redis Cluster on CentOS
2019-08-17 04:07 384Redis Cluster 2019(3)Redis Clus ...
相关推荐
MyBatis和iBatis是两个著名的Java持久层框架,它们简化了数据库操作与Java对象之间的映射。本文将深入探讨“generate mybatis ibatis artifacts”这个主题,它是一款为Eclipse开发的插件,旨在帮助开发者自动化创建...
mybatis generator Java类方法生成方法。附件是源码,可以直接运行,属于maven工程。 使用依赖: <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> </...
3. **运行生成**:配置完成后,运行MybatisGenerate,它会根据你的配置自动扫描数据库中的表,并生成相应的代码文件。这一步骤通常是自动化的,避免了手动创建和编辑大量文件的工作。 4. **自定义扩展**:如果你有...
mybatis也能方向生成代码,能方向生成实体类(po)、mapper接口和Mapper接口映射文件,能减少...4、右击generatorConfig.xml 点击Generate MyBatis/iBATIS Artifacts 生成对应接口、接口映射文件、实体类 5、查看结果
dao和mapper 抛弃了mybatis插件生成的一个POJO一个mapper 很冗余的数据,这里直接生成增删改查3个dao/mapper对照,依托于IDEA-DataBase-Tools能够直接选取需要生成POJOS 表模型的选择器。可以很方便的生成pojo/dao/...
Mybatis Generator是一款强大的自动化工具,它能够帮助Java开发者自动生成DAO层、Mapper映射文件以及POJO对象,极大地提高了开发效率。通过配置XML文件,我们可以指定数据库连接信息、表名及字段,Generator会根据...
Jeecg-Mybatis-Generate的核心理念是“自动化”,它基于Mybatis的XML配置和Mapper接口,能够自动生成Service、DAO、Mapper、Model以及Controller等常见模块的代码。这个工具使得开发者可以快速搭建项目结构,节省...
MyBatis Generator是一款强大的工具,它能够自动生成Java实体类、Mapper接口以及XML配置文件,大大简化了开发人员的工作量,特别是在处理大量的数据库表时。本教程将详细讲解如何使用MyBatis Generator来生成带有...
MyBatis Generator(MBG)是一款强大的自动化代码生成工具,专为MyBatis框架设计,极大地简化了开发过程中的DAO层编码工作。它可以根据数据库表信息自动生成Java实体类、Mapper接口及其XML配置文件,以及对应的DAO...
标题 "mybatis-generate" 指的是 MyBatis Generator,这是一个强大且高效的代码生成工具,用于自动生成 MyBatis 框架的 XML 映射文件、Java 模型类以及 DAO 接口。这个工具能够显著提高开发效率,减少手动编写重复...
MyBatis/iBatis 是两个流行的数据访问框架,它们简化了Java开发中的数据库操作。iBatis 是早期的版本,而 MyBatis 是其后继者,提供了更加强大和灵活的功能。本教程将深入探讨如何使用MyBatis/iBatis自动生成SQL...
MyBatis Generator(MBG)是一个强大的工具,用于自动生成MyBatis的SQL映射文件、Java模型类和DAO接口。在使用MBG时,一个关键步骤是配置`mybatis-generator.xml`文件,这个文件包含了所有必要的信息来指导MBG如何...
Mybatis Generate 是一个非常实用的工具,用于自动生成MyBatis框架中的DAO层接口和Mapper文件,极大地提高了开发效率。这个工具通过配置文件模板和指定的数据库表信息,能够自动化地创建与数据库表对应的Java实体类...
在Java中,可以创建`org.mybatis.generator.api.MyBatisGenerator`对象并调用`generate()`方法: ```java import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration...
在Java Web开发中,特别是使用MyBatis框架时,频繁地编写DAO层接口(Mapper)以及对应的实体类(Entity)是一项既繁琐又容易出错的工作。为了提高开发效率并减少错误,可以使用MyBatis Generator工具来自动生成这些...
本主题将深入探讨如何在IDEA中实现MyBatis的自动代码生成(generateConfig),并介绍所需的相关工具和步骤。 一、MyBatis简介 MyBatis是一个优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。与传统的...
MyBatis Generator是一款强大的代码生成工具,它可以自动根据数据库表生成对应的JavaBean、Mapper接口、以及XML映射文件。它极大地简化了MyBatis的开发工作,使得开发者不必再手写繁琐的映射文件和数据访问层代码。...
MyBatis Generator(MBG)是一个强大的工具,它可以自动生成Mapper接口、Mapper XML文件、实体类和DAO层的实现类。首先,创建一个`generatorConfig.xml`配置文件,如下: ```xml <!DOCTYPE generatorConfiguration...
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(MBG)是一个强大的工具,用于自动生成Java源代码和XML配置文件,大大简化了开发人员的工作,特别是在处理与数据库交互时。它根据数据库中的表信息,能够生成DAO层、Model层以及Mapper XML文件,...