99、【JavaEE】【Mybatis】多条件查询等

2021-10-15  本文已影响0人  yscyber

1、#{}

1.1、概述

1.2、举例

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Xxx {

    private Integer id;

    private String name;

}
public interface XxxRepo { 
    
    int deleteXxxById(Integer id);
    
}
<?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">

<mapper namespace="com.yscyber.mybatis.one.repo.XxxRepo">

    <delete id="deleteXxxById" parameterType="int">
        DELETE FROM xxx
        WHERE id=#{id}
    </delete>

</mapper>
<?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>
    
    <properties resource="jdbc.properties"/>

    <typeAliases>
        <typeAlias type="com.yscyber.mybatis.one.Xxx" alias="Xxx"/>
    </typeAliases>
    
    <environments default="dev">
        <!-- 配置数据源 -->
        <environment id="dev">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc_driver}"/>
                <property name="url" value="${jdbc_url}"/>
                <property name="username" value="${jdbc_username}"/>
                <property name="password" value="${jdbc_password}"/>
            </dataSource>
        </environment>
    </environments>

    <!-- 加载映射文件 -->
    <mappers>
        <mapper class="com.yscyber.mybatis.one.repo.XxxRepo"/>
    </mappers>
    
</configuration>
    @Test
    public void test7() {
        try {
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
            SqlSession sqlSession = sqlSessionFactory.openSession();

            XxxRepo xxxRepo = sqlSession.getMapper(XxxRepo.class);
            xxxRepo.deleteXxxById(1);
            sqlSession.commit();

            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Xxx {

    private Integer id;

    private String name;

}
public interface XxxRepo {

    int insertXxx(Integer id, String name);

}
<?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">

<mapper namespace="com.yscyber.mybatis.one.repo.XxxRepo">

    <insert id="insertXxx">
        INSERT INTO xxx(id, name)
        VALUES (#{param1}, #{param2})
    </insert>

</mapper>
<?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>
    
    <properties resource="jdbc.properties"/>

    <typeAliases>
        <typeAlias type="com.yscyber.mybatis.one.Xxx" alias="Xxx"/>
    </typeAliases>
    
    <environments default="dev">
        <!-- 配置数据源 -->
        <environment id="dev">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc_driver}"/>
                <property name="url" value="${jdbc_url}"/>
                <property name="username" value="${jdbc_username}"/>
                <property name="password" value="${jdbc_password}"/>
            </dataSource>
        </environment>
    </environments>

    <!-- 加载映射文件 -->
    <mappers>
        <mapper class="com.yscyber.mybatis.one.repo.XxxRepo"/>
    </mappers>
    
</configuration>
    @Test
    public void test7() {
        try {
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
            SqlSession sqlSession = sqlSessionFactory.openSession();

            XxxRepo xxxRepo = sqlSession.getMapper(XxxRepo.class);
            xxxRepo.insertXxx(1, "555");
            sqlSession.commit();

            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Xxx {

    private Integer id;

    private String name;

}
import org.apache.ibatis.annotations.Param;

public interface XxxRepo {

    int insertXxx(@Param(value = "id") Integer id, @Param(value = "name") String name);

}
<?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">

<mapper namespace="com.yscyber.mybatis.one.repo.XxxRepo">

    <insert id="insertXxx">
        INSERT INTO xxx(id, name)
        VALUES (#{id}, #{name})
    </insert>

</mapper>
<?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>
    
    <properties resource="jdbc.properties"/>

    <typeAliases>
        <typeAlias type="com.yscyber.mybatis.one.Xxx" alias="Xxx"/>
    </typeAliases>
    
    <environments default="dev">
        <!-- 配置数据源 -->
        <environment id="dev">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc_driver}"/>
                <property name="url" value="${jdbc_url}"/>
                <property name="username" value="${jdbc_username}"/>
                <property name="password" value="${jdbc_password}"/>
            </dataSource>
        </environment>
    </environments>

    <!-- 加载映射文件 -->
    <mappers>
        <mapper class="com.yscyber.mybatis.one.repo.XxxRepo"/>
    </mappers>
    
</configuration>
    @Test
    public void test7() {
        try {
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
            SqlSession sqlSession = sqlSessionFactory.openSession();

            XxxRepo xxxRepo = sqlSession.getMapper(XxxRepo.class);
            xxxRepo.insertXxx(1, "555");
            sqlSession.commit();

            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Xxx {

    private Integer id;

    private String name;

}
import com.yscyber.mybatis.one.Xxx;

public interface XxxRepo {

    int insertXxx(Xxx xxx);

}
<?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">

<mapper namespace="com.yscyber.mybatis.one.repo.XxxRepo">

    <insert id="insertXxx" parameterType="Xxx">
        INSERT INTO xxx(id, name)
        VALUES (#{id}, #{name})
    </insert>

</mapper>
<?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>
    
    <properties resource="jdbc.properties"/>

    <typeAliases>
        <typeAlias type="com.yscyber.mybatis.one.Xxx" alias="Xxx"/>
    </typeAliases>
    
    <environments default="dev">
        <!-- 配置数据源 -->
        <environment id="dev">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc_driver}"/>
                <property name="url" value="${jdbc_url}"/>
                <property name="username" value="${jdbc_username}"/>
                <property name="password" value="${jdbc_password}"/>
            </dataSource>
        </environment>
    </environments>

    <!-- 加载映射文件 -->
    <mappers>
        <mapper class="com.yscyber.mybatis.one.repo.XxxRepo"/>
    </mappers>
    
</configuration>
    @Test
    public void test7() {
        try {
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
            SqlSession sqlSession = sqlSessionFactory.openSession();

            XxxRepo xxxRepo = sqlSession.getMapper(XxxRepo.class);
            xxxRepo.insertXxx(new Xxx(4, "666"));
            sqlSession.commit();

            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

2、${}

2.1、概述

3、多条件查询

4、模糊查询

MyBatis-13 MyBatis-14
上一篇下一篇

猜你喜欢

热点阅读