Mybatis -- Mybatis快速入门映射文件概述Myabtis增删改查操作
Posted CodeJiao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis -- Mybatis快速入门映射文件概述Myabtis增删改查操作相关的知识,希望对你有一定的参考价值。
1. Mybatis快速入门
快速入门步骤:
准备工作 创建一个maven工程:
1.1 添加Mybatis坐标(导入相关的依赖)
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>smm_heima</artifactId>
<groupId>com.tian</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mybatis_01</artifactId>
<dependencies>
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!-- mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- 单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<!-- 日志打印-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
1.2 创建User数据表(搭建数据库环境)
执行下列sql语句
CREATE DATABASE `test`;
USE `test`;
CREATE TABLE `user` (
`id` INT NOT NULL PRIMARY KEY,
`username` VARCHAR ( 30 ) DEFAULT NULL,
`password` VARCHAR ( 30 ) DEFAULT NULL
) ENGINE = INNODB DEFAULT CHARSET = utf8;
INSERT INTO USER (`id`, `username`, `password`) VALUES
(1,'thylove', '123456'),
(2, 'root', '123456'),
(3, 'admin', '123456');
执行结果:
1.3 编写User实体类
User.java
package com.tian.pojo;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\\'' +
", password='" + password + '\\'' +
'}';
}
}
1.4 编写映射文件UserMapper.xml
UserMapper.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 namespace="userMapper">
<!--查询操作-->
<select id="findAll" resultType="com.tian.pojo.User">
select *
from user
</select>
</mapper>
1.5 编写核心文件SqIMapConfig.xml
准备工作:编写数据库配置文件:
jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=317525
正式开始:
SqIMapConfig.xml
<?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>
<!--通过properties标签加载外部properties文件-->
<properties resource="jdbc.properties"/>
<!--数据源环境-->
<environments default="developement">
<environment id="developement">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--加载映射文件-->
<mappers>
<mapper resource="com.tian.mapper/UserMapper.xml"/>
</mappers>
</configuration>
1.6 编写测试类
MyBatisTest.java
package com.tian.test;
import com.tian.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;
public class MyBatisTest {
@Test
//查询操作
public void test1() throws IOException {
//获得核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("SqIMapConfig.xml");
//获得session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得session回话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行操作 参数:namespace+id
List<User> userList = sqlSession.selectList("userMapper.findAll");
//打印数据
System.out.println(userList);
//释放资源
sqlSession.close();
}
}
运行结果:
2. 映射文件概述
3. Myabtis增删改查操作
3.1 查询操作
查询操作上面已经演示过了,这里就不演示了。
3.2 插入操作
UserMapper.xml
<!--插入操作 values后面对应的User的属性名-->
<insert id="save" parameterType="com.tian.pojo.User">
insert into user
values (#{id}, #{username}, #{password})
</insert>
测试代码:
MyBatisTest.java
@Test
//插入操作:除了读以外, 其余的操作都需要提交事务
public void test2() throws IOException {
//模拟user对象
User user = new User();
user.setId(1000);
user.setUsername("CodeJiao");
user.setPassword("root");
//获得核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("SqIMapConfig.xml");
//获得session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得session回话对象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//执行操作 参数:namespace+id
sqlSession.insert("userMapper.save", user);
//mybatis执行更新操作 提交事务
sqlSession.commit();
//释放资源
sqlSession.close();
}
执行结果:
3.3 插入操作需要注意的问题
3.4 修改操作
UserMapper.xml
<!--修改操作-->
<update id="update" parameterType="com.tian.pojo.User">
update user
set username=#{username},
password=#{password}
where id = #{id}
</update>
测试代码:
MyBatisTest.java
@Test
//修改操作
public void test3() throws IOException {
//模拟user对象
User user = new User();
user.setId(1000);
user.setUsername("IronMan");
user.setPassword("mypwd");
//获得核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("SqIMapConfig.xml");
//获得session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得session回话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行操作 参数:namespace+id
sqlSession.update("userMapper.update", user);
//mybatis执行更新操作 提交事务
sqlSession.commit();
//释放资源
sqlSession.close();
}
运行结果:
3.5 删除操作
UserMapper.xml
<!--删除操作-->
<delete id="delete" parameterType="int">
delete
from user
where id = #{id}
</delete>
测试代码:
MyBatisTest.java
@Test
//删除操作
public void test4() throws IOException {
//获得核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("SqIMapConfig.xml");
//获得session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得session回话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行操作 参数:namespace+id
sqlSession.delete("userMapper.delete", 1000);
//mybatis执行更新操作 提交事务
sqlSession.commit();
//释放资源
sqlSession.close();
}
运行结果:
3.6 小结
以上是关于Mybatis -- Mybatis快速入门映射文件概述Myabtis增删改查操作的主要内容,如果未能解决你的问题,请参考以下文章