课时2 Mybatis 动态代理方式实现增删改查(mybatis接口方式开发)以及配置分析

官方推荐接口方式开发

另外一种就是statement开发

 

 

原则:约定优于配置

.1)分析主配置文件

<configuration>
    <environments default="mysql">
        <environment id="mysql">
<!--                配置事务-->
            <transactionManager type="JDBC">
            </transactionManager>
<!--            配置数据库连接信息-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
<!--加载sql映射文件-->
    <mappers>
<!--        注意如果是xml文件 格式是../../..-->
        <mapper resource="org/hbz/dao/personMapper.xml"></mapper>
    </mappers>
</configuration>

分析如下:

1.environments标签和environment关系

  default:代表在哪个环境上运行项目

  id:代表的是环境标识 每一个id都代表一个环境

上诉的总结:通过default 指定的id值产生映射关系 可以随意的更换运行的环境 列如第一个环境是本机的数据库 第二个是真正的发布项目的数据库

 

也可以通过java代码来指定运行的环境

第二值就是更改默认运行的环境

SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader,"mysql");

2.dataSource标签

数据源类型:

    POOLED:使用数据库连接池

    UNPOOLED:传统的JDBC模式(每次访问数据库 均需要打开 关闭等数据库操作 但是打开关闭数据库是比较消耗性能的)

    JNDI:从tomcat中获取一个内置的数据库连接池 (数据连接池--数据源)

3.transactionManager标签

提交事务方式:

    JDBC:利用JDBC方式处理事务 (commit rollback close)

    MANAGED:将事务交由 其他组件去托管 (spring,jobss),默认 会关闭连接 如果不想关闭使用下面代码

<property name="closeCConnection" value="false"/>配置在事务标签下

.2)分析sql映射文件

<mapper namespace="org.hbz.dao.IStudentDao">
    <select id="selectStudentById" resultType="org.hbz.entity.Student" parameterType="Integer">
        select * from student where stuno=#{stuno}
    </select>
    
    <select id="selectAllStudent" resultType="org.hbz.entity.Student">
        select * from student
    </select>

    <insert id="addStudent" parameterType="org.hbz.entity.Student">
        insert into student(stuName,stuAge,graName) values(#{stuName},#{stuAge},#{graName})
    </insert>

    <delete id="deleteStudent" parameterType="Integer">
        delete from student where id=#(id)
    </delete>

    <update id="updateStudent" parameterType="org.hbz.entity.Student">
        update into student set stuName=#{stuName},stuAge=#{stuAge},graName=#{graName} where id=#{id}
    
</mapper>

1.mybatis约定:resultType和parameter只能有一个值

2.select :标签代表查询操作

3.namespace:映射的接口路径

4.id:代表接口中的方法名称

5.parameterType:

  5.1 代表接口中的方法参数类型

  5.2 如果parameterType的参数是8个基本类型(基本类型+String) 占位符里面的值可以随便填写#{xxx}

  5.3 如果是对象类型 必须写的是对象的属性名称 #{属性名}

6.resultMap:

  6.1 代表接口方法的返回值

  6.2 如果返回值是一个 对象(如Student) ,则无论是返回一个还是多个,再resultType都写成org.hbz.entity.Student

.7)dao层

public interface IStudentDao {
    /**
     * 按照id查询一个学生
     * @param id
     * @return
     */
    Student selectStudentById(Integer id);

    /**
     * 查询全部学生
     * @return
     */
    List<Student> selectAllStudent();

    /**
     * 增加一个学生
     * @param student
     * @return
     */
    Integer addStudent(Student student);

    /**
     * 根据id删除学生
     * @param id
     * @return
     */
    Integer deleteStudent(Integer id);

    /**
     * 按照id更新学生
     * @param student
     * @return
     */
    Integer updateStudent(Student student);
}

1.如果调用了dao层的方法 则会通过接口名称来查找配置的文件namespace

并且通过方法名值映射id得到sql语句

2.习惯:既然dao层和mapper.xml方法一起 而在idea中resources的包和java源码的包结构一样即可 会自动放在一起

.8)测试类

 //查询一个学生
    public static void selectOneStudent() throws IOException {
        Reader reader = Resources.getResourceAsReader("config.xml");
        SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
        SqlSession session=sessionFactory.openSession();
        IStudentDao iStudentDao=session.getMapper(IStudentDao.class);
        Student student = iStudentDao.selectStudentById(1);
        System.out.println(student);
        session.close();
        reader.close();
    }
    //查询全部学生
    public static void selectAll() throws IOException {
        Reader reader = Resources.getResourceAsReader("config.xml");
        SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
        SqlSession session=sessionFactory.openSession();
        IStudentDao iStudentDao=session.getMapper(IStudentDao.class);
        List<Student> stus = iStudentDao.selectAllStudent();
        System.out.println(stus);
        session.close();
        reader.close();
    }

    //增加一个学生
    public static void addStu() throws IOException {
        Reader reader = Resources.getResourceAsReader("config.xml");
        SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
        SqlSession session=sessionFactory.openSession();
        IStudentDao iStudentDao=session.getMapper(IStudentDao.class);
        Student stu=new Student();

        stu.setStuName("王五");
        stu.setStuAge(20);
        stu.setGraName("S1");

        Integer count = iStudentDao.addStudent(stu);
        session.commit();
        System.out.println(count);
        session.close();
        reader.close();
    }

    //删除一个学生
    public static void deleteStu() throws IOException {
        Reader reader = Resources.getResourceAsReader("config.xml");
        SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
        SqlSession session=sessionFactory.openSession();
        IStudentDao iStudentDao=session.getMapper(IStudentDao.class);

        Integer count = iStudentDao.deleteStudent(2);
        session.commit();
        System.out.println(count);
        session.close();
        reader.close();
    }

    //更新学生
    public static void updateStudent() throws IOException {
        Reader reader = Resources.getResourceAsReader("config.xml");
        SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
        SqlSession session=sessionFactory.openSession();
        IStudentDao iStudentDao=session.getMapper(IStudentDao.class);
        Student stu=new Student();
        stu.setStuNo(3);
        stu.setStuName("王子");
        stu.setStuAge(20);
        stu.setGraName("S2");

        Integer count = iStudentDao.updateStudent(stu);
        session.commit();
        System.out.println(count);
        session.close();
        reader.close();
    }

1.通过getMapper(接口.class)方法反射出类来得到这个接口

之后调用接口中的方法即可

 

原文地址:https://www.cnblogs.com/thisHBZ/p/12438995.html