您好,登錄后才能下訂單哦!
導入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內容如下
log4j.rootLogger=DEBUG, stdout,dailyRollingFile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。