【Mybatis】快速入门

1.1原始JDBC操作数据

截图.png

1.2原始JDBC操作数据的分析

截图.png

1.3Mybatis介绍

截图.png

2.1mybatis开发步骤

截图.png

MyBatis官网地址:http://www.mybatis.org/mybatis-3/

2.2开发步骤

2.2.1导入mybatis坐标和其他坐标

<?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-in[stance"](http://maven.apache.org/POM/4.0.0)

​     xsi:schemaLocation="http://maven.apache.org/PO[M/4.0.0 http://maven.apache.org/x](http://maven.apache.org/POM/4.0.0)sd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

 

  <groupId>org.example</groupId>

  <artifactId>mybatis-quick</artifactId>

  <version>1.0-SNAPSHOT</version>

 

  <properties><maven.compiler.source>8</maven.compiler.source[>](http://maven.apache.org/POM/4.0.0)

​    <maven.compiler.target>8</maven.compiler.target[>](http://maven.apache.org/POM/4.0.0)

  </properties>

 

  <dependencies><!--mybatis坐标--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.5</version></dependency><!--mysql驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactI[d>](http://maven.apache.org/POM/4.0.0)

​      <version>5.1.46</version><scope>runtime</scope></dependency><!--单元测试--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13</version><scope>test</scope></dependency><!--日志坐标--><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency>

  </dependencies>

</project>

2.2.2创建User表,编写User实体

package top.plutos.pojo;


public class User {

 private int id;

 private String username;

 private String password;


 @Override

 public String toString() {return "User{" +"id=" + id +", username='" + username + '\'' +", password='" + 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;

 }

}

2.2.3编写UserMapper.xml配置文件

截图.png

<?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">](http://mybatis.org/dtd/mybatis-3-mapper.dtd)

<mapper namespace="userMapper">

  <select id="findAll" resultType="top.plutos.pojo.User" >

​    select *from user;

  </select>

</mapper>

2.2.4编写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">](http://mybatis.org/dtd/mybatis-3-config.dtd)



<configuration>

 <!-- 数据环境 -->

 <environments default="development"><environment id="development"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/test"/><property name="username" value="root"/><property name="password" value="sjh123"/></dataSource></environment>

 </environments>

 <!--加载映射文件-->

 <mappers><mapper resource="top/plutos/mapper/UserMapper.xml"></mapper>

 </mappers>

</configuration>

2.2.5 编写测试代码

package top.plutos.test;

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 top.plutos.pojo.User;

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("sqlMapConfig.xml");//获得SQLSession工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);//获得sqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession();//执行SQL语句List<User> userList = sqlSession.selectList("userMapper.findAll");//打印结果System.out.println(userList);//释放资源

​    sqlSession.close();

 }

}

3.mybatis映射文件概述

截图.png

4.增删改查操作

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">](http://mybatis.org/dtd/mybatis-3-mapper.dtd)

<mapper namespace="userMapper">

  <!--删除操作-->

  <delete id="deleteById" parameterType="java.lang.Integer">

​    delete from user where id=#{id};

  </delete>

  <!--修改操作-->

  <update id="updateById" parameterType="top.plutos.pojo.User">

​    update user

​      set username=#{username},

​      password=#{password} where id=#{id};

  </update>

  <!--查询操作-->

  <select id="findAll" resultType="top.plutos.pojo.User">

​    select *

​    from user;

  </select>

  <!--插入操作-->

  <insert id="insert" parameterType="top.plutos.pojo.User">

​    insert into user

​    values (#{id}, #{username}, #{password})

  </insert>

</mapper>

测试文件

package top.plutos.test;



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 top.plutos.pojo.User;



import java.io.IOException;

import java.io.InputStream;

import java.util.List;



public class MyBatisTest {

 @Test

 public void test4() throws IOException {//模拟修改数据int id = 5;//加载核心配置文件InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");//获得SQLSession工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);//获得sqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession();//执行sql语句int delete = sqlSession.delete("userMapper.deleteById",id);//提交事务

​    sqlSession.commit();//打印结果System.out.println(delete);//释放资源

​    sqlSession.close();

 }

 @Test

 public void test3() throws IOException {//模拟修改数据User user = new User();

​    user.setId(5);

​    user.setUsername("luxi");

​    user.setPassword("sjh123");//加载核心配置文件InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");//获得SQLSession工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);//获得sqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession();//执行sql语句int update = sqlSession.update("userMapper.updateById",user);//提交事务

​    sqlSession.commit();//打印结果System.out.println(update);//释放资源

​    sqlSession.close();

 }

 @Test

 public void test1() throws IOException {//加载核心配置文件InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");//获得SQLSession工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);//获得sqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession();//执行SQL语句List<User> userList = sqlSession.selectList("userMapper.findAll");//打印结果System.out.println(userList);//释放资源

​    sqlSession.close();

 }

 @Test

 public void test2() throws IOException {//模拟插入数据User user = new User();

​    user.setUsername("tom");

​    user.setPassword("sjh123");//加载核心配置文件InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");//获得SQLSession工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);//获得sqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession();//执行sql语句int insert = sqlSession.insert("userMapper.insert",user);//提交事务

​    sqlSession.commit();//打印结果System.out.println(insert);//释放资源

​    sqlSession.close();

 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值