mybaitis增删改查

本文介绍了如何使用MyBatis进行基本的增删改查操作。首先,文章提及了导入必要的jar包,然后将spring.xml配置文件放入src目录。接着,创建了User实体类,并定义了IUserDao接口,包含findAll、delete、update和insert方法。同时,展示了UserMapper1.xml的部分内容,用于映射SQL操作。最后,提供了一个测试类,通过SqlSession执行查询、删除、更新和插入操作,完成对数据库的CRUD操作。
摘要由CSDN通过智能技术生成

在这里插入图片描述
1.导入jar
2.spring.xml放入src
3.user 实体类
private int id;
private String username;
private String password;

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 + '\'' +
            '}';
}

}

IUserDao

package com.zhongruan.dao;

import com.zhongruan.bean.User;

import java.util.List;

public interface IUserDao {
List findAll();
void delete(int id);
void update(User user);
void insert(User user);
}

UserMapper1.xml

<?xml version="1.0" encoding="UTF-8"?>
<select id="findAll" resultType="com.zhongruan.bean.User">
    select *from tb_user;
</select>
<delete id="delete" parameterType="int">
    delete from tb_user where id=#{id};
</delete>
<update id="update" parameterType="com.zhongruan.bean.User">
   update tb_user set username=#{username},password=#{password} where id=#{id};
</update>

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

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;
import java.util.List;

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 users=session.selectList(“findAll”);
//System.out.println(users);
// session.delete(“delete”,587);
User user1=new User();
// user.setId(5);
// user.setUsername(“aaa”);
// user.setPassword(“123”);
// session.update(“update”,user);
user1.setUsername(“hehe”);
user1.setPassword(“aaaa”);
session.insert(“insert”,user1);
session.commit();
session.close();

}

}

sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值