• 【无标题】


    0817_mybatis简介

    单表的crud操作【字段多一些】 employees表

    条件查询
    public List findByConds1(String firstname,String lastname,int minsalary,int maxlary);
    public List findByConds2(Map map);
    public List findByConds3(@Param(“firstname”)String firstname,@Param(“lastname”)String lastname,int minsalary,int maxlary);
    public List findByConds4(EmployeesVo vo,int start,int pageSize);
    动手能力强的同学,可以将之前的项目中的dao层更改为mybatis的实现。
    结构:
    在这里插入图片描述
    Employees:

    
    import lombok.Data;
    
    import java.sql.Date;
    
    @Data
    public class Employees {
        private int employeeId;
        private String firstName;
        private String lastName;
        private String email;
        private String phoneNumber;
        private String jobId;
        private Double salary;
        private Double commissionPct;
        private Integer managerId;
        private Integer departmentId;
        private Date hiredate;
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    Employeesvo:

    import lombok.Data;
    
    @Data
    public class Employeesvo {
        private String firstName;
        private String lastName;
        private Integer minSalary;
        private Integer maxSalary;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    mybatis-config.xml:

    
    DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        
            <settings>
                
                <setting name="logImpl" value="STDOUT_LOGGING"/>
                <setting name="mapUnderscoreToCamelCase" value="true" />
            settings>
            
        <typeAliases>
            <package name="com.cdcas.pojo"/>
        typeAliases>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql:///jspzzx?useSSl=false&serverTimezone=Asia/Shanghai&useServerPrepStmts=true"/>
                    <property name="username" value="root"/>
                    <property name="password" value="redhat"/>
                dataSource>
            environment>
        environments>
        <mappers>
            
            <package name="com.cdcas.mapper"/>
        mappers>
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    EmployeesMapper接口:

    import com.cdcas.pojo.Employees;
    import com.cdcas.pojo.Employeesvo;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.List;
    import java.util.Map;
    
    public interface EmployeesMapper {
    
        List<Employees> findByConds1(String firstName, String lastName, int minSalary, int maxSalary);
        List<Employees> findByConds2(Map<String,Object> map);
        List<Employees> findByConds3(@Param("firstName") String firstName, @Param("lastName") String lastName, int minSalary, int maxSalary);
        List<Employees> findByConds4(Employeesvo vo, int start, int pageSize);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    EmployeesMapper.xml:

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.cdcas.mapper.EmployeesMapper">
    
    
    
        <select id="findByConds1" resultType="com.cdcas.pojo.Employees">
        select * from employees where first_name like #{param1} and last_name like #{param2}
            and salary>=#{param3} and salary <=#{param4}
        select>
        <select id="findByConds2" resultType="com.cdcas.pojo.Employees">
            select * from employees where first_name like #{firstName} and last_name like #{lastName} 
            and salary>=#{minSalary} and salary <=#{maxSalary}
        select>
        <select id="findByConds3" resultType="com.cdcas.pojo.Employees">
            select * from employees where first_name like #{firstName} and last_name like #{lastName}
            and salary>=#{param3} and salary <=#{param4}
        select>
        <select id="findByConds4" resultType="com.cdcas.pojo.Employees">
            select * from employees where first_name like #{param1.firstName} and last_name like #{param1.lastName} 
            and salary>=#{param1.minSalary} and salary <=#{param1.maxSalary} limit #{param2}, #{param3}
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    之前的:
    在这里插入图片描述

    dao层和pojo单独抽成一个模块:
    在这里插入图片描述
    通过maven模块可以相互调用。和导入jar一样
    在这里插入图片描述

  • 相关阅读:
    【Python】使用Docker部署Python项目
    ITIL 4指导、计划和改进—沟通和组织变革管理
    P27 含并行连结的网络 GoogLeNet / Inception V3
    【async/await】--异步编程最终解决方案
    这个困扰程序员50年的问题,终于要被解决了?
    python+appium自动化测试如何控制App的启动和退出
    SpringBoot SpringBoot 原理篇 3 核心原理 3.3 启动流程【2】
    5-RabbitMQ工作模式-Publish/Subscribe发布与订阅模式
    Nvidia Triton使用教程:从青铜到王者
    VS Code For Web 深入浅出 -- 进程间通信篇
  • 原文地址:https://blog.csdn.net/weixin_53050118/article/details/126393851