2.MyBatis的增删查改

1.创建数据库
2.数据库连接测试
3.导入依赖:mysql、mybatis、junit
4.编写mybatis-config.xml核心配置文件
5.编写Utils工具类
6.编写实体类
7.编写Mapper
8.Mapper文件注册
9.资源过滤
10.测试

1.创建数据库
2.MyBatis的增删查改

2.数据库连接测试
2.MyBatis的增删查改

3.导入依赖:mysql、mybatis、junit
2.MyBatis的增删查改

依赖代码
<!--导入依赖-->
    <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核心配置文件
2.MyBatis的增删查改

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工具类
2.MyBatis的增删查改

点击查看代码
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.编写实体类
2.MyBatis的增删查改

点击查看代码
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
2.MyBatis的增删查改

Mapper接口
public interface StudentMapper {
    public int addStudent(Student student);
    public int subStudent(String sno);
    public int updateStudent(Student student);
    public Student getStudentForSno(String sno);
}

2.MyBatis的增删查改

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="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文件注册
2.MyBatis的增删查改

点击查看代码
    <mappers>
        <mapper resource="com/user102/dao/StudentMapper.xml"></mapper>
    </mappers>

9.资源过滤
2.MyBatis的增删查改

代码过滤
    <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.测试

①增加
2.MyBatis的增删查改
2.MyBatis的增删查改
2.MyBatis的增删查改

增加测试代码
    public void addStudent(){
        SqlSession sqlSession = Utils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        mapper.addStudent(new Student("160121454125","愚公公",60,"男","太行山"));
        sqlSession.commit();
        sqlSession.close();
    }

②删除
2.MyBatis的增删查改
2.MyBatis的增删查改
2.MyBatis的增删查改

删除测试代码
    public void subStudent(){
        SqlSession sqlSession = Utils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        mapper.subStudent("160121454124");
        sqlSession.commit();
        sqlSession.close();
    }

③改
2.MyBatis的增删查改
2.MyBatis的增删查改
2.MyBatis的增删查改

修改测试代码
    public void updateStudent(){
        SqlSession sqlSession = Utils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        mapper.updateStudent(new Student("160121454125","姜太公",80,"男","湖中亭"));
        sqlSession.commit();
        sqlSession.close();
    }

④查
2.MyBatis的增删查改
2.MyBatis的增删查改

查询测试代码
    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();
    }

文件目录
2.MyBatis的增删查改

上一篇:自己珍藏的数据库SQL基础练习题答案


下一篇:[学习笔记/做题记录]李超树