Mybatis对数据库基本操作

先创建数据库mybatis并创建表studnt

Maven Web项目,导入依赖

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.3.14.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.14.RELEASE</version>
    </dependency>
    <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>

    <!--dbcp连接池-->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
    <dependency>
      <groupId>commons-pool</groupId>
      <artifactId>commons-pool</artifactId>
      <version>1.6</version>
    </dependency>

 在resources中创建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.zhang.dao.StudentDao">
    <select id="query" resultType="com.zhang.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.zhang.pojo.Student">
        select * from student where id=#{id}
    </select>
    <delete id="delete">
        delete from student where id=#{id}
    </delete>

</mapper>

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

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

在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="database.properties"/>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <!-- 数据库连接相关配置 ,db.properties文件中的内容-->
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.db.driver}"/>
                <property name="url" value="${jdbc.db.url}"/>
                <property name="username" value="${jdbc.db.username}"/>
                <property name="password" value="${jdbc.db.password}"/>
            </dataSource>
        </environment>
    </environments>
    <!-- mapping文件路径配置 -->
    <mappers>

        <mapper resource="mapper/studentMapper.xml"/>
    </mappers>

</configuration>

student实体类

package com.zhang.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 + '\'' +
                '}';
    }
}

studentDao层

 

package com.zhang.dao;

import com.zhang.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);
}

util包创建BaseDao封装sqlSessionFactory方法

package com.zhang.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();
    }

}

service接口

package com.zhang.service;

import com.zhang.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.zhang.service.impl;

import com.zhang.dao.StudentDao;
import com.zhang.pojo.Student;
import com.zhang.service.StudentService;
import com.zhang.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);

    }
}

测试类

package com.zhang;

import com.zhang.pojo.Student;
import com.zhang.service.StudentService;
import com.zhang.service.impl.StudentServiceImpl;

import java.util.List;

public class Test {
    public static void main(String[] args) {

        StudentService studentService=new StudentServiceImpl();
        Student student=new Student();
        List<Student> list1=studentService.query();//查询全部
        System.out.println(list1);

    }
}

运行成功

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值