视频地址:http://edu.51cto.com/sd/be679

这里讲的单表的增删改查,是由mapper代理的增删改查,先来看看步骤:

    1.jar包的导入

    2.配置全局的配置文件

    3.建立接口

    4.编写mapper.xml

    5.测试

 

    工程结构:这个你们自己可以调整,不一定和我一样

  

    目录解析:

       com.etc.mapper:这个包放的就相当于是dao的接口(com.etc.dao)

      config/mapper:这个放的就是操作数据库的相当于dao的实现类(com.etc.dao.impl)

      mybatis-config.xml:这个就是mybatis的全局配置文件

      com.etc.test: 测试你增删改查是否成功

    1.jar的导入(其实只要最后两个就可以了,这里这么多是因为后期还会用到一些所以就全都加进去省的麻烦)

    

    2.配置全局配置配置文件(目前只有两个:数据源,告诉mybatis实现类mapper.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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <!-- 配置数据库连接信息 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/student" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/student-mapper.xml"/>//mapper.xml的位置
    </mappers>
</configuration>

    3.编写接口 

public interface StudentMapper {
    //增加一个学生
    public void addStudent(Student student);
    //删除一个学生
    public void deleteById(int sid);
    //更新一个学生
    public void updateStudent(Student student);
    //根据ID查找学生
    public Student findById(int sid);
    //查询全部的学生
    public List<Student> findAll();
}

   4.编写mapper.xml

  其中:

      namespace:命名空间

      id:唯一标识

      parameterType:传入的参数类型(全路径)

      resultType(放回的结果集记录的类型,这里是你单条记录是什么类型,多条记录就是什么类型)

  使用mapper代理的开发模式有几个注意点:

    1.mapper的命名空间指向的必须是你的接口全路径

    2.mapper底下语句的id必须和你接口的方法名一致

    3.mapper底下语句的参数类型必须和你接口的入参类型一致

    4..mapper底下语句的返回结果类型必须和你接口中的一致(集合的话,匹配单条记录)

    5.你要把mapper配置到mybatis的全局配置中去

  <mappers>
        <mapper resource="mapper/student-mapper.xml"/>//mapper.xml的位置
    </mappers>
<?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">
<!--命名空:作用:就是对sql进行分类化管理  -->
<mapper namespace="com.etc.mapper.StudentMapper">
    <!-- 添加用户 -->
    <insert id="addStudent" parameterType="com.etc.entity.Student">
        insert into t_student (name,sex,hobbies) value(#{name},#{sex},#{hobbies})
    </insert>
    <!-- 根据ID删除学生 -->
    <delete id="deleteById" parameterType="java.lang.Integer">
        delete from t_student where sid=#{sid}
    </delete>
    <!-- 更新学生信息 -->
    <update id="updateStudent" parameterType="com.etc.entity.Student">
        update t_student set name=#{name},sex=#{sex},hobbies=#{hobbies} where sid=#{sid}
    </update>
        <!--根据ID查找学生  -->
    <select id="findById" parameterType="java.lang.Integer" resultTypep="com.etc.entity.Student">
        select sid sid_,name name_,sex sex_,hobbies hobbies_ from t_student where sid=#{sid}
    </select>
    <!-- 查找全部学生 -->
    <select id="findAll" parameterType="com.etc.entity.Student"  resultType="com.etc.entity.Student">
        select * from t_student   
    </select>
</mapper>

    5.编写测试代码(Junit测试)(实体类记得给一个空的构造函数供初始化,否则会报初始化异常

@Test
    public void findById() throws IOException{
        //mybatis的配置文件
        String resource="mybatis-config.xml";
        //得到配置文件流
        InputStream inputStream= Resources.getResourceAsStream(resource);
        //创建会话工厂,需要传入Mybatis的配置文件信息
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //通过工厂得到SqlSession
        SqlSession session=sessionFactory.openSession();
        //创建StudentMapper对象,mybatis自动生成代理对象
        StudentMapper studentMapper=session.getMapper(StudentMapper.class);
        Student student=studentMapper.findById(1);
        System.out.println(student);
        //资源释放
        session.close();
    }