16boke - 一路博客

MyBatis学习2--完成简单的增、删、改、查操作

使用MyBatis进行数据库增删改查的操作是很简单的,主要在配置文件中写好相应的SQL语句,然后在程序中进行调用即可。具体代码如下:

一、映射配置文件

<?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="com.mapper.UserMapper">
  <select id="selectUser" parameterType="String" resultType="com.dto.UserDto">
    select * from user where username = #{username}
  </select>
  <select id="selectAllUser" resultType="com.dto.UserDto">
    select * from user 
  </select>
  <insert id="insertUser" parameterType="com.dto.UserDto">
  	insert into user(username,password,address,age,sex)
  	values (#{username},#{password},#{address},#{age},#{sex})
  </insert>
  <update id="updateUser" parameterType="com.dto.UserDto">
  	update user set password=#{password},address=#{address},age=#{age},sex=#{sex}
  	where username=#{username}
  </update>
  <delete id="deleteUser" parameterType="String">
  	delete from user where username=#{username}
  </delete>
  <delete id="deleteAllUser">
  	delete from user
  </delete>
</mapper>


二、测试类

package com.test;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.binding.MapperRegistry;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import com.dto.UserDto;
import com.mapper.UserMapper;

public class UserTest{
	SqlSessionFactory sqlSessionFactory = null;

	@Before
	public void setUp() throws Exception {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
	}

	@Test
	public void seleteXMLTest() {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			UserDto userDto = (UserDto) session.selectOne("com.mapper.UserMapper.selectUser", "test");
			System.out.println(userDto.getUsername()+"\t"+userDto.getPassword()+"\t"+userDto.getAddress()+"\t"+userDto.getAge()+"\t"+userDto.getSex());
		} finally {
			session.close();
		}
	}
	@Test
	public void seleteAllXMLTest() {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			List<UserDto> list = session.selectList("com.mapper.UserMapper.selectAllUser");
			for(UserDto userDto : list){
				System.out.println(userDto.getUsername()+"\t"+userDto.getPassword()+"\t"+userDto.getAddress()+"\t"+userDto.getAge()+"\t"+userDto.getSex());
			}
		} finally {
			session.close();
		}
	}

	@Test
	public void seleteAnnotaionTest() {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			UserMapper userMapper = session.getMapper(UserMapper.class);
			UserDto userDto = userMapper.seleteUser("test");
			System.out.println(userDto.getUsername()+"\t"+userDto.getPassword()+"\t"+userDto.getAddress()+"\t"+userDto.getAge()+"\t"+userDto.getSex());
		} finally {
			session.close();
		}
	}

	@Test
	public void insertXMLTest() {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			UserDto userDto = new UserDto();
			userDto.setUsername("test");
			userDto.setPassword("test");
			userDto.setAddress("测试地址......");
			userDto.setAge("30");
			userDto.setSex("男");
			int res = session.insert("com.mapper.UserMapper.insertUser", userDto);
			session.commit();
			System.out.println("res = " + res);
		} finally {
			session.close();
		}
	}
	@Test
	public void updateXMLTest() {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			UserDto userDto = new UserDto();
			userDto.setUsername("test");
			userDto.setPassword("111111");
			userDto.setAddress("地址");
			userDto.setAge("33");
			userDto.setSex("女");
			int res = session.update("com.mapper.UserMapper.updateUser", userDto);
			session.commit();
			System.out.println("res = " + res);
		} finally {
			session.close();
		}
	}
	@Test
	public void deleteXMLTest() {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			int res = session.delete("com.mapper.UserMapper.deleteUser", "test");
			session.commit();
			System.out.println("res = " + res);
		} finally {
			session.close();
		}
	}
	@Test
	public void deleteAllXMLTest() {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			int res = session.delete("com.mapper.UserMapper.deleteAllUser");
			session.commit();
			System.out.println("res = " + res);
		} finally {
			session.close();
		}
	}
}


注意:在更新操作完成之后需要调用session.commit()方法,不然数据不会更新到数据库中。