Spring集成MyBaits的用法简介

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生类型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

依赖包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
增加接口类,声明方法,
在MyBatis中,增、删、改操作,默认返回的都是受影响的行数,且,由于我们自己不编写实现类,所以,返回值的意义是不可更改的!

在spring-dao.xml中配置MyBatis使用该接口(一次性配置)
需要配置MapperScannerConfigurer中的basePackage属性,使得MyBatis知道接口文件在哪里,
<!-- MapperScannerConfigurer -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 使用MyBatis时定义的接口文件在哪个包中 -->
<property name="basePackage"
value="xx.xx.xx.mapper" />
</bean>
使得MyBatis应用XML映射文件(一次性配置)
在使用MyBatis时,需要配置SqlSessionFactoryBean,使得MyBatis知道XML映射文件在哪里,并且,它最终还要保证SQL语句的执行,所以还需要为它配置数据源(DataSource),所以,在spring-dao.xml中添加配置:
<!-- SqlSessionFactoryBean -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置XML映射文件的位置 -->
<property name="mapperLocations"
value="classpath:mappers/UserMapper.xml" />
<!-- 配置数据源 -->
<property name="dataSource"
ref="dataSource" />
</bean>


增加数据--配置接口对应的XML映射文件
下载Mapper模版.xml文件。
通常,使用MyBatis时定义的接口与XML应该是一一对应的,将该文件重命名为UserMapper.xml,并将该文件复制到项目中的src\main\resources\mappers。
在映射文件中,根节点必须是<mapper>,且根节点上必须配置namespace属性,该属性的值是它对应的Java接口文件:
<mapper namespace="cn.tedu.ssm.mapper.UserMapper">
该文件主要用于配置需要执行的SQL语句,使用的子级节点可以是<insert>、<delete>、<update>、<select>,此次先执行增加,则在<mapper>下添加<insert>节点:
<mapper namespace="cn.tedu.ssm.mapper.UserMapper">
<insert>
</insert>
</mapper>
在<insert>或同类节点,必须配置id属性,该属性的值,就是对应的抽象方法的方法名称:
<mapper namespace="xx.UserMapper">
<insert id="insert">
</insert>
</mapper>
由于接口中的方法使用了User类型的参数,还可以在<insert>节点中添加parameterType属性,取值就是User的完整名称:
<mapper namespace="cn.tedu.ssm.mapper.UserMapper">
<insert id="insert"
parameterType="cn.tedu.ssm.entity.User">
</insert>
</mapper>。
然后,在<insert>节点内部,写上对应的SQL语句,填写数据值的位置,使用#{类中的属性名称}来表示:
<mapper namespace="cn.tedu.ssm.mapper.UserMapper">
<insert id="insert"
parameterType="cn.tedu.ssm.entity.User">
INSERT INTO t_user (
username, password, age, email
) VALUES (
#{username}, #{password}, #{age}, #{email}
)
</insert>
</mapper>
至此,关于增加用户数据的XML配置暂时完成!

xml的sql语句映射比较繁琐,但是适合比较复杂的sql语句,比如涉及判断是否插入值是否存在

等操作,比如以下sql用到的if标签,用于判断是否更新值

<update id="changeInfo">
UPDATE
t_user
SET
<if test="avatar != null">
avatar=#{avatar},
</if>
<if test="username != null">
username=#{username},
</if>
<if test="gender != null">
gender=#{gender},
</if>
<if test="phone != null">
phone=#{phone},
</if>
<if test="email != null">
email=#{email},
</if>
modified_user=#{modifiedUser},
modified_time=#{modifiedTime}
WHERE
id=#{id}
</update>

 

简单的sql可以通过@select等注解直接用于方法注解

@Select("select * from institution")
List<Institution> getAllInstitutionList();

posted @ 2019-06-02 21:53  f790271  阅读(181)  评论(0编辑  收藏  举报