个人撰写MyBatis开发实战【超全面】
原创个人编写 MyBatis项目实战—仅供小白学习参考
一、搭建数据库
#建数据库
CREATE DATABASE li
;
#用数据库
USE li
;
#建表
CREATE TABLE student
(
id
INT(10) NOT NULL,
name
VARCHAR(30) DEFAULT NULL,
tid
INT(10)DEFAULT NULL,
grade
INT(10) DEFAULT NULL,
sex
VARCHAR(30)DEFAULT NULL,teacher
PRIMARY KEY(id
)
)ENGINE=INNODB DEFAULT CHARSET=utf8
二、创建项目
2.1 创建父项目
1、创建一个 maven项目
填写相关信息 作为 【父工程】
2、选择 maven环境本地仓库和地址
3、删掉 该 【父工程】
的 src
文件夹
2.2 创建子模块
查看父子项目模块
2.3 编写配置文件
1、编写 pom.xml
在【父工程】的 pom.xml
文件中导入依赖
mysql
mysql-connector-java
8.0.26
org.mybatis
mybatis
3.5.9
junit
junit
4.12
在【父工程】的 pom.xml
文件中设置resources,防止资源导出失败
src/main/resources
**/*.properties
**/*.xml
true
src/main/java
**/*.properties
**/*.xml
true
2、【子模块】 resources
文件下 创建 mybatis-config.xml
添加下面内容
3、【子模块】 resources
文件下 创建 db.properties
添加下面内容
#连接数据库信息 根据自己情况改写
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username=root
password=123456
三、编写代码
3.1 实体类
pojo层 编写实体类
Student.java
其中 get/set/有参/无参方法 的编写
方法一:
-
如下图所示,传统的编写get/set/有参/无参方法
package com.li.pojo;
public class Student { private int id; private String name; private int tid; private String sex; private int grade;
public Student(int id, String name, int tid, String sex, int grade) { this.id = id; this.name = name; this.tid = tid; this.sex = sex; this.grade = grade; } public Student() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getTid() { return tid; } public void setTid(int tid) { this.tid = tid; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; }
}
方法二:
- 使用 lombok插件 使用 注解方式 来直接插入
- lombok的安装使用可以参考:
https://blog.csdn.net/junR_980218/article/details/124059764
3.2 接口
dao层 编写接口
StudentMapper.java
package com.li.dao;
import com.li.pojo.Student;
import java.util.List;
import java.util.Map;
public interface StudentMapper {
//查询全部学生的信息
List queryStudent ();
//根据id查询学生
Student queryStudentId(int i);
//动态查询语句
List queryStudentDong(Map map);
//添加学生信息
int addStudent(Student student);
//修改学生信息
int updateStudent(Student student);
//删除学生信息
int deleteStudentId(int id);
}
3.3 接口的实现—配置文件
最好跟接口在一个包下 即直接写 dao层
StudentMapper.xml
insert into li.student(id,name,tid,grade,sex)
values(#{id},#{name},#{tid},#{grade},#{sex})
update li.student
set
name =#{name},
tid=#{tid},
grade=#{grade},
sex=#{sex}
where id=#{id};
delete
from li.student
where id=#{id};
3.4 工具类
utils层 编写工具类
MybatisUtils.java
package com.li.utils;
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 java.io.IOException;
import java.io.InputStream;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//使用mybatis的第一步:获取sqlSessionFactory的对象
String resource ="mybatis-configg.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}catch (IOException e){
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
//你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
3.5 测试类
test层 编写测试类
StudentTest.java
package com.li.dao;
import com.li.pojo.Student;
import com.li.utils.MybatisUtils;
import jdk.internal.org.objectweb.asm.Handle;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class StudentTest {
@Test //查询全部学生信息
public void queryStudent(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List students = mapper.queryStudent();
for (Student student : students) {
System.out.println(student);
}
sqlSession.close();
}
@Test //通过id查询学生信息
public void queryStudentId(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.queryStudentId(1);
System.out.println(student);
sqlSession.close();
}
@Test
public void queryStudentDong(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
HashMap
注意:
测试的过程中,增删改必须提交事务才能提交到数据库,否则数据库中的数据是没有变化的,查询不需要提交事务!
四、源码地址
源码地址:
链接:https://pan.baidu.com/s/1gQQNKd03KFjNWEw6uF-CIQ
提取码:9802
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除