02-MyBatis 통합 그린플러스

16963 단어 GreenPlum
문서 목록
  • MyBatis 통합 Greenplum
  • 1. 예
  • 1.1 POM 의존도
  • 1.2 postgresql-config.xml 프로필
  • 1.3 매핑 파일 Mapper.xml
  • 1.4 Java 클래스 및 인터페이스
  • 1.5건표문
  • 1.6 테스트 코드
  • 소결
  • MyBatis 통합 Greenplum
  • MyBatis가Greenplumde를 통합하는 과정은 거의 MySql과 마찬가지로 코드상의 조정이 필요하지 않습니다. 다음은 모든 코드 예시
  • 예제
    1.1 POM 의존성
        
            
                com.alibaba
                fastjson
                1.2.45
            
            
            
                junit
                junit
                4.12
                test
            
            
                org.springframework
                spring-test
                4.3.2.RELEASE
                test
            
    
            
            
                org.slf4j
                slf4j-api
                1.7.10
            
            
                ch.qos.logback
                logback-classic
                1.1.2
            
            
                ch.qos.logback
                logback-core
                1.1.2
            
    
            
            
            
                org.postgresql
                postgresql
                9.3-1101-jdbc41
            
    
            
            
                org.mybatis
                mybatis
                3.4.1
            
    
            
            
                org.projectlombok
                lombok
                1.18.0
                provided
            
        
     
    

    1.2 postgresql-config.xml 프로필
    //   resource/mybatis/postgresql-config.xml
    
    
    <configuration>
    
        <typeAliases>
            <package name="com.intellif.mozping.entity"/>
        typeAliases>
    
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="org.postgresql.Driver"/>
                    <property name="url" value="jdbc:postgresql://192.168.13.51:5432/db1"/>
                    <property name="username" value="gpadmin"/>
                    <property name="password" value="gpadmin"/>
                dataSource>
            environment>
        environments>
    
        
        <mappers>
            
            <mapper resource="mybatis/mapper/UserMapper.xml"/> 
        mappers>
    configuration>
    

    1.3 매핑 파일 Mapper.xml
    
    
    <mapper namespace="com.intellif.mozping.dao.UserMapper">
    
        <insert id="addUser" parameterType="com.intellif.mozping.entity.User">
    		insert into public.usertest (uname) values (#{uname})
    	insert>
     
    mapper>
    
    

    1.4 Java 클래스 및 인터페이스
    public interface UserMapper {
     
        int addUser(User people);
    }
    
     
    public class User {
        private int id;
        private String uname;
        
        //get set  
    }
    
    

    1.5 테이블 문
    CREATE TABLE "public"."usertest" (
    "id" int4 DEFAULT nextval('usertest_id_seq'::regclass) NOT NULL,
    "uname" varchar(50)
    )
    WITH (OIDS=FALSE);
     
    

    1.6 테스트 코드
    public class Test01 {
    
        private static final String CONFIG_FILE_PATH = "mybatis/postgresql-config.xml";
    
        @Test
        public void add() {
            SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSessionFactoryInstaceByConfig(CONFIG_FILE_PATH).openSession();
            User user = new User();
            user.setUname("testName");
    
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    
            int rowAffected = userMapper.addUser(user);
            //int rowAffected = userMapper.addUserGetId(user);
            System.out.println("The rows be affected :" + rowAffected);
            System.out.println("The primary key is:" + user.getId());
            //      
            sqlSession.commit();
            SqlSessionFactoryUtil.closeSession(sqlSession);
        }
    }
    
  • 인쇄
  • The rows be affected :1
    The primary key is:0
    

    소결
  • MySql과 거의 다르지 않습니다. 다만 메인 프로필에 있는 데이터 Source의 드라이브가 다릅니다
  • 샘플 코드 링크
  • 좋은 웹페이지 즐겨찾기