其他分享
首页 > 其他分享> > 初始化配置mybatis,以及使用mybatis实现增删改查curd操作

初始化配置mybatis,以及使用mybatis实现增删改查curd操作

作者:互联网

1:导入mybatis jar包到maven中

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.10</version>
</dependency>

2:把maven关键配置导入pom.xml中防止报错

    <build>
        <!--   **.xml写在src找不到问题解决方案     -->
        <resources>
            <resource>
                <!-- directory:指定资源文件的位置 -->
                <directory>src/main/java</directory>
                <includes>
                    <!-- “**” 表示任意级目录    “*”表示任意任意文件 -->
                    <!-- mvn resources:resources :对资源做出处理,先于compile阶段  -->
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <!--  filtering:开启过滤,用指定的参数替换directory下的文件中的参数(eg. ${name}) -->
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

3:根据官网mybatis文档开始初始化操作

首先初始化maven项目结构

在java目录先新建3个文件包,分别是utils  pojo  mapper

新建MybatisUtils.class到utils包下

package com.terry.utils;

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;

public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession() {
        return sqlSessionFactory.openSession();
    }
}

新建mybatis-config.xml到resources目录下

<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="zhang1234"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/terry/mapper/userMapper.xml"/>
    </mappers>
</configuration>

新建一个实体类user到pojo包下,对应数据库中的user

package com.terry.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;
    }
}

新建一个usermapper接口到mapper包下,接口中定义需要使用的数据库操作方法

package com.terry.mapper;

import com.terry.pojo.user;

import java.util.List;

public interface userMapper {
    //获取全部用户属性
    List<user> getUserList();

    //根据用户id获取用户属性
    user getUserById(int id);

    //增加用户
    void InsertUser(user user);
    //根据id修改用户
    void updatauser(user user);
    //根据id删除用户
    void deleteuser(int id);
}

新建一个usermapper.xml到mapper包下

<?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="com.terry.mapper.userMapper">
    <select id="getUserList" resultType="com.terry.pojo.user">
        select *
        from user
    </select>
    <select id="getUserById" parameterType="int" resultType="com.terry.pojo.user">
        select *
        from user
        where id = #{id}
    </select>
    <insert id="InsertUser" parameterType="com.terry.pojo.user">
        insert into user
        values (#{id}, #{username}, #{password})
    </insert>
    <update id="updatauser" parameterType="com.terry.pojo.user">
        update user set username=#{username},password=#{password} where id=#{id}
    </update>
    <delete id="deleteuser" parameterType="int">
        delete from user where id=#{id}
    </delete>
</mapper>

4:测试CURD代码

新建一个测试目录test

查询全部成员测试类SelectAllTest.class

import com.terry.mapper.userMapper;
import com.terry.pojo.user;
import com.terry.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class SelectAllTest {
    @Test
    public void test() {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            userMapper mapper = sqlSession.getMapper(userMapper.class);
            for (user user : mapper.getUserList()) {
                System.out.println(user.getId() + "\t" + user.getUsername() + "\t" + user.getPassword());
            }

        }
    }
}

根据id查询selectUserByid.class

import com.terry.mapper.userMapper;
import com.terry.pojo.user;
import com.terry.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class selectUserByid {
    @Test
    public void test() {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            userMapper mapper = sqlSession.getMapper(userMapper.class);
            user user = mapper.getUserById(3);
            System.out.println(user.getId() + "\t" + user.getUsername() + "\t" + user.getPassword());
        }
    }

}

根据id更新用户 updatauser.class

import com.terry.mapper.userMapper;
import com.terry.pojo.user;
import com.terry.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class updatauser {
    @Test
    public void test(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        userMapper mapper = sqlSession.getMapper(userMapper.class);
        user u=new user();
        u.setId(1);
        u.setUsername("zzz");
        u.setPassword("zhang12340");
        mapper.updatauser(u);
        sqlSession.commit();
        sqlSession.close();
    }
}

根据id删除用户 deleteuser.clas

import com.terry.mapper.userMapper;
import com.terry.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class deleteuser {
    @Test
    public void test(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        userMapper mapper = sqlSession.getMapper(userMapper.class);
        mapper.deleteuser(1);
        sqlSession.commit();
        sqlSession.close();
    }
}

插入用户操作 InsertUser.class

import com.terry.mapper.userMapper;
import com.terry.pojo.user;
import com.terry.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class InsertUser {
    @Test
    public void test() {
        user u = new user();
        u.setId(5);
        u.setUsername("java");
        u.setPassword("zhang12345");
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            userMapper mapper = sqlSession.getMapper(userMapper.class);
            mapper.InsertUser(u);
            sqlSession.commit();
            sqlSession.close();
        }

    }
}

 

标签:mapper,改查,id,curd,user,mybatis,import,com,public
来源: https://www.cnblogs.com/Sun-fish/p/16360269.html