标签:strong try har vat evel app namespace ret sele
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
搭建环境
创建数据库
新建项目(普通Maven即可)
导入相关依赖
<!--Mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--MyBatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
创建MyBatis核心配置文件(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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc"/>
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="Root1234"/>
</dataSource>
</environment>
</environments>
<!-- 每一个Mapper.xml都需要在MyBatis的核心配置文件中注册 -->
<mappers>
<mapper resource="Mapper文件路径"/>
</mappers>
</configuration>
编写MyBatis工具类用于获取SqlSession
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory;
static{
// 从 XML 文件中构建 SqlSessionFactory 的实例非
String resource = "MyBatis-config.xml";
InputStream inputStream = null;
try{
inputStream = Resources.getResourceAsStream(resource);
}catch(IOException e){
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
//既然有了 SqlSessionFactory,我们可以从中获得 SqlSession 的实例。SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
编写代码
创建实体类
创建接口
public interface empMapper {
List<Employee> getEmpList();
}
创建Mapper配置文件,定义SQL语句
<?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和这个接口绑定-->
<mapper namespace="com.maple.dao.emp.empMapper">
<!--id对应接口中的方法名字,将这句sql和方法绑定resultType是返回的类型,需要指明这个类型的包路径-->
<select id="getEmpList" resultType="com.maple.pojo.Employee">
select * from employee
</select>
</mapper>
在MyBatis核心配置文件(MyBatis-config.xml)中注册Mapper.xml
<mappers>
<mapper resource="MyBatis/Mapper/EmpMapper.xml"/>
</mappers>
测试
public class empMapperTest {
@Test
public void test(){
//1. 获取SqlSession对象
SqlSession sqlSession = MyBatisUtil.getSqlSession();
//2. 执行sql
//方式1获取Mapper
EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
List<Employee> empList = empMapper.getEmpList();
//方式2
// List<Employee> empList = sqlSession.selectList("com.maple.dao.emp.EmpMapper.getEmpList");
for(Employee employee : empList){
System.out.println(employee);
}
//3. 关闭SqlSession
sqlSession.close();
}
}
初识MyBatis及第一个MyBatis程序(基于Maven创建)
标签:strong try har vat evel app namespace ret sele
原文地址:https://www.cnblogs.com/junlinsky/p/12801153.html