温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JAVA Mybatic Access MSSQL

发布时间:2020-08-01 12:19:05 来源:网络 阅读:370 作者:zjy1002261870 栏目:编程语言

导入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>

在项目根目录下创建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内容如下
<?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 数据库配置文件 -->
<properties resource="db.properties" />
<!-- 指定 MyBatis 所用日志的具体实现 -->
<settings>
<setting name="logImpl" value="LOG4J" />
</settings>

<!-- 环境配置,即连接的数据库。 -->
<environments default="mssql">
    <!-- 环境配置,即连接的数据库。 -->
    <environment id="mssql">
        <!-- 指定事务管理类型,type="JDBC"指直接简单使用了JDBC的提交和回滚设置 -->
        <transactionManager type="JDBC" />
        <!-- dataSource指数据源配置,POOLED是JDBC连接对象的数据源连接池的实现。 -->
        <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去×××持久化类的映射文件 -->
<mappers>
    <mapper resource="com/skcc/egss/dao/EmpMapper.xml" />
</mappers>
<!-- mappers告诉了MyBatis去×××持久化类的映射类(注解形式) <mappers> <mapper class="cn.mybatis.mydemo6.mapper.ArticleMapper" 
    /> </mappers> -->

</configuration>

创建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内容如下
<?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内容如下
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进行测试
@Test
br/>JUnit5进行测试
@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");
}
}
测试结果
maps.size() : 1

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI