mybatis注解开发-环境搭建

环境搭建及测试

jar包的引用



    4.0.0

    com.study
    annotation_mybatis
    1.0-SNAPSHOT
    jar


    
        
            org.mybatis
            mybatis
            3.2.8
        
        
            mysql
            mysql-connector-java
            5.1.32
        
        
            log4j
            log4j
            1.2.17
        
        
            junit
            junit
            4.12
        
    

主配置文件SqlMapConfig.xml





    
    
    
    
        
    
    
    
        
            
            
            
            
                
                
                
                
            
        
    
    
    
        
    
    

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///mybatisdb
jdbc.username=root
jdbc.password=123456

实体类和接口

实体类
public class User {

    private Integer id;
    private String username;
    private String address;
    private String sex;
    private Date birthday;

    get/set方法
}
接口
package com.mybatis.dao;

import com.mybatis.model.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;

public interface UserDao {

    @Select("select * from user")
    List findAll();
}

测试方法

package com.mybatis.test;

import com.mybatis.dao.UserDao;
import com.mybatis.model.User;
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.Test;

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

public class MybatsiAnnotationTest {
    @Test
    public void test1() throws IOException {
        String resource = "SqlMapConfig.xml";
        //获取字节输入流
        InputStream is = Resources.getResourceAsStream(resource);
        //根据字节输入流构建SqlSessionFactory
        SqlSessionFactory factory =new  SqlSessionFactoryBuilder().build(is);
        //使用SqlSessionFactory生产一个SqlSession
        SqlSession session = factory.openSession();
        //使用SqlSession获取Dao的代理对象
        UserDao userDao = session.getMapper(UserDao.class);
        //执行方法
        List list = userDao.findAll();
        for (User u:list) {
            System.out.println(u);
        }
        //释放资源
        session.close();
        is.close();

    }
}

日志log4j.properties

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

 

你可能感兴趣的:(------【mybatis】)