1. 程式人生 > 其它 >2.MyBatis的增刪查改

2.MyBatis的增刪查改

1.建立資料庫
2.資料庫連線測試
3.匯入依賴:mysql、mybatis、junit
4.編寫mybatis-config.xml核心配置檔案
5.編寫Utils工具類
6.編寫實體類
7.編寫Mapper
8.Mapper檔案註冊
9.資源過濾
10.測試

1.建立資料庫

2.資料庫連線測試

3.匯入依賴:mysql、mybatis、junit

依賴程式碼
<!--匯入依賴-->
    <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.5.2</version>
        </dependency>
        <!--單元測試架包-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

4.編寫mybatis-config.xml核心配置檔案

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">

<!--configuration核心配置檔案-->
<!-- 配置檔案的根元素 -->
<configuration>
   
    <!-- 和spring整合後 environments配置將廢除 -->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

5.編寫Utils工具類

點選檢視程式碼
package com.user102;

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.InputStream;

public class Utils {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try {
            String resources="mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resources);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }

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

6.編寫實體類

點選檢視程式碼
package com.user102.pojo;

public class Student {
    private String sno;
    private String sname;
    private int age;
    private String sex;
    private String address;

    public Student(String sno, String sname, int age, String sex, String address) {
        this.sno = sno;
        this.sname = sname;
        this.age = age;
        this.sex = sex;
        this.address = address;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sno='" + sno + '\'' +
                ", sname='" + sname + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                '}'+"\n";
    }

    public String getSno() {
        return sno;
    }

    public void setSno(String sno) {
        this.sno = sno;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

7.編寫Mapper

Mapper介面
public interface StudentMapper {
    public int addStudent(Student student);
    public int subStudent(String sno);
    public int updateStudent(Student student);
    public Student getStudentForSno(String sno);
}
![image](https://img2020.cnblogs.com/blog/2221517/202111/2221517-20211129133408796-1143794195.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">

<mapper namespace="com.user102.dao.StudentMapper">
    <insert id="addStudent" parameterType="com.user102.pojo.Student">
        insert into mybatis.student values('${sno}','${sname}',${age},'${sex}','${address}')
    </insert>
    <delete id="subStudent" parameterType="String">
        delete from mybatis.student where sno='${sno}'
    </delete>
    <update id="updateStudent" parameterType="com.user102.pojo.Student">
        update mybatis.student set sno='${sno}',sname='${sname}',age=${age},sex='${sex}',address='${address}'
        where sno='${sno}'
    </update>
    <select id="getStudentForSno" parameterType="String" resultType="com.user102.pojo.Student">
        select * from mybatis.student where sno='${sno}'
    </select>
</mapper>

8.Mapper檔案註冊

點選檢視程式碼
    <mappers>
        <mapper resource="com/user102/dao/StudentMapper.xml"></mapper>
    </mappers>

9.資源過濾

程式碼過濾
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

10.測試

①增加


點選檢視程式碼
    public void addStudent(){
        SqlSession sqlSession = Utils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        mapper.addStudent(new Student("160121454125","愚公公",60,"男","太行山"));
        sqlSession.commit();
        sqlSession.close();
    }

②刪除


點選檢視程式碼
    public void subStudent(){
        SqlSession sqlSession = Utils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        mapper.subStudent("160121454124");
        sqlSession.commit();
        sqlSession.close();
    }

③改


點選檢視程式碼
    public void updateStudent(){
        SqlSession sqlSession = Utils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        mapper.updateStudent(new Student("160121454125","姜太公",80,"男","湖中亭"));
        sqlSession.commit();
        sqlSession.close();
    }

④查

點選檢視程式碼
    public void getStudentForId(){
        SqlSession sqlSession = Utils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student studentForSno = mapper.getStudentForSno("160121454125");
        System.out.println(studentForSno);
        sqlSession.commit();
        sqlSession.close();
    }