Guest User

Untitled

a guest
Apr 10th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <settings>
  7. <setting name="logImpl" value="LOG4J"/>
  8. </settings>
  9. <environments default="development">
  10. <environment id="development">
  11. <transactionManager type="JDBC"/>
  12. <dataSource type="POOLED">
  13. <property name="driver" value="org.postgresql.Driver"/>
  14. <property name="url" value="jdbc:postgresql://hostname:port/dbname"/>
  15. <property name="username" value="user"/>
  16. <property name="password" value="password"/>
  17. </dataSource>
  18. </environment>
  19. </environments>
  20.  
  21. <mappers>
  22. <mapper resource="SomeMapper.xml"/>
  23. </mappers>
  24. </configuration>
  25.  
  26. <!DOCTYPE mapper
  27. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  28.  
  29. <mapper namespace="com.example.SomeMapper">
  30. <resultMap id="result" type="com.example.SomeEntity" />
  31.  
  32. <select id="getEntityById" parameterType="java.lang.Integer" resultMap="result">
  33. select * from some_entities where id = #{id}
  34. </select>
  35.  
  36. <select id="getEntities" resultMap="result">
  37. select * from some_entities
  38. </select>
  39. </mapper>
  40.  
  41. package com.example;
  42.  
  43. import java.io.IOException;
  44. import java.io.Reader;
  45. import java.util.List;
  46.  
  47. import org.apache.ibatis.io.Resources;
  48. import org.apache.ibatis.session.SqlSessionFactory;
  49. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  50.  
  51. public class SomeApp {
  52. public static void main(String[] args) {
  53. SqlSessionFactory factory;
  54. SomeMapper mapper;
  55.  
  56. try (Reader reader = Resources.getResourceAsReader("mybatis-config.xml")) {
  57. factory = new SqlSessionFactoryBuilder().build(reader);
  58. mapper = factory.openSession().getMapper(SomeMapper.class);
  59. }
  60. catch(IOException e) {
  61. e.printStackTrace();
  62. }
  63.  
  64. List<SomeEntity> entities = mapper.getEntities();
  65. SomeEntity entity = mapper.getEntityById(1)
  66. }
  67. }
Add Comment
Please, Sign In to add comment