Mybatis postgresql crud
Всем привет.
У меня есть база данных PostgreSQL.
Приложение основано на Spring Framework.
MyBatis подключен к PostgreSQL. (MyBatis версия 3. запросы в формате xml).
Подскажите, пожалуйста, как реализовать crud-стандартные операции (create read update delete)?
Спасибо!
Что я уже пробовал:
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"/> <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
<!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_entitiesSomeApp.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) } }