|
| 1 | +<?xml version="1.0" encoding="UTF-8"?> |
| 2 | +<!-- |
| 3 | +
|
| 4 | + Copyright 2010-2017 the original author or authors. |
| 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 | +<document xmlns="http://maven.apache.org/XDOC/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 20 | + xsi:schemaLocation="http://maven.apache.org/XDOC/2.0 http://maven.apache.org/xsd/xdoc-2.0.xsd"> |
| 21 | + |
| 22 | + <properties> |
| 23 | + <title>MyBatis-Spring | 第二章 入门</title> |
| 24 | + <author email="hpresnall@gmail.com">Hunter Presnall</author> |
| 25 | + <author email="eduardo.macarron@gmail.com">Eduardo Macarron</author> |
| 26 | + <author email="nanlei1987@gmail.com">Nan Lei</author> |
| 27 | + </properties> |
| 28 | + |
| 29 | + <body> |
| 30 | + <section name="第二章 入门"> |
| 31 | + <p> |
| 32 | +本章将会以简略的步骤告诉你如何安装和创建 MyBatis-Spring,并构建一个简单的数据访问事务性的应用程序。 |
| 33 | + </p> |
| 34 | + |
| 35 | + <subsection name="Installation"> |
| 36 | + <p> |
| 37 | +要使用MyBatis-Spring模块,只需要在类路径下包含 |
| 38 | + <code>mybatis-spring-${project.version}.jar</code> |
| 39 | +文件和相关依赖。 |
| 40 | + </p> |
| 41 | + <p> |
| 42 | +如果使用Maven,仅需要在pom.xml中加入以下代码即可: |
| 43 | + </p> |
| 44 | + <source><![CDATA[ |
| 45 | +<dependency> |
| 46 | + <groupId>org.mybatis</groupId> |
| 47 | + <artifactId>mybatis-spring</artifactId> |
| 48 | + <version>${project.version}</version> |
| 49 | +</dependency>]]></source> |
| 50 | + </subsection> |
| 51 | + |
| 52 | + <subsection name="Quick Setup"> |
| 53 | + <p> |
| 54 | +要和Spring一起使用MyBatis,需要在Spring应用上下文中定义至少两样东西:一个SqlSessionFactory和至少一个数据映射器类。 |
| 55 | + </p> |
| 56 | + |
| 57 | + <p> |
| 58 | +在MyBatis-Spring中,可使用SqlSessionFactoryBean来创建SqlSessionFactory。要配置这个工厂bean,只需要把下面代码放在Spring的XML配置文件中: |
| 59 | + </p> |
| 60 | + |
| 61 | + <source><![CDATA[ |
| 62 | +<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> |
| 63 | + <property name="dataSource" ref="dataSource" /> |
| 64 | +</bean>]]></source> |
| 65 | + |
| 66 | + <source><![CDATA[ |
| 67 | +@Bean |
| 68 | +public SqlSessionFactoryBean sqlSessionFactory() throws Exception { |
| 69 | + SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); |
| 70 | + factoryBean.setDataSource(dataSource()); |
| 71 | + return factoryBean.getObject(); |
| 72 | +}]]></source> |
| 73 | + |
| 74 | + <p> |
| 75 | +需要注意的是:SqlSessionFactory需要一个DataSource(数据源,译者注)。这可以是任意的DataSource,配置它就和配置其它Spring数据库连接一样。 |
| 76 | + </p> |
| 77 | + |
| 78 | + <p> |
| 79 | +假设你定义了一个如下的mapper接口: |
| 80 | + </p> |
| 81 | + <source><![CDATA[public interface UserMapper { |
| 82 | + @Select("SELECT * FROM users WHERE id = #{userId}") |
| 83 | + User getUser(@Param("userId") String userId); |
| 84 | +} ]]></source> |
| 85 | + |
| 86 | + <p> |
| 87 | +那么可以通过MapperFactoryBean像下面那样把接口加入到Spring中: |
| 88 | + </p> |
| 89 | + |
| 90 | + <source><![CDATA[ |
| 91 | +<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> |
| 92 | + <property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" /> |
| 93 | + <property name="sqlSessionFactory" ref="sqlSessionFactory" /> |
| 94 | +</bean>]]></source> |
| 95 | + |
| 96 | + <p> |
| 97 | +需要注意的是:所指定的映射器类必须是一个接口,而不是具体的实现类。在这个示例中通过注解来指定SQL语句,但是通过MyBatis映射器的XML文件也行。 |
| 98 | + </p> |
| 99 | + |
| 100 | + <p> |
| 101 | +一旦完成配置,你可以像其它Spring中的bean注入方法将映射器到业务(bussiness)或服务(service)对象中。MapperFactoryBean用来处理SqlSession的创建和关闭动作。 |
| 102 | +如果使用了Spring的事务,那么当事务完成时,session将会被提交或回滚。最终任何异常都会被翻译成Spring的DataAccessException异常。 |
| 103 | + </p> |
| 104 | + |
| 105 | + <p> |
| 106 | + 如果你使用Java代码来配置: |
| 107 | + </p> |
| 108 | + <source><![CDATA[ |
| 109 | +@Bean |
| 110 | +public UserMapper userMapper() throws Exception { |
| 111 | + SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory()); |
| 112 | + return sqlSessionTemplate.getMapper(UserMapper.class); |
| 113 | +}]]></source> |
| 114 | + |
| 115 | + <p> |
| 116 | +调用MyBatis数据方法现在只需一行代码: |
| 117 | + </p> |
| 118 | + <source><![CDATA[ |
| 119 | +public class FooServiceImpl implements FooService { |
| 120 | +
|
| 121 | + private final UserMapper userMapper; |
| 122 | +
|
| 123 | + public FooServiceImpl(UserMapper userMapper) { |
| 124 | + this.userMapper = userMapper; |
| 125 | + } |
| 126 | +
|
| 127 | + public User doSomeBusinessStuff(String userId) { |
| 128 | + return this.userMapper.getUser(userId); |
| 129 | + } |
| 130 | +}]]></source> |
| 131 | + </subsection> |
| 132 | + </section> |
| 133 | + </body> |
| 134 | +</document> |
0 commit comments