Mybatis入门

Mybatis入门

步骤

1、编写User数据表

2、编写User实体类

package com.example.MyBatis_Learn1;

import org.apache.ibatis.annotations.Param;

public class User {
    private int id;
    private String userName;
    private String userPwd;

    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 getUserPwd() {
        return userPwd;
    }

    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }

    @Override
    public String toString() {
        return "User: " +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", userPwd='" + userPwd + '\'' ;
    }
}

3、编写用户映射文件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.example.MyBatis_Learn1.User">
        select * from user
    </select>
</mapper>

4、编写核心文件SqlMapConfig.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">
<!--数据库驼峰适配实体类-->
<settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

<mapper namespace="userMapper">
    <resultMap id="user" type="com.example.MyBatis_Learn1.User">
        <id property="id" column="id"/>
        <result property="userName" column="name"/>
        <result property="userPwd" column="password"/>
    </resultMap>

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

    <insert id="save" parameterType="com.example.MyBatis_Learn1.User">
        insert into user value (#{id},#{userName},#{userPwd})
    </insert>

    <update id="updata" parameterType="com.example.MyBatis_Learn1.User">
        update user set user_name = #{userName},user_pwd=#{userPwd} where id=#{id}
    </update>

    <select id="findAll" resultType="com.example.MyBatis_Learn1.User">
        select * from user
    </select>
</mapper>

5、编写测试类

package com.example.test;
import com.example.MyBatis_Learn1.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.io.InputStreamReader;
import java.util.List;

public class Test1 {

    public  void delete(int id) throws IOException {
        //获得核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("confige/sqlMapperconfig.xml");
        //获得session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获得会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        sqlSession.delete("userMapper.delete",id);
        sqlSession.commit();
        sqlSession.close();
    }

    public void updata(User user) throws IOException {
        //获得核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("confige/sqlMapperconfig.xml");
        //获得session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获得会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        sqlSession.update("userMapper.updata",user);
        sqlSession.commit();
        sqlSession.close();
    }

    public void select() throws IOException {
        //获得核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("confige/sqlMapperconfig.xml");
        //获得session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获得会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        List<User> list = sqlSession.selectList("userMapper.findAll");
        System.out.println(list);
        sqlSession.close();
    }

    public void insert(User user) throws IOException {
        //获得核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("confige/sqlMapperconfig.xml");
        //获得session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获得会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        sqlSession.insert("userMapper.save",user);
        sqlSession.commit();
        sqlSession.close();
    }

    public static void main(String[] args) throws IOException {
      
    }
}

SqlMapConfig.xml

UserMapper.xml

测试类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值