第二个mybatis程序增删改查

以后的增删改查操作都只跟接口类有关
在这里插入图片描述
配置文件中namespace中的名称为对应Mapper接口或者Dao接口的完整包名,必须一致
select标签功能 选择,查询
id就是namespace中对应的方法名
resultType是sql语句的返回值
parameterType参数类型

UserMapper.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">
        <!--上面是头文件-->
        <!--namespace是绑定一个Dao/Mapper接口-->
<mapper namespace="com.wjx.dao.Userdao">
<!--select查找-->
<!--resultType是返回类型,显而易见该返回值为List<User>配置文件不像java类可以寻找关联,返回类型要写权限名-->
<select id="selectUser" resultType="com.wjx.pojo.User" >
    select * from user
  </select>

</mapper>

Userdao接口中对应的代码块

package com.wjx.dao;

import com.wjx.pojo.User;

import java.util.List;

public interface Userdao {
    //查询全部用户
    List<User> selectUser();
//根据id查询用户
    User selectUserById(int id);//返回值 方法名(传入参数)  写完之后原来是写对应实体类  现在是在xml中扩充
    //insert插入一个用户
   int addUser(User user);
   //修改
   int update(User user);
   //删除
   int delUser(int id);
}

UserMapper中对应的代码块

<?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">
        <!--上面是头文件-->
        <!--namespace是绑定一个Dao/Mapper接口-->
<mapper namespace="com.wjx.dao.Userdao">
<!--select查找-->
<!--resultType是返回类型,显而易见该返回值为List<User>配置文件不像java类可以寻找关联,返回类型要写权限名-->
<select id="selectUser" resultType="com.wjx.pojo.User" >
    select * from user
  </select>
    <!--parameterType传入参数类型-->
<select id="selectUserById" parameterType="int" resultType="com.wjx.pojo.User">
    select * from user where id=#{id}
</select>
    <!--对象中的属性可以直接取出-->
    <insert id="addUser" parameterType="com.wjx.pojo.User">
        insert into user(id,`name`,pwd) values (#{id},#{name},#{pwd})
    </insert>
    <update id="update" parameterType="com.wjx.pojo.User">
        update user set `name`=#{name},pwd=#{pwd} where id=#{id}
    </update>
    <delete id="delUser" parameterType="int">
        delete  from user where id=#{id}
    </delete>
</mapper>

import com.wjx.pojo.User;
import com.wjx.util.mybatis;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;

public class UserDaoTest {
  @Test
public void getUserById(){
//第一步获得sqlSession对象
        SqlSession sqlSession = mybatis.getSqlSession();//main下的util中的mybatis中返回值
        //只有中间部分需要改变
      Userdao mapper = sqlSession.getMapper(Userdao.class);//获得接口
        User user = mapper.selectUserById(1);
        System.out.println(user);
        // 关闭sqlSession
        sqlSession.close();
}
//增删改需要提交事务
@Test
public void addUser(){
    //第一步获得sqlSession对象
    SqlSession sqlSession = mybatis.getSqlSession();//main下的util中的mybatis中返回值
    //只有中间部分需要改变
    Userdao mapper = sqlSession.getMapper(Userdao.class);//获得接口
    int newAdd = mapper.addUser(new User(4, "吴佳欣", "12306"));//传入的参数是一个User所以要new一个User
// 关闭sqlSession
//    提交事务 不提交事务会导致插入不成功
    sqlSession.commit();
    sqlSession.close();
}
@Test
    public void updateTest(){
        //第一步获得sqlSession对象
        SqlSession sqlSession = mybatis.getSqlSession();//main下的util中的mybatis中返回值
        //只有中间部分需要改变
        Userdao mapper = sqlSession.getMapper(Userdao.class);//获得接口
        int up = mapper.update(new User(4, "小吴", "123336"));//传入的参数是一个User所以要new一个User
// 关闭sqlSession
//    提交事务 不提交事务会导致插入不成功
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void delUser(){
        //第一步获得sqlSession对象
        SqlSession sqlSession = mybatis.getSqlSession();//main下的util中的mybatis中返回值
        //只有中间部分需要改变
        Userdao mapper = sqlSession.getMapper(Userdao.class);//获得接口
        int up = mapper.delUser(4);//传入的参数是一个User所以要new一个User
// 关闭sqlSession
//    提交事务 不提交事务会导致插入不成功
        sqlSession.commit();
        sqlSession.close();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值