Click here to Skip to main content
15,885,760 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody.
I have a PostgreSQL database.
The application is based on the Spring Framework.
MyBatis is connected to the PostgreSQL. (MyBatis version 3. queries in xml format).
Please tell me how to implement crud - standard operations (create read update delete)?
Thanks!

What I have tried:

mybatis-config.xml

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"/> 
     
    <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"/> 
             
         
    

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

SomeMapper.xml
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 * from some_entities where id = #{id}  
    

    
        select * from some_entities
SomeApp.java
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)
    }
}
Posted
Updated 17-May-17 23:22pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900