mybatis15 mapper方式 代码

UserMapper.java

package cn.itcast.mybatis.mapper;

import java.util.List;

import cn.itcast.mybatis.po.User;
import cn.itcast.mybatis.po.UserQueryVo;

public interface UserMapper {
    
    //根据用户id查询用户信息
    public User findUserById(int id) throws Exception;
    
    //根据用户名称  查询用户信息
    public List<User> findUserByName(String username) throws Exception;
    
    //自定义查询条件查询用户信息
    public List<User> findUserList(UserQueryVo userQueryVo) throws Exception;
    
    //查询用户,使用resultMap进行映射
    public List<User> findUserListResultMap(UserQueryVo userQueryVo)throws Exception;
    //查询用户,返回记录个数
    public int findUserCount(UserQueryVo userQueryVo) throws Exception;
    
    //插入用户
    public void insertUser(User user)throws Exception;
    //删除用户
    public void deleteUser(int id) throws Exception;
    //修改用户
    public void updateUser(User user) throws Exception;
    
    

}

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--mapper代理开发时将namespace指定为mapper接口的全限定名-->
<mapper namespace="cn.itcast.mybatis.mapper.UserMapper">
<!-- 在mapper.xml文件中配置很多的sql语句,执行每个sql语句时,封装为MappedStatement对象
mapper.xml以statement为单位管理sql语句
 -->
     
     <!-- 将用户查询条件定义为sql片段
     建议对单表的查询条件单独抽取sql片段,提高公用性
     注意:不要将where标签放在sql片段(公用性差)
       -->
     <sql id="query_user_where">
             <!-- 如果 userQueryVo中传入查询条件,再进行sql拼接-->
            <!-- test中userCustom.username表示从userQueryVo读取属性值-->
            <if test="userCustom!=null">
                <if test="userCustom.username!=null and userCustom.username!=''">
                    and username like '%${userCustom.username}%'
                </if>
                <if test="userCustom.sex!=null and userCustom.sex!=''">
                    and sex = #{userCustom.sex}
                </if>
                <!-- 根据id集合查询用户信息 -->
                <!-- 最终拼接的效果:
                SELECT id ,username ,birthday  FROM USER WHERE username LIKE '%小明%'       AND id IN (开始          16,22,25循环             )结束
                collection:集合的属性,userQueryVo的ids属性
                open:开始循环拼接的串
                close:结束循环拼接的串
                item:每次循环取到的对象
                separator:每两次循环中间拼接的串
                 -->
                 <foreach collection="ids" open=" AND id IN ( " close=")" item="id" separator=",">
                     #{id}    <!-- 16,循环 -->
                 </foreach>
                 <!-- 
                如果拼接 SELECT id ,username ,birthday  FROM USER WHERE username LIKE '%小明%' AND (开始                id = 16 OR id = 22 OR id = 25循环             )结束 
                  <foreach collection="ids" open=" AND ( " close=")" item="id" separator="OR">
                     id = #{id}   <!--  id = 16 ,循环 -->
                 </foreach>
                  -->
                <!-- 还有很的查询条件 -->
            </if>
     </sql>
 
     <!-- 定义resultMap,列名和属性名映射配置
     id:mapper.xml中的唯一标识 
     type:最终要映射的pojo类型
      -->
     <resultMap id="userListResultMap" type="user" >
         <!-- 列名
         id_,username_,birthday_
         id:要映射结果集(查询的结果select id id_,username username_,birthday birthday_)的唯 一标识 ,称为主键
         column:结果集(查询的结果)的列名
         property:type指定的哪个属性中
          -->
          <id column="id_" property="id"/>
          <!-- result就是普通列的映射配置 -->
          <result column="username_" property="username"/>
          <result column="birthday_" property="birthday"/>
     
     </resultMap>

    <!-- 根据id查询用户信息 -->
    <!-- 
        id:唯一标识 一个statement
        #{}:表示 一个占位符,如果#{}中传入简单类型的参数,#{}中的名称随意
        parameterType:输入 参数的类型,通过#{}接收parameterType输入 的参数
        resultType:输出结果 类型,不管返回是多条还是单条,指定单条记录映射的pojo类型
     -->
    <select id="findUserById" parameterType="int" resultType="user">
        SELECT * FROM USER WHERE id= #{id}
    
    </select>
    
    <!-- 根据用户名称查询用户信息,可能返回多条
    ${}:表示sql的拼接,通过${}接收参数,将参数的内容不加任何修饰拼接在sql中。
    
     -->
    <select id="findUserByName" parameterType="java.lang.String" resultType="cn.itcast.mybatis.po.User">
        select * from user where username like '%${value}%'
    </select>
    
    <!-- 自定义查询条件查询用户的信息
    parameterType:指定包装类型
    %${userCustom.username}%:userCustom是userQueryVo中的属性,通过OGNL获取属性的值
     -->
    <select id="findUserList" parameterType="userQueryVo" resultType="user">
    
        select id,username,birthday from user
        <!-- where标签相当 于where关键字,可以自动去除第一个and -->
        <where>
            <!-- 引用sql片段,如果sql片段和引用处不在同一个mapper必须前边加namespace -->
            <include refid="query_user_where"></include>
            <!-- 下边还有很其它的条件 -->
            <!-- <include refid="其它的sql片段"></include> -->
        </where>
        
        
        
    </select>
    
    <!-- 使用resultMap作结果映射
    resultMap:如果引用resultMap的位置和resultMap的定义在同一个mapper.xml,
    直接使用resultMap的id,如果不在同一个mapper.xml要在resultMap的id前边加namespace
    
     -->
    <select id="findUserListResultMap" parameterType="userQueryVo" resultMap="userListResultMap">
    
        select id id_,username username_,birthday birthday_ from user where username like '%${userCustom.username}%'
    </select>
    
    <!-- 输出简单类型
    功能:自定义查询条件,返回查询记录个数,通常用于实现 查询分页
     -->
     <select id="findUserCount" parameterType="userQueryVo" resultType="int">
         select count(*) from user 
        <!-- where标签相当 于where关键字,可以自动去除第一个and -->
        <where>
            <!-- 引用sql片段,如果sql片段和引用处不在同一个mapper必须前边加namespace -->
            <include refid="query_user_where"></include>
            <!-- 下边还有很其它的条件 -->
            <!-- <include refid="其它的sql片段"></include> -->
        </where>
     </select>
    
    <!-- 添加用户
    parameterType:输入 参数的类型,User对象 包括 username,birthday,sex,address
    #{}接收pojo数据,可以使用OGNL解析出pojo的属性值
    #{username}表示从parameterType中获取pojo的属性值
    selectKey:用于进行主键返回,定义了获取主键值的sql
    order:设置selectKey中sql执行的顺序,相对于insert语句来说
    keyProperty:将主键值设置到哪个属性
    resultType:select LAST_INSERT_ID()的结果 类型
    
     -->
    <insert id="insertUser" parameterType="cn.itcast.mybatis.po.User">
        <selectKey keyProperty="id" order="AFTER" resultType="int">
            select LAST_INSERT_ID()
        </selectKey>
        
        INSERT INTO USER(username,birthday,sex,address) VALUES(#{username},#{birthday},#{sex},#{address})
    </insert>
    
    <!-- mysql的uuid生成主键 -->
    <!-- <insert id="insertUser" parameterType="cn.itcast.mybatis.po.User">
        <selectKey keyProperty="id" order="BEFORE" resultType="string">
            select uuid()
        </selectKey>
        
        INSERT INTO USER(id,username,birthday,sex,address) VALUES(#{id},#{username},#{birthday},#{sex},#{address})
    </insert> -->
    
    <!-- oracle
    在执行insert之前执行select 序列.nextval() from dual取出序列最大值,将值设置到user对象 的id属性
     -->
    <!-- <insert id="insertUser" parameterType="cn.itcast.mybatis.po.User">
        <selectKey keyProperty="id" order="BEFORE" resultType="int">
            select 序列.nextval() from dual
        </selectKey>
        
        INSERT INTO USER(id,username,birthday,sex,address) VALUES(#{id},#{username},#{birthday},#{sex},#{address})
    </insert> -->
    
    <!-- 用户删除  -->
    <delete id="deleteUser" parameterType="int">
     delete from user where id=#{id}
    </delete>
    <!-- 用户更新 
    要求:传入的user对象中包括 id属性值
    -->
    <update id="updateUser" parameterType="cn.itcast.mybatis.po.User">
        update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}
    </update>

</mapper>

测试代码:

package cn.itcast.mybatis.mapper;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import cn.itcast.mybatis.po.User;
import cn.itcast.mybatis.po.UserCustom;
import cn.itcast.mybatis.po.UserQueryVo;

public class UserMapperTest {

    // 会话工厂
    private SqlSessionFactory sqlSessionFactory;

    // 创建工厂
    @Before
    public void init() throws IOException {

        // 配置文件(SqlMapConfig.xml)
        String resource = "SqlMapConfig.xml";

        // 加载配置文件到输入 流
        InputStream inputStream = Resources.getResourceAsStream(resource);

        // 创建会话工厂
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    }

    @Test
    public void testFindUserById() throws Exception {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 创建代理对象,接口实现类
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        User user = userMapper.findUserById(1);

        System.out.println(user);

    }

    @Test
    public void testFindUserByUsername() throws Exception {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 创建代理对象,接口实现类
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<User> list = userMapper.findUserByName("小明");

        System.out.println(list);

    }

    @Test
    public void testInsertUser() throws Exception {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 创建代理对象,接口实现类
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        // 插入对象
        User user = new User();
        user.setUsername("李奎");
        userMapper.insertUser(user);
        sqlSession.commit();
        sqlSession.close();

    }

    // 通过包装类型查询用户信息
    @Test
    public void testFindUserList() throws Exception {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 创建代理对象,接口实现类
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        // 构造查询条件
        UserQueryVo userQueryVo = new UserQueryVo();
        UserCustom userCustom = new UserCustom();
        userCustom.setUsername("小明");
        userCustom.setSex("1");
        userQueryVo.setUserCustom(userCustom);
        
        //id集合
        List<Integer> ids  = new ArrayList<Integer>();
        ids.add(16);
        ids.add(22);
        userQueryVo.setIds(ids);

        List<User> list = userMapper.findUserList(userQueryVo);

        sqlSession.close();

        System.out.println(list);

    }

    // 使用resultMap进行结果映射 
    @Test
    public void testFindUserListResultMap() throws Exception {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 创建代理对象,接口实现类
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        // 构造查询条件
        UserQueryVo userQueryVo = new UserQueryVo();
        
        UserCustom userCustom = new UserCustom();
        userCustom.setUsername("小明");
        userQueryVo.setUserCustom(userCustom);

        List<User> list = userMapper.findUserListResultMap(userQueryVo);

        sqlSession.close();

        System.out.println(list);

    }

    // 返回查询记录总数
    @Test
    public void testFindUserCount() throws Exception {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 创建代理对象,接口实现类
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        // 构造查询条件
        UserQueryVo userQueryVo = new UserQueryVo();
        UserCustom userCustom = new UserCustom();
        userCustom.setUsername("小明");
        userQueryVo.setUserCustom(userCustom);

        int count = userMapper.findUserCount(userQueryVo);

        sqlSession.close();

        System.out.println(count);

    }

}

UserQueryVo.java

package cn.itcast.mybatis.po;

import java.util.List;

/**
包装类型,将来在使用时从页面传到controller、service、mapper </p>
 */
public class UserQueryVo {
    
    //用户信息
    private User user;
    
    //自定义user的扩展对象
    private UserCustom userCustom;
    
    //用户id集合
    private List<Integer> ids;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public UserCustom getUserCustom() {
        return userCustom;
    }

    public void setUserCustom(UserCustom userCustom) {
        this.userCustom = userCustom;
    }

    public List<Integer> getIds() {
        return ids;
    }

    public void setIds(List<Integer> ids) {
        this.ids = ids;
    }
    
    
    
    
}

 

posted @ 2015-10-11 20:45  无天666  阅读(292)  评论(0编辑  收藏  举报