溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JAVA Mybatic Access MSSQL

發(fā)布時(shí)間:2020-08-01 12:19:05 來(lái)源:網(wǎng)絡(luò) 閱讀:367 作者:zjy1002261870 欄目:編程語(yǔ)言

導(dǎo)入jar依賴
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.0.0.jre8</version>

    </dependency>

在項(xiàng)目根目錄下創(chuàng)建Source Folder (resources)文件夾
在resources目錄下創(chuàng)建db.properties log4j.properties mybatis-config.xml
db.properties內(nèi)容如下
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://172.26.205.1:1433;DatabaseName=EGSS_TEST
username=sa
password=1111111

log4j.properties內(nèi)容如下

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內(nèi)容如下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
<!-- 指定 MyBatis 數(shù)據(jù)庫(kù)配置文件 -->
<properties resource="db.properties" />
<!-- 指定 MyBatis 所用日志的具體實(shí)現(xiàn) -->
<settings>
<setting name="logImpl" value="LOG4J" />
</settings>

<!-- 環(huán)境配置,即連接的數(shù)據(jù)庫(kù)。 -->
<environments default="mssql">
    <!-- 環(huán)境配置,即連接的數(shù)據(jù)庫(kù)。 -->
    <environment id="mssql">
        <!-- 指定事務(wù)管理類(lèi)型,type="JDBC"指直接簡(jiǎn)單使用了JDBC的提交和回滾設(shè)置 -->
        <transactionManager type="JDBC" />
        <!-- dataSource指數(shù)據(jù)源配置,POOLED是JDBC連接對(duì)象的數(shù)據(jù)源連接池的實(shí)現(xiàn)。 -->
        <dataSource type="POOLED">
            <property name="driver" value="${driver}" />
            <property name="url" value="${url}" />
            <property name="username" value="${username}" />
            <property name="password" value="${password}" />
        </dataSource>
    </environment>
</environments>

<!-- mappers告訴了MyBatis去×××持久化類(lèi)的映射文件 -->
<mappers>
    <mapper resource="com/skcc/egss/dao/EmpMapper.xml" />
</mappers>
<!-- mappers告訴了MyBatis去×××持久化類(lèi)的映射類(lèi)(注解形式) <mappers> <mapper class="cn.mybatis.mydemo6.mapper.ArticleMapper" 
    /> </mappers> -->

</configuration>

創(chuàng)建com.skcc.egss.dao包,并在該包下創(chuàng)建SqlSessionFactoryUtil.java EmpMapper.xml EmpDao.java
SqlSessionFactoryUtil.java內(nèi)容如下
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內(nèi)容如下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="egss.empinfo">
<select id="selectEmpList" parameterType="java.util.Map" resultType="java.util.Map">
select top 5 *
from BASEUSERINFO
</select>
</mapper>
EmpDao.java內(nèi)容如下
public class EmpDao {

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

public List<Map<String, Object>> selectEmpList(Map<String, Object> 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進(jìn)行測(cè)試
@Test
br/>JUnit5進(jìn)行測(cè)試
@Test
List<Map<String, Object>> 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");
}
}
測(cè)試結(jié)果
maps.size() : 1

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI