02-MyBatis 통합 그린플러스
16963 단어 GreenPlum
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
소결