Mybatis开篇
mybatis开篇demo架构如图所示创建maven项目,引入mybatis、mysql、log4j依赖包
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
package com.atguigu.mybatis.bean;
/**
* @author ItMkYuan
*/
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", lastName='" + lastName + ''' +
", email='" + email + ''' +
", gender='" + gender + ''' +
'}';
}
}
DAO层创建mapper接口及方法
package com.atguigu.mybatis.dao;
import com.atguigu.mybatis.bean.Employee;
/**
* @author ItMkYuan
*/
public interface EmployeeMapper {
public Employee getEmpById(Integer id);
}
sql映射文件
1.namespace:名称空间,指定为接口的全类名
2.id:唯一标识
3.resultType:返回值类型
4.#{id}:从传过来的参数中取出id值
全局配置文件
1.配置;连接数据库的数据源 2.将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 namespace="com.atguigu.mybatis.dao.EmployeeMapper">
<!--namespace:名称空间,指定为接口的全类名
id:唯一标识
resultType:返回值类型
#{id}:从传过来的参数中取出id值
-->
<select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee">
select id,last_name lastName,gender,email from tbl_employee where id = #{id}
</select>
</mapper>
<?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.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--将写好的sql映射文件注册到全局配置文件-->
<mapper resource="conf\EmployeeMapper.xml"/>
</mappers>
</configuration>
测试类
1.根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象有数据源一些运行环境信息
2.sql映射文件:配置每一个sql,以及sql的封装规则等
3.将sql映射文件注册在全局配置文件中
4.写代码
1).根据全局配置文件得到SqlSessionFactory
2).使用SqlSession工厂,获取到SqlSession对象用它来执行增删改查一个sqlSession就是代表和数据库的一次会话,用完关闭
3).使用sql的唯一标志,告诉Mybatis执行哪个sql,sql就是保存在sql映射文件中的
package com.atguigu.mybatis.test;
import com.atguigu.mybatis.bean.Employee;
import com.atguigu.mybatis.dao.EmployeeMapper;
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.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
/**
* @author ItMkYuan
*
*/
public class MybatisTest {
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "conf\mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
/**
* 1.根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象
* @throws IOException
*/
@Test
public void test() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//2.获取SqlSession实例,能直接执行已经映射的sql语句
SqlSession openSession = sqlSessionFactory.openSession();
try {
Employee employee = openSession.selectOne("com.atguigu.mybatis.EmployeeMapper.selectEmp", 1);
System.out.println(employee);
} finally {
openSession.close();
}
}
@Test
public void test01() throws IOException {
//1.获取sqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//2.获取sqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.获取接口的实现类对象
//会为接口创建一个代理对象,代理对象去执行增删改查方法
try {
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
Employee employee = mapper.getEmpById(1);
System.out.println(employee);
} finally {
sqlSession.close();
}
}
}
视频地址:
https://www.bilibili.com/video/BV1mW411M737?p=3
原文始发于微信公众号(itmkyuan):Mybatis入门(尚硅谷篇)
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/39519.html