本文主要记录一些MyBatis常用的操作方法。
MyBatis的数据源配置
在spring配置文件中配置数据源
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
</bean>
<!-- 加载myBatis-config.xml配置文件,以及扫描myBatis/目录下每个DAO对应的SQL配置的XML文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:myBatis-config.xml" />
<property name="mapperLocations" value="classpath:mybatis/*.xml" />
</bean>
<!-- 将basePackage下的interface,转换为spring bean,service中可以直接 注入使用 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="com.test.my.dal.dao"></property>
</bean>
1. 数据表,我们有一张user的数据表:
2. Dao的持久化对象:
public class UserPojo {
private Integer id;
private String username;
private String password;
/*** setter / getter ****/
}
常用操作
myBatis-config.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 是否启用 数据中 a_column 自动映射 到 java类中驼峰命名的属性。[默认:false] -->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
<typeAliases>
<!-- 映射关系 -->
<typeAlias alias="UserPojo" type="com.test.my.dal.dao.domain.UserPojo" />
</typeAliases>
<mappers></mappers>
</configuration>
1-1,insert操作
<!-- 新增 -->
<insert id="addUser" parameterType="UserPojo" useGeneratedKeys="true"
keyProperty="id">
INSERT INTO
user
<set>
<if test="username != null">
username = #{username},
</if>
<if test="password != null">
password = #{password},
</if>
</set>
</insert>
Dao中:
void addUser(UserPojo pojo);
设置了useGeneratedKeys=”true”和keyProperty=”id”后,主键ID会放置到UserPojo的id属性值中。
1-2. update操作
<!-- 更新一条数据 -->
<update id="updateUser" parameterType="UserPojo">
UPDATE user
<set>
<if test="username != null">
username = #{username},
</if>
<if test="password != null">
password = #{password},
</if>
</set>
WHERE id = #{id}
</update>
Dao中:
int updateUser(UserPojo pojo);
返回的结果为int类型,是更新的影响条数,如果更新成功,则大于0,更新失败,则为0
1-3. delete操作
<!-- 删除一条数据 -->
<delete id="delete" parameterType="int" >
DELETE FROM user WHERE id = #{id}
</delete>
Dao中:
int delete(int id);
返回结果为int类型,如果删除成功,则返回影响记录数,否则返回0
1-4. select操作
<!-- 通过一个ID搜索 -->
<select id="getById" parameterType="int" resultType="UserPojo">
SELECT <include refid="field"/> FROM user WHERE id = #{id}
</select>
<!-- 搜索列表 -->
<select id="getUserList" parameterType="int" resultType="UserPojo">
SELECT <include refid="field"/>
FROM user
ORDER BY id DESC LIMIT 10
</select>
Dao中:
UserPojo getUserById(Integer userId);
UserPojo getUserList();
常用技巧
2-1. 申明共用的SQL块, 以简化代码量
例如:
<!-- 字段 -->
<sql id="field">
`id`,
`username`,
`password`
</sql>
使用方法如下:
<include refid="field"/>
2-2. if语句使用
<if test="username != null">
username = #{username},
</if>
<if test="password != null">
password = #{password},
</if>
2-3. where语句组装
<where>
<if test=”state != null”>
state = #{state}
</if>
<if test=”title != null”>
AND title like #{title}
</if>
<if test=”author != null and author.name != null”>
AND title like #{author.name}
</if>
</where>
2-4. set语句组装
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set>
2-5. 条件语句
<choose>
<when test=”title != null”>
AND title like #{title}
</when>
<when test=”author != null and author.name != null”>
AND title like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
2-6. foreach语句使用
where ID in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
其中collection可以选择list或者数组,一般使用list比较多一些。
2-7. @Param使用
如果Dao中有多个参数传递进来,可以使用
@Param("uids") List<Long> uids
类似于这样的方式来处理,这样在XML中就可以直接使用
#{uids}
本文转载自:http://blog.csdn.net/initphp/article/details/40347927#t5