搜索
首页
笔记
案例
关于
课程列表
Mybatis
入门
配置文件
作用域与生命周期
整合log4j2日志系统
映射文件
工具类及使用DAO层
动态代理及获取主键自增ID
参数绑定
封装结果集
高级映射结果
动态SQL
逆向工程
扩展
分页插件
批处理插件
课程导航
计算机基础知识
C
Linux
linux常用软件
计算机网络
程序员修养
设计模式
工具
Git
composer
vim
IntelliJ IDEA
wireshark
laravel
Spring
SpringMVC
Maven
数据库
MySQL
Redis
MongoDB
JDBC
Mybatis
MyBatis-Plus
服务端编程
PHP
Java
shell script
JavaWeb
HTML / CSS
HTML
CSS
HTML5
CSS3
BOOTSTRAP
JavaScript
JavaScript
JQuery
layui
配置文件
入门
第一次学习mybatis,根据步骤完成CURD操作。 ## 使用Idea创建一个Module ![](https://www.1024phper.com/wp-content/uploads/2021/03/QQ截图20210330192128.jpg) ![](https://www.1024phper.com/wp-content/uploads/2021/03/QQ截图20210330192208.jpg) ![](https://www.1024phper.com/wp-content/uploads/2021/03/QQ截图20210330192229.jpg) ![](https://www.1024phper.com/wp-content/uploads/2021/03/QQ截图20210330192240.jpg) ![](https://www.1024phper.com/wp-content/uploads/2021/03/QQ截图20210330192317.jpg) ## 配置pom.xml文件 ### 加入依赖项 加入mysql的驱动以及mybatis依赖 ```
org.mybatis
mybatis
3.5.1
mysql
mysql-connector-java
5.1.9
``` ### 加入resources配置项 加入该配置项的意义是使编译器,main/java目录下的配置文件会被拷贝到编译目录中(默认main/java目录下的非.java文件不会被拷贝) ```xml
src/main/java
**/*.properties
**/*.xml
false
``` pom.xml的内容如下: ```xml
4.0.0
com.learnmybatis
day2
1.0-SNAPSHOT
UTF-8
1.8
1.8
junit
junit
4.11
test
org.mybatis
mybatis
3.5.1
mysql
mysql-connector-java
5.1.9
src/main/java
**/*.properties
**/*.xml
false
``` ## 准备数据表及编写pojo类 User表结构如下: ```java id int(10) NO PRI auto_increment name varchar(64) NO email varchar(255) NO age int(3) NO 0 ``` 在java/com/learnmybatis/目录下新建包pojo,然后编写User.java ## 创建Mybatis的主配置文件 在resources目录下创建mybatis.xml文件 ```xml
``` ## 创建Mapper映射文件user.xml 在main/java/com/learnmybits/dao/目录下创建user.xml ```xml
select * from `user`
select * from `user` WHERE id=#{id}
UPDATE `user` SET `name` = #{name},`email` = #{email}, `age` = #{age} WHERE id=#{id}
INSERT `user`(`name`, `email`,`age`) VALUE(#{name}, #{email}, #{age})
DELETE FROM `user` WHERE id=#{id}
``` ## 创建测试文件测试CURD ```java package com.learnmybatis; import com.learnmybatis.pojo.User; 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 monkeykingGWX * email 704835519@qq.com * created 2021-03-30 20:13 */ public class TestMybatis { @Test public void testSelect () throws IOException { String resource = "mybatis.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // mybatis默认不是自动提交事务的,这里设置为true,相当于设置autoCommit(true) try (SqlSession session = sqlSessionFactory.openSession(true)) { String sqlId = "com.learnmybatis.dao.user.select"; List
users = session.selectList(sqlId); users.forEach(System.out::println); } } @Test public void testSelectById () throws IOException { String resource = "mybatis.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); try (SqlSession session = sqlSessionFactory.openSession(true)) { String sqlId = "com.learnmybatis.dao.user.selectById"; User user = session.selectOne(sqlId, 1); System.out.println(user); } } @Test public void testInsert () throws IOException { String resource = "mybatis.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); try (SqlSession session = sqlSessionFactory.openSession(true)) { String sqlId = "com.learnmybatis.dao.user.insert"; int n = session.insert(sqlId, new User(null, "周杰伦", "JayZhou@163.com", 40)); System.out.println(n); } } @Test public void testUpdate () throws IOException { String resource = "mybatis.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); try (SqlSession session = sqlSessionFactory.openSession(true)) { String sqlId = "com.learnmybatis.dao.user.updateById"; int n = session.update(sqlId, new User(2, "周杰伦&Jay", "JayZhou@qq.com", 36)); System.out.println(n); } } @Test public void testDelete () throws IOException { String resource = "mybatis.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); try (SqlSession session = sqlSessionFactory.openSession(true)) { String sqlId = "com.learnmybatis.dao.user.delete"; int n = session.delete(sqlId, 2); System.out.println(n); } } } ```
配置文件
文章目录