Mybatis学习笔记(持续更新)

本文介绍了如何初步搭建Mybatis,包括创建Maven工程、引入依赖、配置Mybatis核心文件、创建Mapper接口及映射文件,并展示了增删查改的基本操作。还提到了日志功能的集成和Mybatis中${}

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.初步搭建Mybatis

1.创建maven工程

 2.引入依赖

<dependencies>
    <!-- Mybatis核心 -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.7</version>
    </dependency>
    <!-- junit测试 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!-- MySQL驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.3</version>
    </dependency>
</dependencies>

3.创建Mybatis核心配置文件mybatis-config.xml

<?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>
    <!--设置连接数据库的环境-->
    <environments default="development">
    <environment id="development">
    <transactionManager type="JDBC"/>
        <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver"/>
            <property name="url"
            value="jdbc:mysql://localhost:3306/MyBatis"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
        </dataSource>
    </environment>
    </environments>
	<!--引入映射文件-->
    <mappers>
    	<mapper resource="mappers/UserMapper.xml"/>
    </mappers>
</configuration>

4.创建mapper接口

public interface CustomerMapper {
  
    List<Customer> selectAll();

    int insert(Customer customer);

    Customer selectById(int id);

    int updateRelNameById(Customer customer);

    int  deleteById(int  id);

    List<Customer> selectByCondition(Customer condition);
}

5.创建mybatis映射文件

<?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.ytsky.mybatis.demo.dao.CustomerMapper">
    <select id="selectAll" resultType="Customer">
    select id, name, tel, address, rel_name, other
    from db_quote.tb_customer
    </select>
    <select id="selectById" resultType="Customer">
        select id, name, tel, address, rel_name , other
        from db_quote.tb_customer
        where id = #{id}
    </select>
    <select id="selectByCondition" parameterType="Customer" resultType="Customer">
        select id, name, tel, address, rel_name, other
        from db_quote.tb_customer
        <where>
            <if test="name != null">
                and name = #{name}
            </if>
            <if test="tel != null">
                and tel = #{tel}
            </if>
            <if test="address != null">
                and address like #{address}
            </if>
            <if test="relName != null">
                and rel_name = #{relName}
            </if>
            <if test="other != null">
                and other like #{other}
            </if>
        </where>
    </select>
    <insert id="insert" parameterType="Customer">
        insert into db_quote.tb_customer( name, tel, address, rel_name, other)
        VALUES(#{name},#{tel},#{address},#{relName},#{other})
    </insert>
    <update id="updateRelNameById" parameterType="Customer">
        update db_quote.tb_customer
        set rel_name= #{relName}
        where id= #{id}
    </update>

    <delete id="deleteById" parameterType="int">
        delete from db_quote.tb_customer where id = #{id}
    </delete>

</mapper> 

6. 加入log4J日志功能

1)加入依赖

<!-- log4j日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

2) 加入 log4j 的配置文件

    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--设置 mybatis在运行时-->
        <setting name="logImpl" value="LOG4J"/>
    </settings>

2.Mybatis的增删查改 

1.添加数据

    <insert id="insert" parameterType="Customer">
        insert into db_quote.tb_customer( name, tel, address, rel_name, other)
        VALUES(#{name},#{tel},#{address},#{relName},#{other})
    </insert>

2.删除数据

 <delete id="deleteById" parameterType="int">
        delete from db_quote.tb_customer where id = #{id}
    </delete>

 3.查询数据

    <select id="selectAll" resultType="Customer">
        select id, name, tel, address, rel_name, other
        from db_quote.tb_customer
    </select>
    <select id="selectById" resultType="Customer">
        select id, name, tel, address, rel_name , other
        from db_quote.tb_customer
        where id = #{id}
    </select>

4.修改数据

    <update id="updateRelNameById" parameterType="Customer">
        update db_quote.tb_customer
        set rel_name= #{relName}
        where id= #{id}
    </update>

3.Mybatis获取数据的方式 

1.MyBatis 获取参数值的两种方式:${}和 #{}

2.${}的本质就是字符串拼接,#{}的本质就是占位符赋值

3.${}使用字符串拼接的方式拼接 sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单引号;但是 #{}使用占位符赋值的方式拼接sql.此时为字符串类型或日期类型的字段进行赋值时,可以自动添加单引号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值