mybatis中动态sql的使用

这里介绍基础的mabtis中动态sql的使用

导入必要的jar包

mybatis
JUnit4做测试需要用到的包
mysql操作数据库要用到的包
log4j本次测试使用了log4j.proirtes文件

mybaties_config.xml配置文件


    
    <configuration>
        
        <properties resource="db.properties">
            <property name="db.username" value="xxx"/>
            <property name="db.password" value="xxx"/>
        properties>
        
            <settings>
                <setting name="logImpl" value="LOG4J"/>
            settings>
        
            <typeAliases>
                <package name="cn.com.pojo"/>
            typeAliases>
        
        <environments default="dev">

            
            <environment id="dev">
                
                <transactionManager type="JDBC">transactionManager>
                
                <dataSource type="POOLED">
                    <property name="driver" value="${db.driver}"/>
                    <property name="url" value="${db.url}"/>
                    <property name="username" value="${db.username}"/>
                    <property name="password" value="${db.password}"/>
                dataSource>   
            environment>  
        environments>
        
        <mappers>
            
             <mapper resource="cn/com/pojo/UsertblMapper.xml"/> 
    mappers>
    configuration>

实体类

提供set() get() 以及toString方法

编写对应的mppe.xml文档 以及接口 和测试类

<mapper namespace="cn.com.mapper.xxx"> 

    <resultMap type="Studenttbl" id="stuResultMap">
        <id column="sid" property="sid"/>
        <result column="sname" property="sname"/>
        <collection property="coursetbls" javaType="list" ofType="Coursetbl">
            <id column="cid" property="cid"/>
            <result column="cname" property="cname"/>
        collection>
    resultMap>

    <select id="findbyCondition" parameterType="map" resultMap="stuResultMap">
                select * from studenttbl s where 1=1 
            <if test="sid!=null and sid != ''">
                and s.sid>#{sid}
            if>

            <if test="sname!=null and sname != ''">
                
            if>
    select>

提供相应接口
    public List<Studenttbl> findbyCondition(Map<String, Object> m);
测试类
    @Test
    public void test1(){
        SqlSession session=this.sessionFactory.openSession();
        StudenttblMapper mapper=session.getMapper(StudenttblMapper.class);
        Map<String, Object> map=new HashMap<>();
        map.put("sid", 2);
        //map.p );
        List<Studenttbl> ls=mapper.findbyCondition(map);



System.out.println(ls);
        session.close();
    }

下面的不包含具体的测试类,只介绍几个动态sql方法



<select id="findbywhere" parameterType="map" resultMap="stuResultMap">
        select * from studenttbl s 
            <where>
                <if test="sid!=null and sid != ''">
                    and s.sid>#{sid}
                if>
                <if test="sname!=null and sname != ''">
                     and s.sname=#{sname}
                if>
            where>
    select>


    <select id="findbyin" parameterType="List" resultMap="empResultMap">
        select * from emptbl where empid in 
        <foreach collection="list" item="it" open="(" close=")" separator=",">
            #{it}
        foreach>
    select>

  
    <select id="findbylike" parameterType="Emptbl" resultMap="empResultMap">
        select * from emptbl where empname like '%${empname}%'

    select>

    <select id="findbybind" parameterType="string" resultMap="empResultMap">
        <bind name="pattern" value="'%'+_parameter+'%'"/>
        select * from emptbl where empname like #{pattern}
    select>

你可能感兴趣的:(MYBATIS,mybatis)