mybatis&原始方法的增删改查

//工具类
package com.csdn.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.InputStream;

/**
 * @ClassName DBUtil
 * @Description TODO 与数据库进行连接
 * @Author sudi
 * @Date 2020/5/2 13:50
 */
public class DBUtil {
    public SqlSession getSqlSession() throws IOException {
        //读取主配置文件
        InputStream inputStream = Resources.getResourceAsStream("com/csdn/config/Configuration.xml");
        //通过配置信息构建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //通过SqlSessionFactory打开数据库会话
        return sqlSessionFactory.openSession();
    }
}

//主配值文件层
<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC">
                <property name="" value=""/>
            </transactionManager>
            <dataSource type="UNPOOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/jdbc201910"/>
                <property name="username" value="root"/>
                <property name="password" value="sudi"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
<!--        <mapper resource="org/apache/ibatis/submitted/complex_property/User.xml"/>-->
        <mapper resource="com/csdn/config/sql/User.xml"/>
    </mappers>

</configuration>

mybatis原始方法的删

//dao层
package com.csdn.dao;

import com.csdn.util.DBUtil;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;

/**
 * @ClassName
 * @Description TODO
 * @Author sudi
 * @Date 2020/5/2 14:09
 */
public class UserDao {
    public void deleteUserById(int id) throws IOException {
        SqlSession sqlSession = null;
        try {
            sqlSession = new DBUtil().getSqlSession();
            sqlSession.delete("deleteById", id);
            sqlSession.commit();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        UserDao userDao = new UserDao();
        userDao.deleteUserById(1);
    }
}

//子配置文件层
<?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="User">
<!--    id = #{} 括号中放啥都行-->
<delete id="deleteById">
    delete from t_person where id = #{id}
</delete>
</mapper>

mybatis原始方法的增

/**
     * 插入用户方法
     * @param id int
     * @param name String
     * @param password String
     */
    public void insertUser(int id, String name, String password) throws IOException {
        SqlSession sqlSession = null;
        try {
            // 1、获取sqlSession实例  ctrl + alt + v
            sqlSession = new DBUtil().getSqlSession();
            // 2、调用sqlSession的insert方法
            sqlSession.insert("insertUser", new User(id, name, password));
            // 3、提交
            sqlSession.commit();
        } finally {
            // 4、关闭资源
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    /**
     * 插入方法
     * @param user User
     */
    public void insertUserByUser(User user) throws IOException {
        SqlSession sqlSession = null;
        try {
            // 1、获取sqlSession实例  ctrl + alt + v
            sqlSession = new DBUtil().getSqlSession();
            // 2、调用sqlSession的insert方法
            sqlSession.insert("insertUserByUser", user);
            // 3、提交
            sqlSession.commit();
        } finally {
            // 4、关闭资源
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
//子配置文件层
 <insert id="insertUser" parameterType="com.colin.bean.User">
    insert into user values(#{id}, #{name}, #{password})
  </insert>
  <insert id="insertUserByUser">
    insert into user values(#{id}, #{name}, #{password})
  </insert>
  • 注意:当有多个参数传进来的时候,子配置文件中i
    insert into user values(#{id}, #{name}, #{password})
    的传入参数名字必须与User里边的成员变量名字一样。

mybatis原始方法的改

/**
     * 修改用户
     * @param user User
     */
    public void updateUserByUser(User user) throws IOException {
        SqlSession sqlSession = null;
        try {
            sqlSession = new DBUtil().getSqlSession();
            sqlSession.update("updateByUser", user);
            sqlSession.commit();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
//Map方法在主方法实现
    public void updateUserByMap(Map<String, Object> map) throws IOException {
        SqlSession sqlSession = null;
        try {
            sqlSession = new DBUtil().getSqlSession();
            sqlSession.update("updateUserByMap", map);
            sqlSession.commit();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
主方法实现
		userDao.updateUserByUser(new User(888, "memeda", "dashagua"));

        Map<String, Object> map = new HashMap<>();
        map.put("id", 888);
        map.put("password", "yyyy");
        userDao.updateUserByMap(map);
// 子配置文件
  <update id="updateByUser">
    update user set name = #{name}, password = #{password} where id = #{id}
  </update>
  <update id="updateUserByMap">
    update user set password = #{password} where id = #{id}
  </update>

mybatis原始方法的查

//dao层
// 查询
    public User selectUserById(int id) throws IOException {
        SqlSession sqlSession = null;
        try {
            // 1、获取sqlSession实例
            sqlSession = new DBUtil().getSqlSession();
            // 2、调用selectOne方法
            User user = sqlSession.selectOne("selectUserById", id);
            return user;
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    // 注意,查询所有的子配置文件中的resultType属性放的值是泛型的类型
    public List<User> selectAll() throws IOException {
        SqlSession sqlSession = null;
        try {
            sqlSession = new DBUtil().getSqlSession();
            List<User> list = sqlSession.selectList("selectAll");
            return list;
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
//子配置文件
<select id="selectUserById" resultType="com.colin.bean.User">
    select id, name, password from user where id = #{fangshadouxing}
  </select>
  <select id="selectAll" resultType="com.colin.bean.User">
    select id, name, password from user
  </select>
  • 子配置文件selectAll中resultType为什么是User而不是List呢?
  • 因为在源码中会循环查询数据,将User类型的数据通过List中的add方法一个一个的塞进List创建的对象中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值