MyBatis下载地址:https://github.com/mybatis/mybatis-3/releases
一、创建数据库和表
数据库名ssm,数据表student
1
2
3
4
5
6
7
8
9
10
11
12
|
mysql> create database ssm; Query OK, 1 row affected (0.01 sec) mysql> use ssm Database changed mysql> CREATE TABLE `student` ( -> `id` int (11) NOT NULL , -> ` name ` varchar (255) DEFAULT NULL , -> `email` varchar (255) DEFAULT NULL , -> `age` int (11) DEFAULT NULL , -> PRIMARY KEY (`id`) -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected, 3 warnings (0.03 sec) |
二、创建maven工程
1、pom.xml加入maven坐标
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
< dependencies > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.11</ version > < scope >test</ scope > </ dependency > < dependency > < groupId >org.mybatis</ groupId > < artifactId >mybatis</ artifactId > < version >3.5.1</ version > </ dependency > < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > < version >5.1.9</ version > </ dependency > </ dependencies > |
2、加入maven插件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
< build > < resources > < resource > < directory >src/main/java</ directory > <!--所在的目录--> < includes > <!--包括目录下的.properties,.xml 文件都会扫描到--> < include >**/*.properties</ include > < include >**/*.xml</ include > </ includes > < filtering >false</ filtering > </ resource > </ resources > < plugins > < plugin > < artifactId >maven-compiler-plugin</ artifactId > < version >3.1</ version > < configuration > < source >1.8</ source > < target >1.8</ target > </ configuration > </ plugin > </ plugins > </ build > |
三、代码编写
1、编写Student实体类
创建包 com.Example.domain, 包中创建 Student 类
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.bjpowernode.domain; /** * <p>Description: 实体类 </p> * <p>Company: http://www.bjpowernode.com */ public class Student { //属性名和列名一样 private Integer id; private String name; private String email; private Integer age; // set ,get , toString } |
2、编写DAO接口StudentDao
创建 com.Example.dao 包,创建 StudentDao 接口
1
2
3
4
5
6
7
8
9
10
11
|
package com.bjpowernode.dao; import com.bjpowernode.domain.Student; import java.util.List; /* * <p>Description: Dao 接口 </p> * <p>Company: http://www.bjpowernode.com */ public interface StudentDao { /*查询所有数据*/ List<Student> selectStudents(); } |
3、编写DAO接口Mapper映射文件StudentDao.xml。
- 在 dao 包中创建文件 StudentDao.xml
- 要 StudentDao.xml 文件名称和接口 StudentDao 一样,区分大小写的一 样。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<? 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"> <!-- namespace:必须有值,自定义的唯一字符串 推荐使用:dao 接口的全限定名称 --> < mapper namespace = "com.Example.dao.StudentDao" > <!-- <select>: 查询数据, 标签中必须是 select 语句 id: sql 语句的自定义名称,推荐使用 dao 接口中方法名称, 使用名称表示要执行的 sql 语句 resultType: 查询语句的返回结果数据类型,使用全限定类名 --> < select id = "selectStudents" resultType = "com.Example.domain.Student" > <!--要执行的 sql 语句--> select id,name,email,age from student </ select > </ mapper > |
4、创建MyBatis主配置文件
项目 src/main 下创建 resources 目录,设置 resources 目录为 resources root
创建主配置文件:名称为 mybatis.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<? 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 > <!--配置 mybatis 环境--> < environments default = "mysql" > <!--id:数据源的名称--> < environment id = "mysql" > <!--配置事务类型:使用 JDBC 事务(使用 Connection 的提交和回 滚)--> < transactionManager type = "JDBC" /> <!--数据源 dataSource:创建数据库 Connection 对象 type: POOLED 使用数据库的连接池 --> < dataSource type = "POOLED" > <!--连接数据库的四个要素--> < property name = "driver" value = "com.mysql.jdbc.Driver" /> < property name = "url" value = "jdbc:mysql://localhost:3306/ssm" /> < property name = "username" value = "root" /> < property name = "password" value = "123456" /> </ dataSource > </ environment > </ environments > < mappers > <!--告诉 mybatis 要执行的 sql 语句的位置--> < mapper resource = "com/Example/dao/StudentDao.xml" /> </ mappers > </ configuration > |
支持中文的url
jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8
四、创建测试类进行测试
1、创建测试类MyBatisTest
src/test/java/com/Example/ 创建 MyBatisTest.java 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/* * mybatis 入门 */ @Test public void testStart() throws IOException { //1.mybatis 主配置文件 String config = "mybatis-config.xml" ; //2.读取配置文件 InputStream in = Resources.getResourceAsStream(config); //3.创建 SqlSessionFactory 对象,目的是获取 SqlSession SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //4.获取 SqlSession,SqlSession 能执行 sql 语句 SqlSession session = factory.openSession(); //5.执行 SqlSession 的 selectList() List<Student> studentList = session.selectList( "com.bjpowernode.dao.StudentDao.selectStudents" ); //6.循环输出查询结果 studentList.forEach( student -> System.out.println(student)); //7.关闭 SqlSession,释放资源 session.close(); } |
List<Student> studentList =
session.selectList("com.bjpowernode.dao.StudentDao.selectStudents");
近似等价的 jdbc 代码
Connection conn = 获取连接对象
String sql=” select id,name,email,age from student”
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
2、配置日志功能
mybatis.xml 文件加入日志配置,可以在控制台输出执行的 sql 语句和参数
1
2
3
|
< settings > < setting name = "logImpl" value = "STDOUT_LOGGING" /> </ settings > |
五、增删改操作
insert操作
(1)StudentDAO接口中的方法
1
|
int insertStudent(Student student); |
(2)StudentDAO.xml加入sql语句
1
2
3
4
|
< insert id = "insertStudent" > insert into student(id,name,email,age) values(#{id},#{name},#{email},#{age}) </ insert > |
(3)增加测试方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
@Test public void testInsert() throws IOException { //1.mybatis 主配置文件 String config = "mybatis-config.xml" ; //2.读取配置文件 InputStream in = Resources.getResourceAsStream(config); //3.创建 SqlSessionFactory 对象 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //4.获取 SqlSession SqlSession session = factory.openSession(); //5.创建保存数据的对象 Student student = new Student(); student.setId( 1005 ); student.setName( "张丽" ); student.setEmail( "zhangli@163.com" ); student.setAge( 20 ); //6.执行插入 insert int rows = session.insert( "com.bjpowernode.dao.StudentDao.insertStudent" ,student); //7.提交事务 session.commit(); System.out.println( "增加记录的行数:" +rows); //8.关闭 SqlSession session.close(); } |
到此这篇关于MyBatis框架零基础快速入门案例详解的文章就介绍到这了,更多相关MyBatis 案例内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/m0_54925305/article/details/123444621