记录mybatis的使用方法

第一步打开intellij idea建立一个java 项目,我这里起名叫mybatis

第二步在项目跟目录建立lib目录,把需要的两个jar包导入

mybatis-3.4.2.jar

mysql-connector-java-5.0.8-bin.jar

第三步在src下建立这样一个包com.how2java.pojo,然后在建立个文件mybatis-config.xml

配置mybatis-config.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>
    <typeAliases>
        <package name="com.how2java.pojo"/>
    </typeAliases>
    <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:3306/how2java?characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/how2java/pojo/Category.xml"/>
    </mappers>
</configuration>

接着在com.how2java.pojo包下面建立一个类

Category:
package com.how2java.pojo;
public class Category {
    private int id;
    private String name;
    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;
    }
}

继续在这个包下面创建一个文件

Category.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.how2java.pojo">
    
    <insert id="addCategory" parameterType="Category">
        insert into category_(name) values (#{name})

    </insert>

    <delete id="deleteCategory" parameterType="Category">
        delete from category_ where id=#{id}
    </delete>

    <update id="updateCategory" parameterType="Category">
        update category_ set name=#{name} where id=#{id}

    </update>

    <select id="getCategory" parameterType="Category" resultType="Category">
        select * from category_ where id=#{id}
    </select>
    
    <select id="listCategory" resultType="Category">
        select * from   category_
    </select>
</mapper>

然后在how2java包下创建测试类:

TestMybatis代码如下:
package com.how2java;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

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 com.how2java.pojo.Category;

public class TestMybatis {

    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        /**
         * 插入一条记录
         */
        Category c1 = new Category();
        c1.setName("新增加的Category");
        session.insert("addCategory", c1);


        /**
         * 删除一条记录
         */
        Category c2 = new Category();
        c2.setId(3);
        session.delete("deleteCategory",c2);

        /**
         * 获取一条记录
         */

        Category c3 = session.selectOne("getCategory", 2);
        //System.out.println(c3.getName());
        c3.setName("tom");
        session.update("updateCategory", c3);

        /**
         * 查询所有记录
         */
        List<Category> cs = session.selectList("listCategory");
        for (Category c : cs) {
            System.out.println(c.getName());
        }

    }
}

ok,这里只是记录一下mybatis的基本用法,以备需要的时候使用!

贴出两个jar包的下载地址:链接:https://pan.baidu.com/s/1k-JgDI1urhPxD0mM5Ldtlw  提取码:oswc 

demo下载地址:链接:https://pan.baidu.com/s/1RLVfRjqo6GnVb5jWdl__NA  提取码:jwl0 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一铭11199

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值