MyBatis与Spring的整合(映射器及动态SQL)
时间: 2023-06-13 14:04:24 浏览: 63
MyBatis与Spring的整合可以通过Spring提供的MyBatis-Spring集成模块来实现,这个模块提供了对MyBatis的完全支持,包括:
1. SqlSessionFactory的管理和配置
2. SqlSession的管理和注入
3. Mapper接口的自动扫描和注入
4. 事务管理
下面我们来看一下如何使用MyBatis-Spring集成模块来实现映射器和动态SQL的使用。
一、映射器的使用
1. 创建Mapper接口
Mapper接口定义了一组操作数据库的方法,这些方法对应了SQL映射文件中定义的SQL语句。在使用Mapper接口之前,我们需要定义这个接口。例如:
```java
public interface UserMapper {
List<User> findAll();
User findById(int id);
void addUser(User user);
void updateUser(User user);
void deleteUser(int id);
}
```
2. 编写SQL映射文件
MyBatis中的SQL语句都是通过XML文件来定义的,我们需要创建一个XML文件,来定义Mapper接口中的方法对应的SQL语句。例如:
```xml
<mapper namespace="com.example.mapper.UserMapper">
<select id="findAll" resultType="com.example.model.User">
select * from user
</select>
<select id="findById" parameterType="int" resultType="com.example.model.User">
select * from user where id=#{id}
</select>
<insert id="addUser" parameterType="com.example.model.User">
insert into user(name, age, email) values(#{name}, #{age}, #{email})
</insert>
<update id="updateUser" parameterType="com.example.model.User">
update user set name=#{name}, age=#{age}, email=#{email} where id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id}
</delete>
</mapper>
```
3. 配置MapperScannerConfigurer
在Spring的配置文件中,我们需要配置MapperScannerConfigurer来自动扫描Mapper接口,并将Mapper接口交给Spring容器来管理。例如:
```xml
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.mapper" />
</bean>
```
4. 在代码中使用Mapper接口
在代码中我们可以通过注入Mapper接口的方式来使用它,例如:
```java
@Autowired
private UserMapper userMapper;
public void test(){
List<User> userList = userMapper.findAll();
User user = userMapper.findById(1);
user.setName("Tom");
userMapper.updateUser(user);
userMapper.deleteUser(1);
}
```
二、动态SQL的使用
MyBatis中的动态SQL可以根据条件来动态生成SQL语句,如果条件满足则将条件拼接到SQL语句中,否则不拼接。动态SQL包括了if、where、foreach、choose等标签。
1. if标签
if标签可以根据条件来动态生成SQL语句,例如:
```xml
<select id="findByNameAndAge" resultType="com.example.model.User">
select * from user
<where>
<if test="name != null">
and name = #{name}
</if>
<if test="age != null">
and age = #{age}
</if>
</where>
</select>
```
2. where标签
where标签可以根据条件来动态生成SQL语句,并且会去掉第一个条件前面的and或者or,例如:
```xml
<select id="findByNameAndAge" resultType="com.example.model.User">
select * from user
<where>
<if test="name != null">
and name = #{name}
</if>
<if test="age != null">
and age = #{age}
</if>
</where>
</select>
```
3. foreach标签
foreach标签可以遍历一个集合,并且根据集合中的元素来动态生成SQL语句,例如:
```xml
<select id="findByIdList" resultType="com.example.model.User">
select * from user
<where>
<foreach collection="idList" item="id" separator="or">
id = #{id}
</foreach>
</where>
</select>
```
4. choose标签
choose标签可以根据条件来选择生成SQL语句的部分,类似于Java中的switch语句,例如:
```xml
<select id="findByNameOrAge" resultType="com.example.model.User">
select * from user
<where>
<choose>
<when test="name != null">
and name = #{name}
</when>
<when test="age != null">
and age = #{age}
</when>
<otherwise>
and 1=1
</otherwise>
</choose>
</where>
</select>
```
以上就是MyBatis与Spring集成的映射器和动态SQL的使用方法。