Mybatis-helloworld
mybatis框架计划使用10篇文章讲解完,尽量将mybatis所有的点都概括到,本文作为mybatis框架系列的第一讲,主要是进行一个开篇的练手环节,搭建,调通mybatis框架。学习框架之前,你应该具有一个mysql的连接环境,了解maven的基本使用技能。
十一讲的内容为:
1.开篇框架搭建入门
2.mybatis全局配置文件config
3.mybatis映射文件mapper,
4.Mybatis动态SQL(DynamicSQL)
5.Mybatis缓存机制
6.Mybatis与Spring,SpringMvc整合
7.Mybatis逆向工程(mbg)
8.Mybatis源码分析
9.Mybatis分页插件(PageHelper)
10.Mybatis其他一些零碎点
如果遇到什么问题,欢迎在我品论区留言,我看到会第一时间给大家回复
1.Mybatis简介
MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
2.搭建Mybatis-helloworld
引入maven依赖,因为我本机装的mysql使用的是8.x版本,所以mysql-connector-java使用的版本为8.x
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
创建resources文件夹,并染色(使用idea工具的同学,应该知道,目的是将resource文件夹变成资源文件)
在resources中创建log4j2.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
创建测试表,当然你可以使用navicat创建,只要你能创建出这张表就可以
-- 创建库
CREATE DATABASE ssm;
-- 使用库
USE ssm;
-- 创建表
CREATE TABLE employee(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(50),
email VARCHAR(50),
gender CHAR(1)
);
-- 插入数据
INSERT INTO employee VALUES(1,'helloworld','153@163.com','男');
创建Employee类
package com.together.www.bean;
public class Employee {
private Integer id;
private String lastName;
private String email;
private char gender;
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", gender=" + gender +
'}';
}
public Employee() {
}
public Employee(Integer id, String lastName, String email, char gender) {
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = 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 char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
}
创建全局配置文件mybatis-config.xml,在resources文件夹中,注意修改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.cj.jdbc.Driver" />
<!--使用mysql server 超过6版本的同学 加上 serverTimezone=UTC,否则会报告关于时区错误的问题-->
<property name="url" value="jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC" />
<property name="username" value="root" />
<property name="password" value="1234" />
</dataSource>
</environment>
</environments>
<!-- 引入SQL映射文件,Mapper映射文件 class 映射mapper接口的文件 -->
<mappers>
<mapper class="com.together.www.mapper.EmployeeMapper" />
</mappers>
</configuration>
创建Employee.java接口文件
package com.together.www.mapper;
import com.together.www.bean.Employee;
import org.apache.ibatis.annotations.Select;
public interface EmployeeMapper {
@Select("select * from employee where id = #{id}")
Employee selectOneById(Integer id);
}
创建测试文件EmployeeTest.java
package com.together.www.test;
import com.together.www.bean.Employee;
import com.together.www.mapper.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.Test;
import java.io.InputStream;
public class EmployeeTest {
@Test
public void test1() throws Exception{
//1.加载配置文件
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
//2.创建sqlsessionFactory工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//3.创建session会话
SqlSession session = factory.openSession();
//4.获取mapper接口的代理实现类对象
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
//5.执行sql语句
Employee employee = mapper.selectOneById(1);
System.out.println(employee);
}
}
本文地址:https://blog.csdn.net/weixin_43249121/article/details/109565166
上一篇: 数组\字符串\排序\栈队列
下一篇: pandas 读取excel文件