Mybatis基础使用

1、配置文件,映射文件

  • mvn依赖配置
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.10</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.7</version>
        </dependency>
  • 配置文件
<?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>
    
    <!--配置驼峰映射-->
    <!--springboot 配置 :mybatis.configuration.map-underscore-to-camel-case=true -->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    
    <!--配置数据源-->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/database_one?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=true&amp;serverTimeZone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="980726bgbg"/>
            </dataSource>
        </environment>
    </environments>

    <!--配置映射文件-->
    <mappers>
        <mapper resource="mapping/UserMapper.xml"/>
    </mappers>
</configuration>
  • 映射文件
<?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="mapping.UserMapper">

    <select id="getAll" resultType="com.bbg.entity.User">
        select * from user;
    </select>

    <select id="selectById" parameterType="java.lang.String" resultType="com.bbg.entity.User">
        select * from user where id = #{id}
    </select>

    <insert id="insertUser" parameterType="com.bbg.entity.User">
        insert into user values(#{id}, #{name}, #{age}, #{mobileTel})
    </insert>

    <update id="updateUser" parameterType="com.bbg.entity.User">
        update user set name = #{name}, age = #{age}, mobile_tel = #{mobileTel} where id = #{id}
    </update>

    <delete id="deleteUser" parameterType="java.lang.String">
        delete from user where id = #{id}
    </delete>

</mapper>

2、测试类

package com.bbg;

import com.bbg.entity.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 java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        List<Object> objects = sqlSession.selectList("mapping.UserMapper.getAll");
        for (Object object : objects) {
            System.out.println(object.toString());
        }

        Object o = sqlSession.selectOne("mapping.UserMapper.selectById", "20210923001");
        System.out.println(o);
        User user1 = (User) o;
        user1.setMobileTel("1888888888");
        sqlSession.update("mapping.UserMapper.updateUser", user1);
        o = sqlSession.selectOne("mapping.UserMapper.selectById", "20210923001");
        System.out.println(o);
        
        User user = new User();
        user.setId("20210923002");
        user.setAge(24);
        user.setName("superhero");
        user.setMobileTel("13235441205");
        int insert = sqlSession.insert("mapping.UserMapper.insertUser", user);
        if (insert > 0){
            System.out.println("插入成功");
        } else {
            System.out.println("插入失败");
        }

    }
}

3、整体代码结构

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值