mybatis配置XML及注解,实现简单的增删改查

配置MyBatis

1.添加Mybatis的配置文件mybatis.xml
mybatis配置XML及注解,实现简单的增删改查_第1张图片

  • 配置jdbc连接:
    mybatis配置XML及注解,实现简单的增删改查_第2张图片

2.定义表所对应的实体类,如下图所示:
mybatis配置XML及注解,实现简单的增删改查_第3张图片

3.定义操作users表的sql映射文件userMapper.xml
mybatis配置XML及注解,实现简单的增删改查_第4张图片

4.编写XML的测试代码:

package cn.et.mybatistest.service;


import cn.et.mybatistest.bean.User;
import cn.et.mybatistest.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

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

public class UserDaoTest {

    @Test//通过UserId获得用户
    public void testGetUserById() throws IOException {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        User user = sqlSession.selectOne("cn.et.mybatistest.service.UserDao.getUserById", 2);
        System.out.println(user);
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        sqlSession.close();
        User userById = mapper.getUserById(5);
        System.out.println(userById);
    }

    @Test//所有用户
    public void testGetUser() throws IOException {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List userList = mapper.getUser();
        for(User user : userList){
            System.out.println(user);
        }
        sqlSession.close();
    }

    @Test//通过id删除用户
    public void testDeleteById() throws IOException {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        mapper.deleteById(1);
        sqlSession.commit();
        sqlSession.close();
    }

    @Test//更新用户
    public void testUpdateUser() throws IOException {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User user = new User();
        user.setId(5);
        user.setSex("男");
        user.setName("小明");
        user.setBalance(410.0);
        mapper.updateUser(user);
        sqlSession.commit();
        sqlSession.close();
    }

    @Test//添加用户
    public void testAddUser() throws IOException {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User user = new User();
        user.setName("小微");
        user.setSex("女");
        user.setBalance(564.0);
        mapper.addUser(user);
        sqlSession.commit();
        sqlSession.close();
    }

    @Test//通过姓名与姓别查询用户
    public void testGetUserByLikeName() throws IOException {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List userList = mapper.getUserByLikeName("小", "女");
        System.out.println(userList);
    }
}

5. 编写XML的测试代码:

package cn.et.mybatistest.service;

import cn.et.mybatistest.bean.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface UserAnnoDao {
    //获得所有用户
    @Select("Select * from t_user")
    List getAll();

    //通过名字模糊查询
    @Select("select u.* from t_user u where u.name like '%${nameUser}%'")
    List getUserByName(@Param("nameUser") String nameUser);

    //更新用户
    @Update("update t_user set name=#{name},balance=#{balance},sex=#{sex} where id=#{id}")
    void updateUser(User user);

    //添加用户
    @Insert("insert into t_user (name,balance,sex) values(#{name},#{balance},#{sex})")
    void addUser(User user);
}

6、编写注解的测试代码:

package cn.et.mybatistest.service;

import cn.et.mybatistest.bean.User;
import cn.et.mybatistest.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

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

import static org.junit.Assert.*;

public class UserAnnoDaoTest {

    @Test
    public void getAll() throws IOException {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserAnnoDao mapper = sqlSession.getMapper(UserAnnoDao.class);
        List all = mapper.getAll();
        for (User user : all) {
            System.out.println(user);
        }
    }

    @Test
    public void getUserByName() throws IOException {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserAnnoDao mapper = sqlSession.getMapper(UserAnnoDao.class);
        List userList = mapper.getUserByName("小");
        for (User user:userList){
          System.out.println(user);
        }
    }

    @Test
    public void updateUser() throws IOException {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserAnnoDao mapper = sqlSession.getMapper(UserAnnoDao.class);
        User user=new User();
        user.setId(5);
        user.setName("小明");
        user.setBalance(800.0);
        user.setSex("男");
        mapper.updateUser(user);
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void addUser() throws IOException {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserAnnoDao mapper = sqlSession.getMapper(UserAnnoDao.class);
        User user=new User();
        user.setName("小话");
        user.setBalance(800.0);
        user.setSex("男");
        mapper.addUser(user);
        sqlSession.commit();
        sqlSession.close();
    }
}

你可能感兴趣的:(框架)