|
| 1 | +<?xml version="1.0" encoding="UTF-8"?> |
| 2 | +<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN" "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"> |
| 3 | +<!-- |
| 4 | + Copyright 2010 The myBatis Team |
| 5 | +
|
| 6 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + you may not use this file except in compliance with the License. |
| 8 | + You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +--> |
| 18 | + |
| 19 | +<!-- |
| 20 | + version: $Id$ |
| 21 | +--> |
| 22 | +<chapter id="using_api"> |
| 23 | + <title>Using MyBatis API</title> |
| 24 | + |
| 25 | + <section id="using_api.main"> |
| 26 | + <title>Using MyBatis API</title> |
| 27 | + <para> |
| 28 | + You can also use directly MyBatis API. In this case you won't have any MyBatis-Spring Dependency and you |
| 29 | + simply use a injected <literal>SqlSessionFactory</literal> on your DAOs: |
| 30 | + <programlisting language="java"><![CDATA[public class UserMapperSqlSessionImpl implements UserMapper { |
| 31 | +
|
| 32 | + private SqlSessionFactory sqlSessionFactory; |
| 33 | +
|
| 34 | + public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { |
| 35 | + this.sqlSessionFactory = sqlSessionFactory; |
| 36 | + } |
| 37 | +
|
| 38 | + public User getUser(String userId) { |
| 39 | + SqlSession session = sqlSessionFactory.openSession(); |
| 40 | + User user = (User) session.selectOne("sample.UserMapper.getUser", userId); |
| 41 | + session.close(); |
| 42 | + return user; |
| 43 | + } |
| 44 | +
|
| 45 | +}]]></programlisting> |
| 46 | + This scenario also supports transaction but ther will be no exception translation. |
| 47 | + </para> |
| 48 | + </section> |
| 49 | + |
| 50 | +</chapter> |
0 commit comments