欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MyBatis对MySQL数据库的增删改查操作代码详解

程序员文章站 2022-03-02 22:23:14
mybatis对mysql的增删改查操作代码详解 mysqlsessionfactory.java package factory; import org.apache.ibatis.i...

mybatis对mysql的增删改查操作代码详解

MyBatis对MySQL数据库的增删改查操作代码详解

mysqlsessionfactory.java

package factory;

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 java.io.inputstream;


public class mysqlsessionfactory {
   
   private static sqlsessionfactory sqlsessionfactory = null;

   static{
      // 初始化创建sqlsessionfactory对象
      try {
         inputstream inputstream = resources.getresourceasstream("mybatis-config.xml");
            // 读取mybatis-config.xml文件
         sqlsessionfactory = new sqlsessionfactorybuilder()
               .build(inputstream);
      } catch (exception e) {
         e.printstacktrace();
      }
   }

   public static sqlsession getsqlsession(){
        // 获取sqlsession对象的静态方法
      return sqlsessionfactory.opensession();
   }

   public static sqlsessionfactory getsqlsessionfactory() {
        // 获取sqlsessionfactory的静态方法
      return sqlsessionfactory;
   }


}

user.java

package pojo;

import java.io.serializable;
/**
 * create table `tb_user` (
  `id` int(11) not null auto_increment,
  `name` varchar(18) default null,
  `sex` char(2) default null,
  `age` int(11) default null,
  primary key  (`id`)
)
 */


public class user implements serializable{
   private integer id;
    // 用户标识
   private string name;
    // 用户姓名
   private string sex;
    // 用户性别
   private integer age;
    // 用户年龄

   public user() {
        // 无参数构造器
      super();
   }

   public user( string name, string sex, integer age) {
        // 有参数构造器
      super();
      this.name = name;
      this.sex = sex;
      this.age = age;
   }
    // setter和getter方法
   public integer getid() {
      return id;
   }
   public void setid(integer id) {
      this.id = id;
   }
   public string getname() {
      return name;
   }
   public void setname(string name) {
      this.name = name;
   }
   public string getsex() {
      return sex;
   }
   public void setsex(string sex) {
      this.sex = sex;
   }
   public integer getage() {
      return age;
   }
   public void setage(integer age) {
      this.age = age;
   }

   @override
   public string tostring() {
      return "user [id=" + id + ", name=" + name + ", sex=" + sex + ", age="
            + age + "]";
   }
   

}

db.properties

driver=com.mysql.jdbc.driver
url=jdbc:mysql://192.168.1.13:3306/mybatis?characterencoding=utf8&usessl=false
username=root
password=abcdef@123456

log4j.properties

log4j.rootlogger=error, stdout
# 全局的日志配置
log4j.logger.usermapper=debug
# mybatis的日志配置
log4j.appender.stdout=org.apache.log4j.consoleappender
log4j.appender.stdout.layout=org.apache.log4j.patternlayout
log4j.appender.stdout.layout.conversionpattern=%5p [%t] - %m%n
# 控制台输出

mybatis-config.xml



    系统的核心设置 -->


    

usermapper.xml







    
    
        insert into tb_user(name,sex,age)
        values(#{name},#{sex},#{age})
    

    
    
        select * from tb_user where id = #{id}
    

    
    
        update tb_user
        set name = #{name},sex = #{sex},age = #{age}
        where id = #{id}
    

    
    
        delete from tb_user where id = #{id}
    


deletetest.java

import factory.mysqlsessionfactory;
import org.apache.ibatis.session.sqlsession;


public class deletetest {

   public static void main(string[] args) throws exception {
      sqlsession session = mysqlsessionfactory.getsqlsession();
      // 获得session实例
      session.delete("usermapper.removeuser", 1);
        // 删除id为1的user对象
      session.commit();
        // 提交事务
      session.close();
        // 关闭session
   }


}

inserttest.java

import factory.mysqlsessionfactory;
import org.apache.ibatis.session.sqlsession;
import pojo.user;


public class inserttest {

   public static void main(string[] args) throws exception {
      sqlsession session = mysqlsessionfactory.getsqlsession();
      // 获得session实例
      user user = new user("jack", "男", 22);
        // 创建user对象
      session.insert("usermapper.saveuser", user);
        // 插入数据
      session.commit();
        // 提交事务
      session.close();
        // 关闭session
   }


}

selecttest.java

import factory.mysqlsessionfactory;
import org.apache.ibatis.session.sqlsession;
import pojo.user;


public class selecttest {

   public static void main(string[] args) throws exception {
      sqlsession session = mysqlsessionfactory.getsqlsession();
        // 获得session实例
      user user = session.selectone("usermapper.selectuser",1);
        // 根据id查询user对象
      system.out.println(user);
      session.commit();
        // 提交事务
      session.close();
        // 关闭session
   }


}

upadetest.java

import factory.mysqlsessionfactory;
import org.apache.ibatis.session.sqlsession;
import pojo.user;


public class upadetest {

   public static void main(string[] args) throws exception {
      sqlsession session = mysqlsessionfactory.getsqlsession();
        // 获得session实例
      user user = session.selectone("usermapper.selectuser",1);
        // 根据id查询user对象
      user.setname("tom");
      user.setage(25);
        // 修改user对象的属性值
      session.update("usermapper.modifyuser", user);
        // 修改user对象
      session.commit();
        // 提交事务
      session.close();
        // 关闭session
   }


}

pom.xml



    4.0.0

    shanghai
    mybatis
    1.0-snapshot

    
        
            org.testng
            testng
            6.11
            test
        

        
            org.mybatis
            mybatis
            3.3.1
        

        
            mysql
            mysql-connector-java
            5.1.45
        

        
            log4j
            log4j
            1.2.17
        
    

    
        
                org.apache.maven.plugins
                maven-surefire-plugin
                2.20.1
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.7.0
                
                    1.8
                    1.8
                
            utf-8
        utf-8