0

Всем привет. покажите пожалуйста примеры как в postgre подключиться MyBatis 3 версия через xml делал раньше через хибернейт, а тут не очень ясно.

1 ответ 1

0

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE configuration 
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
        "http://mybatis.org/dtd/mybatis-3-config.dtd"> 
<configuration> 
    <settings>
        <setting name="logImpl" value="LOG4J"/> 
    </settings> 
    <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://hostname:port/dbname"/> 
                <property name="username" value="user"/> 
                <property name="password" value="password"/> 
            </dataSource> 
        </environment> 
    </environments>

    <mappers>
        <mapper resource="SomeMapper.xml"/> 
    </mappers> 
</configuration>

SomeMapper.xml

<!DOCTYPE mapper
     PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.SomeMapper">
    <resultMap id="result" type="com.example.SomeEntity" />

    <select id="getEntityById" parameterType="java.lang.Integer" resultMap="result">
        select * from some_entities where id = #{id}  
    </select>

    <select id="getEntities" resultMap="result">
        select * from some_entities
    </select>
</mapper>

SomeApp.java

package com.example;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SomeApp {
    public static void main(String[] args) {
        SqlSessionFactory factory;
        SomeMapper mapper;

        try (Reader reader = Resources.getResourceAsReader("mybatis-config.xml")) {
            factory = new SqlSessionFactoryBuilder().build(reader);
            mapper = factory.openSession().getMapper(SomeMapper.class);
        }
        catch(IOException e) {
            e.printStackTrace();
        }

        List<SomeEntity> entities = mapper.getEntities();
        SomeEntity entity = mapper.getEntityById(1)
    }
}
2

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.