mybatis完整增删改查入门实例

mybatis完整增删改查入门实例

编程步骤

①创建Java工程。

②导入jar包。

③创建sqlMapConfig.xml配置文件,这是mybatis的入口。

④创建数据库,数据库表user_c,插入测试记录。

⑤创建PO对象user.java.

⑥创建映射文件user.xml.

⑦创建测试类。

mybatis的占位符

#{遵循Java变量名规范的名称均可}

导入核心jar包

mybatis-3.2.2.jar

依赖jar包

asm-3.3.1.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
javassist-3.17.1-GA.jar
log4j-1.2.17.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar

核心配置文件sqlMapConfig.xml

事务:JDBC/MANAGED

数据源:UNPOOLED/POOLED/JNDI




    
        
            
            
                
                
                
                
            
        
    

    
        
    

创建数据库

创建mybatisdb数据库,创建User表。

sql语句:

create table USER_C

(

   ID   varchar(40) not null,

   NAME varchar(30),

   AGE  int,

   ADDRESS  varchar(200),

   primary key (ID)

);

insert  into `user_c`(`id`,`name`,`age`,`address`)

values ('1','夏言',73,'桂州村'),

('2','严嵩',87,'分宜县城介桥村'),

('3','徐阶',80,'明松江府华亭县'),

('4','高拱',66,'河南省新郑市高老庄村'),

('5','张居正',58,'江陵');

配置映射文件User.xml






    
    
         id,name,age,address       
    
    
    

      
    

    
    
        insert into user_c(  ) values (#{id},#{name},#{age},#{address})    
    

    
    
        update user_c set name=#{name},age=#{age},address=#{address} where id=#{id}    
    

    
    
        delete from user_c where id= #{id}   
    

PO对象User.java

package cn.nwtxxb.domain;

public class User {
    private String id;
    private String name;
    private Integer age;
    private String address;
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
    }

}

测试类

package cn.nwtxxb.test;

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

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.Before;
import org.junit.Test;

import cn.nwtxxb.domain.User;

public class TestMybatis {

    SqlSessionFactory sqlSessionFactory;

    @Before
    public void initFactory() throws IOException
    {
        String resource = "sqlMapConfig.xml";

        InputStream inputStream = Resources.getResourceAsStream(resource);

        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public  void testListAll()
    {
        SqlSession session=sqlSessionFactory.openSession();
        List users=session.selectList("cn.nwtxxb.mybatis.listAll");
        System.out.println(users.size());
    }
    @Test
    public void testQueryOne()
    {
        SqlSession session=sqlSessionFactory.openSession();
        User user=session.selectOne("cn.nwtxxb.mybatis.getOne", 1);
        System.out.println(user);
    }
    //事务需要程序员处理
    @Test
    public void testInsertOne()
    {
        User u=new User();
        u.setId(UUID.randomUUID().toString());
        u.setName("sbsbb");
        u.setAge(18);
        u.setAddress("china");
        SqlSession session=sqlSessionFactory.openSession();
        int count=session.insert("cn.nwtxxb.mybatis.insertOne", u);
        session.commit();
        System.out.println(count);
    }

    @Test
    public void testUpdateOne()
    {
        SqlSession session=sqlSessionFactory.openSession();
        User u=new User();
        //u=session.selectOne("cn.nwtxxb.mybatis.getOne", "2");
        u.setId("2");
        u.setName("clclclclclcfei");
        u.setAge(100);
        u.setAddress("USA");
        int count=session.update("cn.nwtxxb.mybatis.updateOne", u);
        session.commit();
        System.out.println(count);
    }
    @Test
    public void testDeleteOne()
    {
        SqlSession session=sqlSessionFactory.openSession();
        User u=new User();
        u.setId("2");
        int count=session.delete("cn.nwtxxb.mybatis.deleteOne", u);
        session.commit();
        System.out.println(count);
    }

}

你可能感兴趣的:(后台编程,经验分享,学亮说JAVA)