资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

利用mybatis怎么对数据库进行增删改查操作

这篇文章将为大家详细讲解有关利用mybatis怎么对数据库进行增删改查操作,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

十多年的顺城网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网整合营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整顺城建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“顺城网站设计”,“顺城网站推广”以来,每个客户项目都认真落实执行。

所需要用到的其他工具或技术:

项目管理工具 : Maven

测试运行工具 : Junit

数据库 : Derby

Maven Dependencies:

 
     
      org.mybatis 
      mybatis 
      3.2.7 
     
     
      junit 
      junit 
      4.9 
      test 
     
     
      org.apache.derby 
      derby 
      10.10.2.0 
     
     
      org.apache.derby 
      derbyclient 
      10.10.2.0 
     
   

SQL 建表及数据插入(沿用前两节中的数据库表及数据):

CREATE TABLE USER_TEST_TB(  
ID INT PRIMARY KEY,  
USERNAME VARCHAR(20) NOT NULL,  
PASSWORD VARCHAR(20) NOT NULL,  
NICKNAME VARCHAR(20) NOT NULL  
);    
INSERT INTO USER_TEST_TB VALUES(1,'1st','111','Jack');  
INSERT INTO USER_TEST_TB VALUES(2,'2nd','222','Rose');  
INSERT INTO USER_TEST_TB VALUES(3,'3rd','333','Will');  

Mybatis配置文件 src/main/resource源目录下

test-mybatis-configuration.xml

<?xml version="1.0" encoding="UTF-8" ?>  
 
 
   
     
     
   
   
     
       
       
         
         
       
     
   
   
     
  

 User.java对象类(src/main/java/com/bjpowernode/practice目录下)

package com.bjpowernode.practice;  
public class User 
{ 
  private Integer id; 
  private String username; 
  private String password; 
  private String nickname; 
  public Integer getId() 
  { 
    return id; 
  } 
  public void setId(Integer id) 
  { 
    this.id = id; 
  } 
  public String getUsername() 
  { 
    return username; 
  } 
  public void setUsername(String username) 
  { 
    this.username = username; 
  } 
  public String getPassword() 
  { 
    return password; 
  } 
  public void setPassword(String password) 
  { 
    this.password = password; 
  } 
  public String getNickname() 
  { 
    return nickname; 
  } 
  public void setNickname(String nickname) 
  { 
    this.nickname = nickname; 
  } 
} 

UserMapper.xml  Mapper文件(src/main/java/com.bjpowernode.practice目录下)

<?xml version="1.0" encoding="UTF-8" ?>  
 
 
   
   
   
   
    insert into  
      USER_TEST_TB  
    values(#{id},#{username},#{password},#{nickname}) 
   
   
   
    update USER_TEST_TB set 
      USERNAME = #{username}, 
      PASSWORD = #{password}, 
      NICKNAME = #{nickname} 
    where ID = #{id} 
   
   
   
    delete from USER_TEST_TB WHERE ID=#{id} 
   
  

UserMapper.java Mapper类(src/main/java/com.bjpowernode.practice目录下)

package com.bjpowernode.practice; 
import java.util.List; 
public interface UserMapper 
{ 
  public List getUser(); 
  public void insertUser(User user); 
  public void updateUser(User user); 
  public void deleteUser(int userId); 
} 

测试类TestMyBatis.java(src/test/java/com.bjpowernode.practice目录下)

package com.bjpowernode.practice;  
import java.io.InputStream; 
import java.text.MessageFormat; 
import java.util.List; 
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; 
public class TestMyBatis 
{ 
  private String source; 
  private InputStream inputStream; 
  private SqlSessionFactory sqlSessionFactory; 
  @Before 
  public void setUp() 
  { 
    /** 
     * 准备Mybatis运行环境 
     */ 
    source = "test-mybatis-configuration.xml"; 
    inputStream = TestMyBatis.class.getClassLoader().getResourceAsStream(source); 
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 
  } 
  @Test 
  public void testGet() 
  { 
    try 
    { 
      // 获取Session连接 
      SqlSession session = sqlSessionFactory.openSession(); 
      // 获取Mapper 
      UserMapper userMapper = session.getMapper(UserMapper.class); 
      // 显示User信息 
      System.out.println("Test Get start..."); 
      this.printUsers(userMapper.getUser()); 
      System.out.println("Test Get finished..."); 
    } 
    catch (Exception e) 
    { 
      e.printStackTrace(); 
    } 
  } 
  @Test 
  public void testInsert() 
  { 
    try 
    { 
      // 获取Session连接 
      SqlSession session = sqlSessionFactory.openSession(); 
      // 获取Mapper 
      UserMapper userMapper = session.getMapper(UserMapper.class); 
      System.out.println("Test insert start..."); 
      // 显示插入之前User信息 
      System.out.println("Before insert"); 
      this.printUsers(userMapper.getUser()); 
      // 执行插入 
      userMapper.insertUser(this.mockUser("FREU_INS_USER", "BJPOWERNODE_INS_PASS", "BJPOWERNODE_INS_NICK")); 
      // 提交事务 
      session.commit(); 
      // 显示插入之后User信息 
      System.out.println("\r\nAfter insert"); 
      this.printUsers(userMapper.getUser()); 
      System.out.println("Test insert finished..."); 
    } 
    catch (Exception e) 
    { 
      e.printStackTrace(); 
    } 
  } 
  @Test 
  public void testUpdate() 
  { 
    try 
    { 
      // 获取Session连接 
      SqlSession session = sqlSessionFactory.openSession(); 
      // 获取Mapper 
      UserMapper userMapper = session.getMapper(UserMapper.class); 
      System.out.println("Test update start..."); 
      // 显示更新之前User信息 
      System.out.println("Before update"); 
      this.printUsers(userMapper.getUser()); 
      // 执行更新 
      userMapper.updateUser(this.mockUser("FREU_UPD_USER", "BJPOWERNODE_UPD_PASS", "BJPOWERNODE_UPD_NICK")); 
      // 提交事务 
      session.commit(); 
      // 显示更新之后User信息 
      System.out.println("\r\nAfter update"); 
      this.printUsers(userMapper.getUser()); 
      System.out.println("Test update finished..."); 
    } 
    catch (Exception e) 
    { 
      e.printStackTrace(); 
    } 
  } 
  @Test 
  public void testDelete() 
  { 
    try 
    { 
      // 获取Session连接 
      SqlSession session = sqlSessionFactory.openSession(); 
      // 获取Mapper 
      UserMapper userMapper = session.getMapper(UserMapper.class); 
      System.out.println("Test delete start..."); 
      // 显示删除之前User信息 
      System.out.println("Before delete"); 
      this.printUsers(userMapper.getUser()); 
      // 执行删除 
      userMapper.deleteUser(this.mockUser(null, null, null).getId()); 
      // 提交事务 
      session.commit(); 
      // 显示删除之后User信息 
      System.out.println("\r\nAfter delete"); 
      this.printUsers(userMapper.getUser()); 
      System.out.println("Test delete finished..."); 
    } 
    catch (Exception e) 
    { 
      e.printStackTrace(); 
    } 
  } 
  /** 
   * 
   * 组装一个User对象 
   * 
   * @return 
   */ 
  public User mockUser(String username, String password, String nickname) 
  { 
    User user = new User(); 
    user.setId(10); 
    user.setUsername(username); 
    user.setPassword(password); 
    user.setNickname(nickname); 
    return user; 
  } 
  /** 
   * 
   * 打印用户信息到控制台 
   * 
   * @param users 
   */ 
  private void printUsers(final List users) 
  { 
    int count = 0; 
    for (User user : users) 
    { 
      System.out.println(MessageFormat.format("==User[{0}]=================", ++count)); 
      System.out.println("User Id: " + user.getId()); 
      System.out.println("User UserName: " + user.getUsername()); 
      System.out.println("User Password: " + user.getPassword()); 
      System.out.println("User nickname: " + user.getNickname()); 
    } 
  } 
} 

关于利用mybatis怎么对数据库进行增删改查操作就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


分享名称:利用mybatis怎么对数据库进行增删改查操作
URL标题:http://cdkjz.cn/article/piegcp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220