sharding-jdbc集成spring+mybatis分表分库

maven:

 



    4.0.0

    com.fulihui.com
    sharding-jdbc-demo
    1.0-SNAPSHOT

    
        UTF-8
        4.1.3.RELEASE
        3.2.4
        1.3.1
    

    
        
            com.dangdang
            sharding-jdbc-core
            ${sharding-jdbc.version}
            
                
                    org.slf4j
                    slf4j-log4j12
                
                
                    log4j
                    log4j
                
            
        
        
            com.dangdang
            sharding-jdbc-transaction
            ${sharding-jdbc.version}
            
                
                    org.slf4j
                    slf4j-log4j12
                
                
                    log4j
                    log4j
                
            
        
        
            com.dangdang
            sharding-jdbc-config-spring
            ${sharding-jdbc.version}
        
        
            junit
            junit
            4.10
            test
        
        
        
            org.projectlombok
            lombok
            1.16.10
        
        
            org.springframework
            spring-orm
            ${spring.version}
        
        
            commons-dbcp
            commons-dbcp
            1.4
        
        
            org.mybatis
            mybatis-spring
            1.2.2
        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        

        
            org.springframework
            spring-expression
            ${spring.version}
        
        
            org.springframework
            spring-aop
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
            
                
                    org.slf4j
                    slf4j-log4j12
                
                
                    log4j
                    log4j
                
            
        
        
            org.springframework
            spring-context-support
            ${spring.version}
        
        
            org.springframework
            spring-test
            ${spring.version}
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
            mysql
            mysql-connector-java
            5.1.28
        
    

 

建表语句:

 

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for t_student_0
-- ----------------------------
DROP TABLE IF EXISTS `t_student_0`;
CREATE TABLE `t_student_0` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `student_id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `age` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `t_student_1`;
CREATE TABLE `t_student_1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `student_id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `age` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `t_user_0`;
CREATE TABLE `t_user_0` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `age` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `t_user_1`;
CREATE TABLE `t_user_1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `age` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `t_user_2`;
CREATE TABLE `t_user_2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `age` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

 

spring配置:

 





   

    
        
        
    
    
    
     
        
        
    

    
    
    

    
    
    

    
        
            
                
                
            
            
            
        
        
            false
        
    

    
    
        
    

    


 

 

 




   

   
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
   

   
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
   


增、删、改、查接口代码就不贴了。

 

测试类:

 

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
        locations = { "classpath*:config/spring/spring-database.xml",
        "classpath*:config/spring/spring-sharding.xml" })
public class ShardingJdbcMybatisTest {

    @Resource
    public UserService userService;
    
    @Resource
    public StudentService studentService;

    @Test
    public void testUserInsert() {
        UserDO u = new UserDO();
        u.setUserId(9);
        u.setAge(25);
        u.setName("levon");
        Assert.assertEquals(userService.insert(u), true);
    }
    
    @Test
    public void testStudentInsert() {
        StudentDO StudentDO = new StudentDO();
        StudentDO.setStudentId(9);
        StudentDO.setAge(21);
        StudentDO.setName("hehe");
        Assert.assertEquals(studentService.insert(StudentDO), true);
    }

    @Test
    public void testFindAll(){
        List UserDOs = userService.findAll();
        if(null != UserDOs && !UserDOs.isEmpty()){
            for(UserDO u :UserDOs){
                System.out.println(u);
            }
        }
    }
    
    @Test
    public void testSQLIN(){
        List UserDOs = userService.findByUserIds(Arrays.asList(12,14,17));
        if(null != UserDOs && !UserDOs.isEmpty()){
            for(UserDO u :UserDOs){
                System.out.println(u);
            }
        }
    }
    
    @Test
    public void testTransactionTestSucess(){
        userService.transactionTestSucess();
    }
    
    @Test(expected = IllegalAccessException.class)
    public void testTransactionTestFailure() throws IllegalAccessException{
        userService.transactionTestFailure();
    }
    
    
}

 

你可能感兴趣的:(sharding-jdbc集成spring+mybatis分表分库)