• 09.JavaWeb-MyBatis


    3.MyBatis        

            MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。

    3.1 配置MyBatis

    3.1.1 导入依赖

    1. <dependency>
    2. <groupId>mysqlgroupId>
    3. <artifactId>mysql-connector-javaartifactId>
    4. <version>8.0.33version>
    5. dependency>
    6. <dependency>
    7. <groupId>org.mybatisgroupId>
    8. <artifactId>mybatisartifactId>
    9. <version>3.5.6version>
    10. dependency>

    3.1.2 配置MyBatis的xml文件

    1. "1.0" encoding="UTF-8" ?>
    2. configuration
    3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    4. "https://mybatis.org/dtd/mybatis-3-config.dtd">
    5. <configuration>
    6. <environments default="development">
    7. <environment id="development">
    8. <transactionManager type="JDBC"/>
    9. <dataSource type="POOLED">
    10. <property name="driver" value="${driver}"/>
    11. <property name="url" value="${url}"/>
    12. <property name="username" value="${username}"/>
    13. <property name="password" value="${password}"/>
    14. dataSource>
    15. environment>
    16. environments>
    17. <mappers>
    18. <mapper resource="org/mybatis/example/BlogMapper.xml"/>
    19. mappers>
    20. configuration>

    3.1.3 配置mapper相应的xml文件

    1. "1.0" encoding="UTF-8" ?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="org.mybatis.example.BlogMapper">
    6. <select id="selectBlog" resultType="Blog">
    7. select * from Blog where id = #{id}
    8. select>
    9. mapper>

    3.2使用MyBatis

            在要使用mybatis的类中操作

    3.2.1 加载mybatis主配置文件

    1. String resource = "mybatis-config.xml";
    2. InputStream inputStream = Resources.getResourceAsStream(resource);

    3.2.2 创建工厂对象

     SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    3.2.3 创建SqlSession对象

                    用来调用sql语句操作数据库

    SqlSession sqlSession = sqlSessionFactory.openSession();

    3.2.4 创建Mapper相应的接口

            注意:
            * 1.方法的名字与对象sql的id保持一致
            * 2.对应xml的namespace值必须是对应接口的类型全名

    3.2.5 接口中创建需要的方法(crud)

    1. List findAll();
    2. Employee findById(int id);
    3. Employee findByIdAndName(Map map);
    4. Employee findByIdAndName(Employee employee);
    5. //@Param()其实就是将参数封装为map对象,注解的内容作为key,参数的值作为值
    6. Employee findByIdAndName(@Param("id")int id,@Param("name") String name);
    7. //增
    8. boolean add(Employee employee);
    9. //改
    10. void update(@Param("id") int id,@Param("name") String name);

    3.2.6 mapper相应的xml文件创建相应的sql语句

    1. <mapper namespace="com.woniuxy.mapper.EmployeeMapper">
    2. <select id="findByIdAndName" resultType="com.woniuxy.entity.Employee">
    3. select * from employee where id = #{id} and name = #{name}
    4. select>
    5. <insert id="add">
    6. insert into employee values(null,#{cellphone},#{password},#{avatar},#{name},'0');
    7. insert>
    8. <update id="update">
    9. update employee set name = #{name} where id = #{id}
    10. update>

    3.2.7 通过sqlSession得到接口实现的对象

    EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);

     3.2.8 调用接口的方法实现相应的操作

    1. //5.单参
    2. Employee byId = mapper.findById(1);
    3. System.out.println(byId);
    4. //6.多参
    5. Map map = new HashMap<>();
    6. map.put("id",1);
    7. map.put("name","小李");
    8. Employee byIdAndName = mapper.findByIdAndName(map);
    9. System.out.println("map"+byIdAndName);
    10. Employee arg = new Employee();
    11. arg.setId(1);
    12. arg.setName("小李");
    13. Employee byIdAndName1 = mapper.findByIdAndName(arg);
    14. System.out.println("arg:"+byIdAndName1);

    3.3 传参的三种方法

            mysql不支持多个参数单独传递,只能一个数据或者多个数据(map、entity、array、list)

    1. Employee findByIdAndName(Map map);
    2. Employee findByIdAndName(Employee employee);
    3. //@Param()其实就是将参数封装为map对象,注解的内容作为key,参数的值作为值
    4. Employee findByIdAndName(@Param("id")int id,@Param("name") String name);

            实例对象与注解的方法比较好用,map一般不用

  • 相关阅读:
    LQ0265 汉诺塔【水题】
    SpringCloud 学习笔记总结 (六)
    Linux 入门
    whisper large-v3 模型文件下载链接
    什么是RPA?一文了解RPA发展与进程!
    三只松鼠、盐津铺子:战略相似,命运迥异
    java python+vue校园摄图网的设计与实现
    虚拟化简介
    Ubuntu20.0工作区(workspace)介绍,切换工作区方式和快捷键
    【负载均衡在线OJ项目日记】项目简介
  • 原文地址:https://blog.csdn.net/LB_bei/article/details/131139953