189 8069 5689

JAVAMybaticAccessMSSQL

导入jar依赖


org.mybatis
mybatis
3.5.0



log4j
log4j
1.2.17



com.microsoft.sqlserver
mssql-jdbc
7.0.0.jre8

成都创新互联公司专注于土默特右旗网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供土默特右旗营销型网站建设,土默特右旗网站制作、土默特右旗网页设计、土默特右旗网站官网定制、成都微信小程序服务,打造土默特右旗网络公司原创品牌,更为您提供土默特右旗网站排名全网营销落地服务。

    

在项目根目录下创建Source Folder (resources)文件夹
在resources目录下创建db.properties log4j.properties mybatis-config.xml
db.properties内容如下
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://172.26.205.1:1433;DatabaseName=EGSS_TEST
username=sa
password=1111111

log4j.properties内容如下

Global logging configuration

log4j.rootLogger=DEBUG, stdout,dailyRollingFile

MyBatis logging configuration...

log4j.logger.com.skcc.egss.dao.*=DEBUG

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

File

log4j.appender.dailyRollingFile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyRollingFile.File = F:\javalog\logredis.log
log4j.appender.dailyRollingFile.Append = true
log4j.appender.dailyRollingFile.Threshold = DEBUG
log4j.appender.dailyRollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.dailyRollingFile.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %c.%M:%L %m%n

mybatis-config.xml内容如下

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">









    
    
        
        
        
        
            
            
            
            
        
    




    

创建com.skcc.egss.dao包,并在该包下创建SqlSessionFactoryUtil.java EmpMapper.xml EmpDao.java
SqlSessionFactoryUtil.java内容如下
public class SqlSessionFactoryUtil {

public static SqlSessionFactory getSqlSessionFactory() throws IOException {

    SqlSessionFactory sqlSessionFactory = null;
    try {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
         sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return sqlSessionFactory;
}

}
EmpMapper.xml内容如下

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">



EmpDao.java内容如下
public class EmpDao {

public Logger logger = Logger.getLogger(EmpDao.class);

public List> selectEmpList(Map map){
    logger.info("selectEmpList");
    try (SqlSession session=SqlSessionFactoryUtil.getSqlSessionFactory().openSession()){
        return session.selectList("egss.empinfo.selectEmpList", map);

    } catch (Exception e) {
        // TODO: handle exception
        System.out.println(e);
    }
    return null;

}

}JUnit5进行测试
@Test
br/>JUnit5进行测试
@Test
List> maps = new EmpDao().selectEmpList(null);
if(maps != null ) {
System.out.println("maps.size() : " + maps.size());
System.out.println(maps);
}else {
System.err.println("no data");
}
}
测试结果
maps.size() : 1


分享标题:JAVAMybaticAccessMSSQL
网页路径:http://cdxtjz.cn/article/iejcod.html

其他资讯