Coder Social home page Coder Social logo

zhangjingpu / spring-data-mybatis-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jclagache/spring-data-mybatis

0.0 2.0 0.0 69 KB

Simplifies the development of creating a MyBatis-based data access layer. http://www.springsource.org/spring-data

Java 100.00%

spring-data-mybatis-1's Introduction

Spring Data MyBatis

The primary goal of the Spring Data project is to make it easier to build Spring-powered applications that use data access technologies. This module deals with enhanced support for MyBatis based data access layers.

Features

This project defines a MyBatisRepository base interface :

public interface MyBatisRepository<T, ID extends Serializable> extends Repository<T, ID> {	
	T findOne(ID id);
	List<T> findAll();
	boolean exists(ID id);
	long count();
}

The only goal for now of this module is to make your MyBatis mappers created by MyBatis-Spring :

  • implement these four methods by only providing one select statement
  • registered as Spring Data repositories

Quick Start

Build and deploy it into your local maven repository :

git clone https://github.com/jclagache/spring-data-mybatis.git
cd spring-data-mybatis
mvn install

Add the jar to your maven project :

<dependency>
  <groupId>me.jclagache</groupId>
  <artifactId>spring-data-mybatis</artifactId>
  <version>0.1-SNAPSHOT</version>
</dependency>

Configure your infrastructure:

  1. add @EnableMyBatisRepositories annotation:
@Configuration
@EnableMyBatisRepositories
@EnableAutoConfiguration
public class ApplicationConfig {

}
  1. Configure datasource:
spring.datasource.url=jdbc:mysql://mysql:3306/opentsp_user
spring.datasource.username=admin
spring.datasource.password=opentsp
spring.datasource.test-on-borrow=true
spring.datasource.test-while-idle=true
spring.datasource.validation-query=SELECT 1;
spring.datasource.driverClassName=com.mysql.jdbc.Driver

#package for scanning mappers see https://mybatis.github.io/spring/mappers.html#scan default value *
mybatis.mapper.base.package=me.jclagache.data.mybatis.repository
#package mybatis aliases see https://mybatis.github.io/mybatis-3/configuration.html#typeAliases default value ""
mybatis.aliases.package=me.jclagache.data.mybatis.domain

Create an entity:

public class User {

	private Integer id;
	private String firstname;
	private String lastname;
	// Getters and setters
}

Create a repository interface:

public interface CustomerRepository extends MyBatisRepository<Customer, Integer> {
}

Write your mapper :

<?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.acme.CustomerRepository">
	<resultMap id="customerResultMap" type="Customer">
		<id property="id" column="id" />
		<result property="firstName" column="first_name" />
		<result property="lastName" column="last_name" />
	</resultMap>
	<select id="find" resultMap="customerResultMap">
		SELECT customer.id id,
		customer.first_name first_name,
		customer.last_name last_name
		FROM
		customer customer	
		<if test="pk">
			WHERE customer.id = #{pk.value}
		</if>
	</select>
</mapper>

The select statement id must be named "find". The parameter type of the select statement is a map. This map has a key pk whose value is the object identifying this domain object.

Write a test client

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationConfig.class)
public class UserRepositoryIntegrationTest {
     
  @Autowired UserRepository repository;
     
  @Test
  public void sampleTestCase() {         
	Customer customer = customerRepository.findOne(100);
	assertNotNull(customer); 
	List<Customer> customers = customerRepository.findAll();
	assertNotNull(customers);
	assertTrue(customers.size() > 0);
  }
}

Transaction management

see: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html#transaction-declarative-attransactional-settings

See the test classes for more.

spring-data-mybatis-1's People

Contributors

interair avatar jclagache avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.