MyBatis增、删、改、查

时间:2021-12-28 17:39:37

1.config.xml文件的基本配置信息

MyBatis增、删、改、查

2.选择数据源

MyBatis增、删、改、查

3.mybatis约定

(1)parameterType和resultType 只能传一个参数,但是我们可以传一个数组或者集合,达到传多个参数的目的

(2)输入参数:如果是(8种常用数据类型+String),是可以使用任何占位符#{xxxx};如果是对象类型,则必须是对象的属性名#{属性名}

(3)输出类型:返回到Java实体类

4.增、删、改、查

(1)mapper.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="entity.personMapper">
     <!-- 查询 -->
     <select id="select" resultType="entity.Person" parameterType="int">
          select * from Person
   </select>

    <!--增加 -->
    <insert id="insert" parameterType="entity.Person">
         insert into Person values(#{id},#{name},#{age})
    </insert>

    <!--删除 -->
    <delete id="delete"  parameterType="int">
        delete from Person where id=#{id}
    </delete>

    <!--改 -->
    <update id="update" parameterType="entity.Person">
        update Person set name=#{name},age=#{age} where  id=#{id}
    </update>
  </mapper>

(2)Java主函数

package entity;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class starts {

    public static void main(String[] args) throws IOException {
		//insert();
    	//delete();
    	update();
    	select();
    }
    public static void select() throws IOException {

        Reader reader = Resources.getResourceAsReader("entity/config.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader,"development1");
        SqlSession session = sessionFactory.openSession();
        String statement = "entity.personMapper."+"select";
        List<Person> person = session.selectList(statement);
        System.out.println(person);
        session.close();
    }
    public static void insert() throws IOException {

        Reader reader = Resources.getResourceAsReader("entity/config.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader,"development1");
        SqlSession session = sessionFactory.openSession();
        Person person = new Person(4,"赵帅",36);
        String statement = "entity.personMapper."+"insert";
        int count = session.insert(statement,person);
        session.commit();
        System.out.println("增加"+count+"记录");
        session.close();
    }
    public static void delete() throws IOException {

        Reader reader = Resources.getResourceAsReader("entity/config.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader,"development1");
        SqlSession session = sessionFactory.openSession();
        String statement = "entity.personMapper."+"delete";
        int count = session.insert(statement,2);
        session.commit();
        System.out.println("删除"+count+"记录");
        session.close();
    }
    public static void update() throws IOException {

        Reader reader = Resources.getResourceAsReader("entity/config.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader,"development1");
        SqlSession session = sessionFactory.openSession();
        String statement = "entity.personMapper."+"update";
        Person person = new Person();
        person.setId(2);
        person.setName("*明");
        person.setAge(36);
        int count = session.update(statement,person);//注意:这里应该传实体类,不是person.getId(),不然会报错
        session.commit();
        System.out.println("更新"+count+"记录");
        session.close();
    }

}

  5.总结

(1)java的实体类,在创建有参构造器后,必须同时创建无参构造器,否则在Java程序中将无法进行实例化。