使用mybatis完成增删改查日志信息

mysql数据库


create table t_user
(
    uid int primary key auto_increment,
    username varchar(20),
    password varchar(20),
    phone varchar(11),
    address varchar(50)
);


insert into t_user(username,password,phone,address) values('张三','666','18965423548','南阳');
insert into t_user(username,password,phone,address) values('李四','333','18754263548','许昌');
insert into t_user(username,password,phone,address) values('小美','123','18565234759','信阳');

mybatis所需jar包

使用mybatis完成增删改查日志信息_第1张图片

创建包

使用mybatis完成增删改查日志信息_第2张图片

在com.a.bean包在里面创建User实体类

package com.a.bean;


public class User {
private Integer uid;
private String username;
private String password;
private String phone;
private String address;

    public Integer getUid() {
        return uid;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getPhone() {
        return phone;
    }

    public String getAddress() {
        return address;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

创建mybatis.xml在里面配置环境信息





    
        
            
            
                
                
                
                

            
        
        
        
            
            
        
    

    
        
    

.在com.a.dao包中创建UserDao接口,在里面编写全查的代码

package com.a.dao;

import com.a.bean.User;

import java.util.List;

public interface UserDao {
    List selectAll();
    int userAdd(User user);

    int delete(int uid);

   User selectByuid(int uid);

   int update(User user);
}

在com.a.dao包中创建UserDao.xml文件在里面设置映射和通过select标签进行查询





    
    


    insert into t_user(username,password,phone,address) values(#{username},#{password},#{phone},#{address});


    
        delete  from t_user where uid=#{uid} ;
    



    
    update t_user set username=#{username},password=#{password},phone=#{phone},address=#{address} where uid=#{uid}
    






在com.a.test包中创建测试类UserTest,测试查询

package com.a.test;

import com.a.bean.User;
import com.a.dao.UserDao;
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.After;
import org.junit.Before;
import org.junit.Test;

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

public class UserTest {
    //加载核心配置文件的字节输入流
    InputStream stream =null;

    //创建SqlSessionFactory的构建对象--框架使用的是构建者模式
    SqlSessionFactoryBuilder builder = null;
    //通过构建对象加载配置文件的输入流获取SqlSessionFactory
    SqlSessionFactory factory = null;

    //通过工厂对象获取SqlSession对象--执行JDBC的操作
    SqlSession sqlSession = null;
    //通过SqlSession对象获取接口对应的代理对象
    UserDao userDao = null;
    @Before
    public void init() throws IOException {
        //加载核心配置文件的字节输入流
      stream = Resources.getResourceAsStream("mybatis.xml");

        //创建SqlSessionFactory的构建对象--框架使用的是构建者模式
         builder = new SqlSessionFactoryBuilder();
        //通过构建对象加载配置文件的输入流获取SqlSessionFactory
       factory = builder.build(stream);

        //通过工厂对象获取SqlSession对象--执行JDBC的操作
       sqlSession = factory.openSession();
        //通过SqlSession对象获取接口对应的代理对象
        userDao = sqlSession.getMapper(UserDao.class);
    }


    @Test
    public void  testSelectAll() throws IOException {

        //通过代理对象执行查询方法
        List userList = userDao.selectAll();
        //遍历集合
       for ( User user :userList){
           System.out.println(user);
       }
    }
@Test
    public void add() throws IOException {

        User user = new User();
        user.setUsername("aa");
        user.setPassword("fdsf");
        user.setPhone("10086");
        user.setAddress("sdgdsf");
        int i = userDao.userAdd(user);
        if(i>0){
            System.out.println("成功");
        }else {
            System.out.println("失败");
        }
    }

    @Test
    public void delete(){
        int i = userDao.delete(1);
        if(i>0){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
    }
   @Test
    public void selectByuid(){
        User user = userDao.selectByuid(2);
        System.out.println(user);
    }
@Test
    public void update(){
        //1.数据库回显
    User oldUser = userDao.selectByuid(2);
    System.out.println("修改前:"+oldUser);
//2.根据需要修改字段值
    oldUser.setUsername("修改测试");
    oldUser.setAddress("河南郑州");
//3.执行数据库更新
    userDao.update(oldUser);
//4.再次查询

    User newUser = userDao.selectByuid(2);
    System.out.println("修改后:"+newUser);

}

    @After
    public void drstroy() throws IOException {
        sqlSession.commit();
        sqlSession.close();
        stream.close();

    }
}

以下代码是打印日志信息所需代码

日志信息代码

使用mybatis完成增删改查日志信息_第3张图片

新加两个properties文件

jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mydb
jdbc.user=root
jdbc.password=root
mapper.dao=com.dao.EmpDao

log4j.properties



log4j.rootLogger=TRACE,stdout  

log4j.appender.stdout=org.apache.log4j.ConsoleAppender   
#log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout   


log4j.appender.logfile=org.apache.log4j.FileAppender   
log4j.appender.logfile.File=wocao.log   
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout   
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n   
log4j.logger.mapperNS =TRACE

log4j.logger.com.mybatis=DEBUG  
log4j.logger.com.mybatis.common.jdbc.SimpleDataSource=DEBUG   
log4j.logger.com.mybatis.common.jdbc.ScriptRunner=DEBUG   
log4j.logger.com.mybatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG   
log4j.logger.java.sql.Connection=DEBUG  
log4j.logger.java.sql.Statement=DEBUG  
log4j.logger.java.sql.PreparedStatement=DEBUG  
log4j.logger.java.sql.ResultSet=DEBUG  

log4j.logger.org.springframework=error 
log4j.logger.org.apache=ERROR  
log4j.logger.org.mybatis=DEBUG 

mybatis.xml更新代码





    
    
    
        
    
    
        
        
    

    
        
        




        
    
    
        
        
    

    
    
        
    

你可能感兴趣的:(mybatis,mysql,java)