ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2.MyBatis的增删查改

2021-11-29 14:02:17  阅读:189  来源: 互联网

标签:sname String sno sex 增删 sqlSession 查改 MyBatis public


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

1.创建数据库
image

2.数据库连接测试
image

3.导入依赖:mysql、mybatis、junit
image

依赖代码
<!--导入依赖-->
    <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核心配置文件
image

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工具类
image

点击查看代码
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.编写实体类
image

点击查看代码
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
image

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

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文件注册
image

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

9.资源过滤
image

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

①增加
image
image
image

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

②删除
image
image
image

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

③改
image
image
image

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

④查
image
image

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

文件目录

标签:sname,String,sno,sex,增删,sqlSession,查改,MyBatis,public
来源: https://www.cnblogs.com/user102/p/15619082.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有