MyBatis操作数据库

创建项目
使用IDEA创建Maven工程
IDEA配置Maven
设置好Maven版本、配置文件以及Maven仓库

连接数据库

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	    <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
	    <property name="url" value="jdbc:mysql://localhost:3306/bysj?characterEncoding=UTF-8&amp;useSSL=false&amp;serverTimezone=GMT%2B8"/>
	    <property name="username" value="root"/>
	    <property name="password" value="123456"/>
	 </bean>
	 

SQL语句
select语句

<select id="load" parameterType="java.lang.Integer" resultMap="noteResult">
        select * from note where id=#{id}
    </select>

select多条语句

 <select id="query" parameterType="note" resultMap="noteResult">
        select * from note
        <where>
            <if test="id != null and id != ''">
                and id like concat('%',#{id},'%')
            </if>
            <if test="title != null and title != ''">
                and title like concat('%',#{title},'%')
            </if>
            <if test="author != null and author != ''">
                and author like concat('%',#{author},'%')
            </if>
            <if test="content != null and content != ''">
                and content like concat('%',#{content},'%')
            </if>
        </where>
    </select>

insert语句

 <insert id="add" parameterType="note">
        insert into note(title,author,content) values(#{title},#{author},#{content})
    </insert>

update语句

  <update id="update" parameterType="person">
        update note set title=#{title},author=#{author},content=#{content} where id= #{id}
    </update>

delete语句

    <delete id="delete" parameterType="java.lang.Integer">
        delete from note where id=#{id}
    </delete>

执行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值