Mybatis+PostgreSQL+Maven的一些配置

一、看一下项目结构吧
项目结构.png

二、POM文件的一些配置


  4.0.0

  zhd.cn
  Mybatis
  0.0.1-SNAPSHOT
  jar

  Mybatis
  http://maven.apache.org

  
    UTF-8
  

  
        
            org.mybatis
            mybatis
            3.4.6
        
        
              junit
              junit
              3.8.1
          test
        
            
            log4j
            log4j
            1.2.12
        
        
            org.postgresql
            postgresql
             9.3-1102-jdbc41
        
  

三、Mybatis_config.xml的简单配置




    
        
        
        
    
    
        
            
            
                
                
                
                
            
        
    
    
        
    

四、Pojo和Mapper的配置

StudentPojo

package zhd.cn.Mybatis.pojo;

public class StudentPojo {
    
    private Long id;
    private String name;
    private String sex;
    
    public Long getId() {
        return id;
    }
    public void setId(Long 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;
    }
}

StudentMapper

package zhd.cn.Mybatis.mapper;

import zhd.cn.Mybatis.pojo.StudentPojo;

public interface StudentMapper {
    public StudentPojo getStudentPojo(Long id);
    public int insertStudentPojo(StudentPojo studentPojo);
    public int deleteStudentPojo(Long id);
}

StudentMapper.xml




    
    
        insert into student values(#{id},#{name},#{sex})
    
    
        delete from student where id=#{id}
    

五、会话工厂工具类

package zhd.cn.Mybatis.utils;

import java.io.IOException;
import java.io.InputStream;
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.apache.log4j.Logger;
import org.apache.log4j.Priority;

public class SqlSessionFactoryUtils {
    
    private static SqlSessionFactory sqlSessionFactory = null;
    private static final Class CLASS_LOCK=SqlSessionFactoryUtils.class;
    
    private SqlSessionFactoryUtils() {
        
    }
    public static SqlSessionFactory initSqlSessionFactory() {
        String resource = "mybatis_config.xml";
        InputStream inputStream = null;
        try {
            inputStream=Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            Logger.getLogger(SqlSessionFactoryUtils.class.getName()).log(Priority.DEBUG, null, e);
        }
        synchronized (CLASS_LOCK) {
            if(sqlSessionFactory==null) {
                sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
            }
        }
        return sqlSessionFactory;
    }
    public static SqlSession openSqlSession() {
        if(sqlSessionFactory==null) {
             initSqlSessionFactory();
        }
        return sqlSessionFactory.openSession();
    }
}

六、测试类

package zhd.cn.Mybatis;

import org.apache.ibatis.session.SqlSession;

import zhd.cn.Mybatis.mapper.StudentMapper;
import zhd.cn.Mybatis.pojo.StudentPojo;
import zhd.cn.Mybatis.utils.SqlSessionFactoryUtils;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        SqlSession sqlSession=null;
        sqlSession=SqlSessionFactoryUtils.openSqlSession();
        StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
        StudentPojo  studentPojo =studentMapper.getStudentPojo((long) 1);
        System.out.println(studentPojo.getId()+" "+studentPojo.getName());
        studentPojo.setId((long) 2);
        studentMapper.insertStudentPojo(studentPojo);
        sqlSession.commit();       
    }
}

你可能感兴趣的:(postgresql)