R01.Non-JEE‎ > ‎MyBatis(iBatis)‎ > ‎

Using XML




resources/Configuration.xml


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE configuration

PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"

"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">

<configuration>

<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://localhost:5432/blogdb" />

<property name="username" value="chaoyi" />

<property name="password" value="123456" />

</dataSource>

</environment>

</environments>

<mappers>

<mapper resource="conf/BlogMapper.xml"/>

</mappers>

</configuration>



resources/Configuration.properties


driver=
url=
username=
password=




mappers/BlogMapper.xml


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper

PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"

"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

<mapper namespace="conf.BlogMapper">

<select id="selectBlog" parameterType="int" resultType="ArrayList">

select *

from blog where id = ${id}

</select>

</mapper>




kuo/Main.java


import java.io.IOException;

import java.io.Reader;

import java.util.List;


import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import conf.Blog;


public class Main {

public static void main(String[] args) throws IOException {

String resource = "conf/Configuration.xml";

Reader reader = Resources.getResourceAsReader(resource);

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()

.build(reader);


SqlSession session = sqlSessionFactory.openSession();

List<Blog> blogList = null;


try {

blogList = session.selectList("conf.BlogMapper.selectBlog", 1);


} finally {


for (Blog blog : blogList) {

System.out.println(blog.getId());

System.out.println("Hello");

session.close();

}


}


}

}


Comments