1. 开发规范Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体跟Dao原始方法中接口实现类的方法相同。

  Mapper接口开发需要遵循以下规范:

  1.映射文件中namespace要等于接口的全路径

  2.通过sql语句实现数据库的操作 

  3.映射文件中sql语句id要等与于接口的方法名称
  4.映射文件中传入参数类型要等于接口方法的传入参数类型
  5.映射文件中返回结果集类型要等于接口方法的返回值类型

2.创建包结构:com.huida.mapper

3. 在com.huida.mapper包下创建UserMapper接口。

/**
 * 用户管理mapper
 */
Public interface UserMapper {
    //根据用户id查询用户信息
    public User findUserById(int id) throws Exception;
    //查询用户列表
    public List<User> findUserByUsername(String username) throws Exception;
    //添加用户信息
    public void insertUser(User user)throws Exception; 
}

3. 在com.huida.mapper下创建Mapper.xml(映射文件)

  在com.huida.mapper下创建UserMapper.xml,配置文件的名字应该与接口的名字相同。这里的头与User.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接口代理实现编写规则:
1.映射文件中namespace要等于接口的全路径
2.通过sql语句实现数据库的操作
3.映射文件中sql语句id要等与于接口的方法名称
4.映射文件中传入参数类型要等于接口方法的传入参数类型
5.映射文件中返回结果集类型要等于接口方法的返回值类型
 -->
<mapper namespace="com.huida.mapper.UserMapper">
    <select id="findUserById" parameterType="java.lang.Integer" resultType="com.huida.po.User">
        <!-- select语句返回的是user对象,所以resultType中写User类的全路径 -->
        select * from user where id=#{id}
    </select>

</mapper>

  需要注意的地方:

  1.映射文件中namespace要等于接口的全路径

  2.映射文件中sql语句id要等与于接口的方法名称

  3.映射文件中传入参数类型要等于接口方法的传入参数类型

  4.映射文件中返回结果集类型要等于接口方法的返回值类型

4. 加载UserMapper.xml文件

  修改SqlMapConfig.xml文件,在SqlMapConfig.xml中引入我们的UserMapper.xml文件:

<mappers>
        <mapper resource="com/huida/mapper/UserMapper.xml"/>
    </mappers>

5. 测试

package com.huida.test;

import java.io.IOException;
import java.io.InputStream;

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 com.huida.mapper.UserMapper;
import com.huida.po.User;

public class UserMapperTest {

    private SqlSessionFactory factory=null;
    @Before
    public void init() throws Exception{
        //通过流将核心配置文件读取进来
        InputStream inputStream=Resources.getResourceAsStream("config/SqlMapConfig.xml");
        //通过核心配置文件输入流来创建工厂
        factory=new SqlSessionFactoryBuilder().build(inputStream);
    }
    @Test
    public void testfindUserById(){
        //创建SqlSession
        SqlSession openSession=factory.openSession();
        //通过会话的getMapper方法来实例化接口(实现类的对象)
        UserMapper userMapper=openSession.getMapper(UserMapper.class);//参数放接口的字节码文件
        User user=userMapper.findUserById(1);
        System.out.println(user);
        
    }
    
}

  这里面应该注意的是:我们通过会话的getMapper方法实例化接口(也就是实现类的对象)。

6.小结

(1)selectOne和selectList

  动态代理对象调用sqlSession.selectOne()sqlSession.selectList()是根据mapper接口方法的返回值决定,如果返回list则调用selectList方法,如果返回单个对象则调用selectOne方法。

(2)namespace

  mybatis官方推荐使用mapper代理方法开发mapper接口,程序员不用编写mapper接口实现类,使用mapper代理方法时,输入参数可以使用pojo包装对象或map对象,保证dao的通用性。  

 

<!DOCTYPE mapper

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="cn.huida.mybatis.mapper.UserMapper">

<!-- 根据id获取用户信息 -->

<select id="findUserById" parameterType="int" resultType="cn.huida.mybatis.po.User">

select * from user where id = #{id}

</select>

<!-- 自定义条件查询用户列表 -->

<select id="findUserByUsername" parameterType="java.lang.String" 

resultType="cn.huida.mybatis.po.User">

   select * from user where username like '%${value}%'

</select>

<!-- 添加用户 -->

<insert id="insertUser" parameterType="cn.huida.mybatis.po.User">

<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">

select LAST_INSERT_ID()

</selectKey>

  insert into user(username,birthday,sex,address)

  values(#{username},#{birthday},#{sex},#{address})

</insert>

 

</mapper>

posted on 2018-12-20 20:23  wyhluckydog  阅读(196)  评论(0编辑  收藏  举报