Mybatis学习 - CRUD操作(增删改查操作)

Posted 与其临渊羡鱼,不如退而结网

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis学习 - CRUD操作(增删改查操作)相关的知识,希望对你有一定的参考价值。

直接上例子:

1.项目结构:

技术分享

2.具体代码及配置

User.java

package com.mybatis.bean;

public class User {

    private int id;
    private String name;
    private int age;
    
    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public User(){}
    
    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;
    }
    
}

 GetFacotry.java

package com.mybatis.util;

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

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class GetFactory {

    public static SqlSessionFactory getFactory(){
        
        Reader reader= null;
        try {
            reader = Resources.getResourceAsReader("conf.xml");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        SqlSessionFactory sessionfactory = new SqlSessionFactoryBuilder().build(reader);
        
        return sessionfactory;
    }
}

配置文件 conf.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 name="url" value="jdbc:mysql://localhost:3306/Mybatis" />
                <property name="username" value="root" />
                <property name="password" value="123456" />
         </dataSource>
       </environment>
    </environments>
    <mappers>
        <mapper resource="User.xml"></mapper>
    </mappers>
</configuration>

 

 配置文件 User.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.mybatis.bean.UserMapper">
    <select id="getUser" parameterType="int" resultType="com.mybatis.bean.User">
       select * from user where id=#{id}
    </select>
    <!--  
       CRUD操作
    -->
    <insert id="addUser" parameterType="com.mybatis.bean.User">
       insert into user (name,age) values (#{name},#{age});
    </insert>
    <update id="updateUser" parameterType="com.mybatis.bean.User">
       update user set name=#{name},age=#{age} where id=#{id}
    </update>
    <delete id="deleteUser" parameterType="int">
       delete from user where id=#{id}
    </delete>
    <select id="selectAllUsers" resultType="com.mybatis.bean.User">
       select * from user
    </select>
</mapper>

 

3.单元测试

test.java

package com.mybatis.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;

import com.mybatis.bean.User;
import com.mybatis.util.GetFactory;

public class test {
    
    @Test
    public void UnitTest_ADD(){
        SqlSessionFactory factory = GetFactory.getFactory();
        SqlSession session = factory.openSession();
        
        String statment = "com.mybatis.bean.UserMapper.addUser";
        User user = new User(-1,"新增",32);
        int insert = session.insert(statment,user);
        session.commit();
        session.close();
        System.out.println(insert);
    }
    
    @Test
    public void UnitTest_UPDATE(){
        SqlSessionFactory factory = GetFactory.getFactory();
        SqlSession session = factory.openSession();
        
        String statment = "com.mybatis.bean.UserMapper.updateUser";
        User user = new User(1,"更新",22);
        int insert = session.update(statment,user);
        session.commit();
        session.close();
        System.out.println(insert);
        
    }
    
    @Test
    public void UnitTest_DELETE(){
        SqlSessionFactory factory = GetFactory.getFactory();
        SqlSession session = factory.openSession();
        
        String statment = "com.mybatis.bean.UserMapper.deleteUser";
        int delete = session.delete(statment, 2);
        session.commit();
        session.close();
        System.out.println(delete);
        
    }
    
    @Test
    public void UnitTest_SELECT_ALL(){
        SqlSessionFactory factory = GetFactory.getFactory();
        SqlSession session = factory.openSession();
        
        String statment = "com.mybatis.bean.UserMapper.selectAllUsers";
        List<User> list= session.selectList(statment);
        session.close();
        System.out.println(list);
    }

}

 

以上是关于Mybatis学习 - CRUD操作(增删改查操作)的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis 学习总结 02 对表执行增删改查(CRUD)操作 OLD

ssm 框架实现增删改查CRUD操作(Spring + SpringMVC + Mybatis 实现增删改查)

mybatis 实现增删改查(CRUD)

Mybatis实现增删改查(CRUD)

mybatis 批量增删改查操作的几种操作以及创建数据库索引

mybatis生成的增删改查怎么用