利用Mybatis实现mysql数据库的增删改查功能

利用Mybatis实现mysql数据库的增删改查功能

创建一个maven项目

在pom中依次导入 mysql>mybatis>junit依赖 最后在build中配置resources,来防止我们资源导出失败的问题

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>Myabtis-Study</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>Mybatis-01</module>
        <module>Mybatis-02</module>
    </modules>


    <!-- 导入依赖-->
    <dependencies>
        <!-- mysql 驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!-- mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <!-- junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>

    <!--在build中配置resources,来防止我们资源导出失败的问题-->

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

</project>

第一步创建数据库

随便填写几行数据

第二步

编写mybatis-config.xml文件在mybatis官网有示例(这一步的作用就是连接mysql数据库)https://mybatis.org/mybatis-3/zh/getting-started.html
在这里插入图片描述

第三步创建utils工具类

在这里插入图片描述

第四步编写实体类


/**
 * @author Dou
 * @site 第三步 写实体类
 * @company Dou
 * @create 2020-12-29 15:33
 */

//实体类
public class User {
    private int id;
    private String name;
    private String pwd;

    public User() {
    }

    public User(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }
}

在这里插入代码片

第五步编写接口类


import com.Dou.pojo.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author Dou
 * @site 第四步 写接口
 * @company Dou
 * @create 2020-12-29 15:42
 */
public interface UserDao {
    //查询全部用户
    List<User> getUserList();
    //根据id查询
    User query(int id);
    //新增一个用户
    int addUser(User user);
    //修改用户信息
    int updateUser (User user);
    //删除一个用户
    int deleteUser(int id);

}

在这里插入代码片

第六步

编写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">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.Dou.dao.UserDao">
    <!-- select查询语句-->
    <!-- id ="就是对应的namespace中的方法名"-->
    <!-- sql语句执行的返回值-->
    <!--全查-->
    <select id="getUserList" resultType="com.Dou.pojo.User">
        select * from mybatis.user
    </select>

    <!--通过id查找-->
    <select id="query" parameterType="int" resultType="com.Dou.pojo.User">
        select * from mybatis.user where id = #{id}
    </select>
    
    <!--新增一个用户-->
    <insert id="addUser" parameterType="com.Dou.pojo.User">
        insert into mybatis.user (id, name, pwd) values (#{id},#{name},#{pwd});
    </insert>
    <!--修改用户信息-->
    <update id="updateUser" parameterType="com.Dou.pojo.User">
        update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id};
    </update>
    <!--根据id删除一个用户-->
    <delete id="deleteUser" parameterType="int">
        delete from mybatis.user where id=#{id};
    </delete>


</mapper>

第七步

编写测试类(编写测试类的时候 增删改 都需要提交事务)

package com.Dou.dao;

import com.Dou.pojo.User;
import com.Dou.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;

//最后一步 写测试类
public class UserDaoTest {

@Test
public void test(){


    //第一步:获得sqlsession对象
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    //方式一:getmapper
    UserDao mapper = sqlSession.getMapper(UserDao.class);
    List<User> userList = mapper.getUserList();

    //方式二:
    //List<User> userList = sqlSession.selectList("com.Dou.dao.UserDao.getUserList");

    for (User user : userList) {
        System.out.println(user);
        
    }
    //关闭sqlsession
    sqlSession.close();


}

@Test
public void getUserById(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();

    UserDao mapper = sqlSession.getMapper(UserDao.class);
    User userById = mapper.query(1);

    System.out.println(userById);

    sqlSession.close();

}
//增删改需要提交事务
@Test
public void addUser(){
    //获取sqlsession对象
    SqlSession sqlSession = MybatisUtils.getSqlSession();

    //get取出
    UserDao mapper = sqlSession.getMapper(UserDao.class);

    int res = mapper.addUser(new User(4, "哈哈", "123456"));
    if(res > 0){
        System.out.println("插入成功");
    }
    //提交事务
     sqlSession.commit();


    sqlSession.close();
}
//修改用户信息
@Test
public void updateUser(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();

    UserDao mapper = sqlSession.getMapper(UserDao.class);

    mapper.updateUser(new User(4,"呵呵","123546"));

    sqlSession.commit();
    sqlSession.close();
}
//根据id删除用户信息
@Test
public void deleteUser(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();

    UserDao mapper = sqlSession.getMapper(UserDao.class);
    int i = mapper.deleteUser(1);

    sqlSession.commit();
    sqlSession.close();

}

之中可能会出现找不到方法名的错误,把mybatis-config.xml文件中的代码复制到记事本中再复制回来就可以了


版权声明:本文为qq_45199095原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。