使用Idea和maven搭建Mybatis框架以及基础测试
程序员文章站
2022-10-03 16:15:31
创建maven工程目录结构导入相关的jar导入mybatis和mysql驱动相关的jar mysql my...
创建maven工程
目录结构
导入相关的jar
导入mybatis和mysql驱动相关的jar
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
编写mybatis的全局配置文件
<?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>
<settings>
<!--控制台输出SQL语句-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/db5?characterEncoding=UTF8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"/>
<property name="username" value="root" />
<!--密码换成自己的-->
<property name="password" value="******" />
</dataSource>
</environment>
</environments>
<!--通过config.xml文件与对应的文件绑定-->
<mappers>
<mapper resource="com/xzy/mapper/EmpMapper.xml"/>
</mappers>
</configuration>
编写实体类
package com.xzy.bean;
public class EmpInfo {
private Integer empNo;
private String ename;
private String job;
private Integer mgr;
private String hireDate;
private Double sal;
private Double comm;
private Integer deptNo;
public EmpInfo() {
}
public Integer getEmpNo() {
return empNo;
}
public void setEmpNo(Integer empNo) {
this.empNo = empNo;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Integer getMgr() {
return mgr;
}
public void setMgr(Integer mgr) {
this.mgr = mgr;
}
public String getHireDate() {
return hireDate;
}
public void setHireDate(String hireDate) {
this.hireDate = hireDate;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public Double getComm() {
return comm;
}
public void setComm(Double comm) {
this.comm = comm;
}
public Integer getDeptNo() {
return deptNo;
}
public void setDeptNo(Integer deptNo) {
this.deptNo = deptNo;
}
@Override
public String toString() {
return "EmpInfo{" +
"empNo=" + empNo +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", mgr=" + mgr +
", hireDate='" + hireDate + '\'' +
", sal=" + sal +
", comm=" + comm +
", deptNo=" + deptNo +
'}';
}
}
编写接口
package com.xzy.mapper;
import com.xzy.bean.EmpInfo;
import java.util.List;
public interface EmpMapper {
List<EmpInfo> findAllEmp();
}
编写接口对应的mapper文件
<?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">
<!--将接口与XML文件绑定-->
<mapper namespace="com.xzy.mapper.EmpMapper">
<select id="findAllEmp" resultType="java.lang.String">
select * from emp
</select>
</mapper>
测试
package org.xzy;
import com.xzy.bean.EmpInfo;
import com.xzy.mapper.EmpMapper;
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.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class AppTest {
@Test
public void test01() throws IOException {
InputStream is = Resources.getResourceAsStream("config.xml");
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
SqlSession session = ssf.openSession();
EmpMapper em = session.getMapper(EmpMapper.class);
List<EmpInfo> allEmp = em.findAllEmp();
System.out.println(allEmp);
session.close();
}
}
填坑
找不到对应的mapper文件的解决方法:
在pom.xml文件中添加如下扫描相关配置
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
本文地址:https://blog.csdn.net/weixin_42643321/article/details/107689597
上一篇: Java 反射机制的学习-02
下一篇: 荐 设计模式之单例模式(5种实现方式)