添加框架的步骤
在idea中添加数据库的可视化
这里需要注意:很多小伙伴链接不成功,这个时候要修改一下自己的驱动版本,尽量与数据库版本一致
添加jdbc.properties属性文件(数据库配置)
jdbc.driverclassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=*******
这里有个小细节,新版数据库驱动的类型不要写错 com.mysql.cj.jdbc.Driver
设置完这个就可以连接到数据库了。
添加SqlMapCongig.xml
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> <!-- 读取属性文件(jdbc.properties) 属性: resources:从resouces目录下找指定名称的文件加载 url:使用绝对路径加载属性文件 --> <properties resource="jdbc.properties"></properties> <!-- 配置数据库的环境变量(数据库链接配置) default:使用下面的environment标签下的id属性进行指定配置 --> <!-- <settings>--> <!-- <setting name="" value=""/>--> <!-- </settings>--> <environments default="development"> <!-- 开发时再公司使用的数据库配置 id;就是提供给environment的default属性使用 --> <environment id="development"> <!-- 配置事务管理器 type:指定事务管理的方式 JDBC:事务的控制交给管理员来处理 MANAGED:由容器(Spring)来管理事务 --> <transactionManager type="JDBC"></transactionManager> <!-- 配置数据源: type:指定不同的配置方式 JNDI:java命名目录接口,在服务器端进行数据库连接池的管理 POOLED:使用数据库连接池 UNPOOLED:不使用数据库连接池 --> <dataSource type="POOLED"> <!-- 配置数据库连接的基本参数 private String driver; private String url; private String username; private String password; --> <property name="driver" value="${jdbc.driverclassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> <!--在家时候数据库配置--> <!-- <environment id="home">--> <!-- <transactionManager type=""></transactionManager>--> <!-- <dataSource type=""></dataSource>--> <!-- </environment>--> <!--<!–上线的数据库配置–>--> <!-- <environment id="online">--> <!-- <transactionManager type=""></transactionManager>--> <!-- <dataSource type=""></dataSource>--> <!-- </environment>--> </environments> <!-- 注册mappe.xml文件 resouces:从resouces目录下找指定名称的文件注册 url:使用绝对路径注册 class:动态代理方式下的注册 --> <mappers> <mapper resource="StudentMapper.xml"></mapper> </mappers> </configuration>
创建实体类Student用来封装数据
package com.longlong.pojo; import java.util.Objects; /** * @Author DELL longlong * @Date 2022/7/1 14:51 * @Version 1.0 * @Function 实体类 */ public class Student { private Integer id; private String name; private String email; private Integer age; public Student() { } public Student(String name, String email, Integer age) { this.name = name; this.email = email; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return Objects.equals(id, student.id) && Objects.equals(name, student.name) && Objects.equals(email, student.email) && Objects.equals(age, student.age); } @Override public int hashCode() { return Objects.hash(id, name, email, age); } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", email='" + email + '\'' + ", age=" + age + '}'; } }
添加增删改查
添加完成学生表的增删改查的功能的StudentMapper.xml文件
<?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:是整个文件的打标签,用来区分开始和结束xml文件 属性: namespace:指定命名空间(相当于包名),用来区分不同mapper.xml 文件中的相同id --> <mapper namespace="ll"> <!-- 完成查询全部学生的功能 List<Student> getAll(); resultType:指定查询返回的结果集的类型,如果是集合,则必须是泛型 parameterType:如果有参数,则通过他来完成指定参数的类型 --> <select id="getAll" resultType="com.longlong.pojo.Student" > select * from student </select> </mapper>
这里要说明一下,为了简单明了,暂时只实现了查询全部的功能
创建测试类进行功能测试
最激动人心的时刻,要进行测试了
package com.longlong.test; import com.longlong.pojo.Student; 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; /** * @Author DELL longlong * @Date 2022/7/3 19:32 * @Version 1.0 * @Function */ public class Mytest { @Test public void testGetAll() throws IOException { InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); SqlSession sqlSession = factory.openSession(); List<Student> list = sqlSession.selectList("ll.getAll"); // list.forEach(student -> System.out.println(student)); for (Student student : list){ System.out.println(student); } System.out.println("OK"); sqlSession.close(); } }
运行结果
The above is the detailed content of How to apply Mybatis framework in Java?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment