Mybatis对数据库基本操作

创建一个Maven Web项目

创建数据库mybatis并创建表studnt

导入依赖MySQL、mybatis-plus

 <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.45</version>
    </dependency>
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus</artifactId>
      <version>3.3.2</version>
    </dependency>

创建student实体类

package com.wang.pojo;

public class Student {
    private Integer id;
    private String name;
    private String sex;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

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

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

在resources下创建mapper文件夹

在mapper下创建studentMapper.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">
<!-- 1.namespace:接口的一个路径 ,2.id :接口下抽象方法名
    3.接口的返回值和resultType 对应上 ,4接口的参数 对应上-->
<mapper namespace="com.wang.dao.StudentDao">
    <select id="query" resultType="com.wang.pojo.Student">
       select * from student
    </select>
    <insert id="add">
        insert  into student(name,sex) values (#{name},#{sex})
    </insert>
    <update id="update">
        update student set name=#{name},sex=#{sex} where id=#{id}
    </update>
    <select id="queryById" resultType="com.wang.pojo.Student">
        select * from student where id=#{id}
    </select>
    <delete id="delete">
        delete from student where id=#{id}
    </delete>

</mapper>

创建db.properties文件放入数据库驱动等参数

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
db.username=root
db.password=root

在resources下创建mybatis-config.xml

<?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>
    <!-- 环境配置 -->
    <!-- 加载类路径下的属性文件 -->
    <properties resource="db.properties"/>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <!-- 数据库连接相关配置 ,db.properties文件中的内容-->
            <dataSource type="POOLED">
                <property name="driver" value="${db.driver}"/>
                <property name="url" value="${db.url}"/>
                <property name="username" value="${db.username}"/>
                <property name="password" value="${db.password}"/>
            </dataSource>
        </environment>
    </environments>
    <!-- mapping文件路径配置 -->
    <mappers>
       
        <mapper resource="mapper/studentMapper.xml"/>
    </mappers>

</configuration>

创建studentDao层

package com.wang.dao;

import com.wang.pojo.Student;

import java.util.List;

public interface StudentDao {
    public List<Student> query();

    public void add(Student student);

    public void update(Student student);

    public Student queryById(Integer id);

    public void delete(Integer id);
}

创建service接口

package com.wang.service;

import com.wang.pojo.Student;

import java.util.List;

public interface StudentService {
    public List<Student> query();

    public void add(Student student);

    public void update(Student student);

    public Student queryById(Integer id);

    public void delete(Integer id);
}

创建serviceimpl类

package com.wang.service.impl;

import com.wang.dao.StudentDao;
import com.wang.pojo.Student;
import com.wang.service.StudentService;
import com.wang.util.BaseDao;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentServiceImpl extends BaseDao implements StudentService {
    SqlSession session;
    StudentDao dao;
    public List<Student> query() {
        session=getSession();
        dao=session.getMapper(StudentDao.class);
        List<Student> list=dao.query();
        closeSession(session);
        return list;
    }


    public void add(Student student) {
        session=getSession();
        dao=session.getMapper(StudentDao.class);
        dao.add(student);
        session.commit();
        closeSession(session);

    }


    public void update(Student student) {
        session=getSession();
        dao=session.getMapper(StudentDao.class);
        dao.update(student);
        session.commit();
        closeSession(session);

    }


    public Student queryById(Integer id) {
        session=getSession();
        dao=session.getMapper(StudentDao.class);
        Student student=dao.queryById(id);
        session.commit();
        closeSession(session);
        return student;
    }


    public void delete(Integer id) {
        session=getSession();
        dao=session.getMapper(StudentDao.class);
        dao.delete(id);
        session.commit();
        closeSession(session);

    }
}

创建util包创建BaseDao封装sqlSessionFactory方法

package com.wang.util;

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

public class BaseDao {
    static SqlSessionFactory sqlMapper;
    static {
        String resources="mybatis-config.xml";
        Reader reader=null;
        try {
            reader = Resources.getResourceAsReader(resources);
        } catch (IOException e) {
            e.printStackTrace();
        }
         sqlMapper = new SqlSessionFactoryBuilder().build(reader);
    }
    public SqlSession getSession(){
        SqlSession session = sqlMapper.openSession();
        return session;
    }

    public void closeSession(SqlSession session){
        session.close();
    }

}

创建测试类

package com.wang;

import com.wang.pojo.Customer;
import com.wang.pojo.Student;
import com.wang.service.CustomerService;
import com.wang.service.StudentService;
import com.wang.service.impl.CustomerServiceImpl;
import com.wang.service.impl.StudentServiceImpl;

import java.util.List;

public class Test {
    public static void main(String[] args) {
       
        StudentService studentService=new StudentServiceImpl();
        Student student=new Student();
        /* student.setName("王二");
        student.setSex("男");*/
        //studentService.add(student);//添加一条
        /* student.setId(3);
        student.setName("王五");
        student.setSex("女");*/
        //studentService.update(student);//根据id修改
        //student=studentService.queryById(2);//根据id查询
        //studentService.delete(2);//根据id删除
        List<Student> list1=studentService.query();//查询全部
        System.out.println(list1);

    }
}

运行结果为

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AqrJan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值