2019-05-20 Mybits

Mybits
需要在maven库中下载4个依赖

并且指定文件位置

    
        
        src/main/java
        
          **/*.xml
        
        false
      


    
        
        
        
            mysql
            mysql-connector-java
            5.1.39
        
        
        
        
            log4j
            log4j
            1.2.12
        
        
        
            commons-logging
            commons-logging
            1.2
        
        
        
        
            org.mybatis
            mybatis
            3.4.6
        
    

在resources中要有3个配置文件
db.properties

# 右键,属性--》utf-8
# 数据库配置文件 name = value的形式配置
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/java2demo?useUnicode=true&characterEncoding=utf8
jdbc.user=root
jdbc.password=root

log4j.properties

# Global logging configuration
#在开发环境日志级别要设置为DEBUG、生产环境要设置为INFO或者ERROR
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

SqlMapConfig.xml




    
    
    
    
        
        
        
        
    
    
    
        
            
            
                
                
                
                
            
        
    
    
    
        
    

查询类.xml文件实现接口中方法




    
    

test测试类

package com.neuedu.main;

import com.neuedu.mapper.UserinfoMapper;
import com.neuedu.pojo.Userinfo;
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.IOException;
import java.io.InputStream;

public class Test {
    public static void main(String[] args) {
//        调用mapper,完成数据库操作,在控制台打印
//        1.根据id查询用户
//        1.1 构建session工厂
        InputStream inputStream=null;
        SqlSessionFactory sqlSessionFactory=null;
        SqlSession sqlSession=null;
        try {
            inputStream= Resources.getResourceAsStream("SqlMapConfig.xml");
            sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//            1.2 创建sqlSession对象--代理对象
            sqlSession=sqlSessionFactory.openSession();
//            1.3 通过代理要对应的mapper对象
            UserinfoMapper userinfoMapper= sqlSession.getMapper(UserinfoMapper.class);
//            1.4 调用xml文件
            Userinfo userinfo=userinfoMapper.getUserfiByid(1);
            System.out.println(userinfo.getUid());
            System.out.println(userinfo.getName());
            System.out.println(userinfo.getAge());
            System.out.println(userinfo.getBirthday());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

增删改查

test

package com.neuedu.main;

import com.neuedu.mapper.UserinfoMapper;
import com.neuedu.pojo.Userinfo;
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.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;


public class Test {
    public static void main(String[] args) {
        // 调用mapper,完成数据库操作,在控制台打印
        // 1.根据id查询某个用户
        InputStream inputStream = null;
        SqlSessionFactory sqlSessionFactory = null;
        SqlSession sqlSession = null;
        try {
            // 1.1构建session工厂 -- 工厂生成对象
            inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            // 1.2创建sqlSession对象 -- 代理对象
            sqlSession = sqlSessionFactory.openSession();
            // 1.3通过代理要对应的mapper对象
            UserinfoMapper userinfoMapper = sqlSession.getMapper(UserinfoMapper.class);
            // 1.4调用方法得到返回值
//            Userinfo userinfo = userinfoMapper.getUserinfoById(2);
//            System.out.println(userinfo.getId());
//            System.out.println(userinfo.getName());
//            System.out.println(userinfo.getAge());
//            System.out.println(userinfo.getBirthday());

//            Map map = userinfoMapper.getUserinfoById2(1);
//            System.out.println(map);
//            for(Map.Entry entry : map.entrySet()){
//                System.out.println(entry.getKey()+","+entry.getValue());
//            }

//            List list = userinfoMapper.getUserinfoListByName("mike");
//            for (Userinfo userinfo : list) {
////                System.out.println(userinfo.getId());
////                System.out.println(userinfo.getName());
////                System.out.println(userinfo.getAge());
////                System.out.println(userinfo.getBirthday());
////                System.out.println("===================================");
////            }

//            List> list = userinfoMapper.getUserinfoListByName2("mike");
//            for(Map map : list){
//                for(Map.Entry entry : map.entrySet()){
//                    System.out.println(entry.getKey()+","+entry.getValue());
//                }
//                System.out.println("===================================");
//            }

//            Userinfo userinfo = new Userinfo();
//            userinfo.setAge(32);
//            userinfo.setName("mike");
//
//            Userinfo userinfo1 = userinfoMapper.getUserinfoByNameAndAge(userinfo);
//            System.out.println(userinfo1.getId()+","+userinfo1.getName());
//            System.out.println(userinfo1.getAge()+","+userinfo1.getBirthday());


            // 2.添加用户
//            Userinfo userinfo = new Userinfo();
//            userinfo.setAge(99);
//            userinfo.setName("lilei");
//            userinfo.setBirthday(new Date());
//
//            int num = userinfoMapper.insertUserinfo(userinfo);
//            System.out.println(num);
//            // 增删改需要提交
//            sqlSession.commit();

            // 3.修改用户
//            Userinfo userinfo = new Userinfo();
//            userinfo.setId(2);
//            userinfo.setName("duke");
//            userinfoMapper.updateUserinfoById(userinfo);
//            sqlSession.commit();

            // 4.根据id删除用户
//            userinfoMapper.delUserinfoById(4);
//            sqlSession.commit();

//            动态sql
            Userinfo userinfo = new Userinfo();
            userinfo.setName("dlq");
            userinfo.setId(8);
            userinfo.setAge(42);
            // {name=leo,age=12,birthday=null,id=0}
//            List list = userinfoMapper.getUserinfoNoGD(userinfo);
//            for(Userinfo userinfo1 : list){
//                System.out.println(userinfo1.getId()+","+userinfo1.getName());
//                System.out.println(userinfo1.getAge()+","+userinfo1.getBirthday());
//                System.out.println("=====================================");
//            }
            userinfoMapper.updateUserinfoNoGD(userinfo);
            sqlSession.commit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

接口

package com.neuedu.mapper;

import com.neuedu.pojo.Userinfo;

import java.util.List;
import java.util.Map;

public interface UserinfoMapper{

//     根据id查询某个用户
    Userinfo getUserinfoById(int id);
    Map getUserinfoById2(int id);
    // 根据姓名查询用户
    List getUserinfoListByName(String name);
    List> getUserinfoListByName2(String name);
    // 根据名字和年龄查询用户
    Userinfo getUserinfoByNameAndAge(Userinfo userinfo);
    // 添加用户
    int insertUserinfo(Userinfo userinfo);
    // 修改用户
    int updateUserinfoById(Userinfo userinfo);
    // 根据id删除
    int delUserinfoById(Integer id);
    // 动态SQL
    List getUserinfoNoGD(Userinfo userinfo);
    // 修改
    int updateUserinfoNoGD(Userinfo userinfo);
}

实现xml




    
    
    
    
    
    
    
    
    
    
        insert into userinfo(name,age,birthday)
        values (#{name},#{age},#{birthday})
    
    
    
        update userinfo
        set name = #{name}
        where id = #{id}
    
    
    
        delete from userinfo
        where id = #{value}
    
    
    
    
        update userinfo
        
           name = #{name}
           
                ,age = #{age}
           
         
        where id = #{id}
    

数据插入时返回主键值

    
        
        
            select LAST_INSERT_ID()
        
        insert into user(username, birthday, sex, address)
        values(#{username}, #{birthday}, #{sex}, #{address})
    

你可能感兴趣的:(2019-05-20 Mybits)