欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mybatis 02 对数据的CRUD(增删改查) 操作

程序员文章站 2022-05-08 08:37:57
...

1.导包

Mybatis 02 对数据的CRUD(增删改查) 操作

2.配置文件

<?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:3308/mybatis" />
                <property name="username" value="root" />
                <property name="password" value="210374520" />
            </dataSource>
        </environment>
    </environments>

</configuration>

3.创建实体类

package com.dhl.beyond.shiyan1;

public class User {
    private int id;  //用户的Id 或者是 学号
    private String name; //姓名
    private int age; //年龄

    public User() {
    }

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

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

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}


4.创建Mapper文件

<?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.dhl.beyond.shiyan3.userMapper">
    <select id="selectUser" parameterType="int" resultType="com.dhl.beyond.shiyan1.User">
        select * from users where id=#{id}
    </select>

    <insert id="insertUser" parameterType="com.dhl.beyond.shiyan1.User">
        insert into users(name,age) values(#{name},#{age})
    </insert>

    <update id="updateUser" parameterType="com.dhl.beyond.shiyan1.User">
        update users set name =#{name},age=#{age} where id = #{id}
    </update>

    <delete id="deleteUser" parameterType="int">
        delete from users where id=#{id}
    </delete>
</mapper>

5.注册

<mapper resource="com/dhl/beyond/shiyan3/userMapper.xml"/>

Mybatis 02 对数据的CRUD(增删改查) 操作

6.测试类

package com.dhl.beyond.shiyan3;

import com.dhl.beyond.shiyan1.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 org.junit.Test;

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

public class TestUser {
    @Test
    public void testSelectUser() throws IOException {
        String resource = "config.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        SqlSessionFactory sqlSessionFactory =  new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = sqlSessionFactory.openSession();
        String s = "com.dhl.beyond.shiyan3.userMapper.selectUser";
        User o = session.selectOne(s, 2);
        System.out.println(o);
    }

    @Test
    public void testInsertUser() throws IOException {
        String resouce = "config.xml";
        Reader reader = Resources.getResourceAsReader(resouce);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        String s = "com.dhl.beyond.shiyan3.userMapper.insertUser";
        SqlSession session = factory.openSession();
        User u = new User();
        u.setAge(20);
        u.setName("zhangsan");
        int insert = session.insert(s, u);
        System.out.println(insert);
    }

    @Test
    public void testUpdateUser() throws IOException {
        String resource = "config.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = factory.openSession();
        String s = "com.dhl.beyond.shiyan3.userMapper.updateUser";
        int update = session.update(s, new User(2, "lisi", 22));
        System.out.println(update);

    }

    @Test
    public void testDeleteUser() throws IOException{
        String resource = "config.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = factory.openSession();
        int delete = session.delete("com.dhl.beyond.shiyan3.userMapper.deleteUser", 3);
        System.out.println(delete);

    }
}

7.结果集

Mybatis 02 对数据的CRUD(增删改查) 操作
Mybatis 02 对数据的CRUD(增删改查) 操作

☯☯

Mybatis 02 对数据的CRUD(增删改查) 操作
Mybatis 02 对数据的CRUD(增删改查) 操作

☯☯☯

Mybatis 02 对数据的CRUD(增删改查) 操作
Mybatis 02 对数据的CRUD(增删改查) 操作

☯☯☯☯

Mybatis 02 对数据的CRUD(增删改查) 操作
Mybatis 02 对数据的CRUD(增删改查) 操作

目录结构

Mybatis 02 对数据的CRUD(增删改查) 操作