mybatis实现增删改查

新建一个mybatis项目
在web下新建一个lib,向lib内导入jar包
在这里插入图片描述

并创建如下文件:
在这里插入图片描述

在User内输入如下代码:

package com.zhongruan.bean;

public class User {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

在dao文件下的IUserDao内输入:

package com.zhongruan.dao;

import com.zhongruan.bean.User;

import java.util.List;

public interface IUserDao {
    List<User> findAll();
    void deleteById(int id);
    void updateById(User user);
    void insert(User user);
}

在UserMapper1.xml内输入:

<?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.zhongruan.dao.IUserDao">

    <select id="findAll" resultType="com.zhongruan.bean.User">
        select * from tb_user
    </select>

    <delete id="deleteById" parameterType="int">
        delete from tb_user where id=#{id}
    </delete>

    <delete id="updateById" parameterType="com.zhongruan.bean.User">
       update tb_user set username=#{username},password=#{password} where id=#{id}
    </delete>

    <delete id="insert" parameterType="com.zhongruan.bean.User">
      insert into tb_user (username,password) value (#{username},#{password})
    </delete>


</mapper>

在Test内输入:

package com.zhongruan.test;

import com.zhongruan.bean.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class Test {
    public static void main(String[] args) throws IOException {
        Reader reader= Resources.getResourceAsReader("sqlMapConfig.xml");
        SqlSessionFactory build=new SqlSessionFactoryBuilder().build(reader);
        SqlSession session=build.openSession();
        List<User> users=session.selectList("findAll");
       System.out.println(users);

        //session.delete("deleteById",4);

        User user=new User();
        user.setId(21);
        user.setUsername("yyqx");
       user.setPassword("333");
        session.update("updateById",user);

        User user=new User();
        user.setUsername("zgddan");
        user.setPassword("333");
        session.insert("insert",user);
        session.commit();
        session.close();
    }

}

最后在sqlMapConfig.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"></transactionManager>
            <!--连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql://localhost:3306/zjgm?characterEncoding=utf-8"></property>
                <property name="username" value="root"></property>
                <property name="password" value="123456"></property>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/zhongruan/dao/UserMapper1.xml"></mapper>
    </mappers>


</configuration>

完成以上所有代码就完成了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值