mybatis的crud

新建一张test表,有id,name两个字段

配置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.yzh.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/yzh_mybatis?characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/yzh/pojo/test.xml"/>
    </mappers>
</configuration>

新建表对应的实体类test.java 生成get/set方法

package com.yzh.pojo;

public class Test {
     
        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;
        }
         
}
 

新建实体映射类配置文件test.xml

在配置文件里面写入crud方法,如下

<?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.yzh.pojo">
        <insert id="addTest" parameterType="Test" >
            insert into test ( name ) values (#{name})   
        </insert>
         
        <delete id="deleteTest" parameterType="Test" >
            delete from test where id= #{id}  
        </delete>
         
        <select id="getTest" parameterType="_int" resultType="Test">
            select * from   test  where id= #{id}   
        </select>
 
        <update id="updateTest" parameterType="Test" >
            update test set name=#{name} where id=#{id}   
        </update>
        <select id="listTest" resultType="Test">
            select * from   test     
        </select>    
    </mapper>

 

新建一个测试类

package com.yzh.test;

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.yzh.pojo.Test;
 
 
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();
         Test test=new Test();
         test.setId(3);
         test.setName("新增的test3");
         session.insert("addTest", test);
         
         listAll(session);
         
        session.commit();
        session.close();
    }
    private static void listAll(SqlSession session) {
        List<Test> cs=session.selectList("listTest");
        for (Test c : cs) {
            System.out.println(c.getName());
        }
    }
}
运行测试类获取表里面的name集合

数据库表里面的name集合

 

下面是删除操作,在测试类里面修改

//删除  根据id删除
         Test test=new Test();
         test.setId(2);
         session.insert("deleteTest", test);

//修改 根据id修改 

Test test=new Test();
         test.setId(3);
         test.setName("修改的test3");
         session.insert("updateTest", test);

//查看

         Test c= session.selectOne("getTest",3);
         System.out.println(c.getName());

 

 

java学习    http://h5ip.cn/study

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值