概述 在上一篇文章MyBatis 配置详解 中已经详细地介绍了MyBatis的配置环境,算是对MyBatis有了初步的理解了。今天讲下如何使用MyBatis对数据表进行进行增删改查,本文使用的测试环境是上一篇文章中的测试环境。
基于XML文件的实现 在mapper.xml文件中写入操作语句,代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <?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 ="domain.userMapper" > <select id ="getUser" parameterType ="int" resultType ="com.domain.User" > SELECT * FROM user WHERE id = #{id} </select > <insert id ="insertUser" parameterType ="com.domain.User" > INSERT INTO user(id,name,age) VALUES (#{id},#{name},#{age}) </insert > <delete id ="deleteUser" parameterType ="int" > DELETE FROM user WHERE id = #{id} </delete > <update id ="updateUser" parameterType ="com.domain.User" > UPDATE user SET name=#{name} ,age=#{age} WHERE id = #{id} </update > <select id ="getAllUsers" resultType ="com.domain.User" > SELECT * FROM user; </select > </mapper >
测试代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 import com.domain.User;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 org.testng.annotations.BeforeSuite;import org.testng.annotations.Test;import java.io.IOException;import java.io.InputStream;import java.util.List;public class TestCURD { @BeforeSuite private SqlSession before () throws IOException { String resource = "config.xml" ; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); return sqlSessionFactory.openSession(); } @Test public void testInsert () throws IOException { SqlSession session = before(); User user = new User(); user.setName("dysmorsel" ); user.setAge(18 ); user.setId(3 ); try { int result = session.insert("domain.userMapper.insertUser" ,user); session.commit(); System.out.println(result); }finally { session.close(); } } @Test public void testDelete () throws IOException { SqlSession session = before(); try { int result = session.delete("domain.userMapper.deleteUser" ,3 ); session.commit(); System.out.println(result); }finally { session.close(); } } @Test public void testSelect () throws IOException { SqlSession session = before(); try { User user = session.selectOne("domain.userMapper.selectUser" ,3 ); System.out.println(user); }finally { session.close(); } } @Test public void testUpdate () throws IOException { SqlSession session = before(); User user = new User(); user.setName("dysmorsel" ); user.setAge(22 ); user.setId(3 ); try { int result = session.insert("domain.userMapper.updateUser" ,user); session.commit(); System.out.println(result); }finally { session.close(); } } @Test public void testGetAll () throws IOException { SqlSession session = before(); List<User> users = session.selectList("domain.userMapper.getAllUsers" ); System.out.println(users); session.close(); } }
基于接口方法的实现 在UserMapperj接口中写入操作语句,代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package com.mapping;import com.domain.User;import org.apache.ibatis.annotations.Delete;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update;import java.util.List;public interface UserMapper { @Select (value = "SELECT * FROM user WHERE id = #{id} " ) User findById (int id) ; @Insert (" INSERT INTO user(id,name,age) VALUES (#{id},#{name},#{age})" ) int insertUser (User user) ; @Delete ("delete from user where id=#{id}" ) int deleteById (int id) ; @Update ("update user set name=#{name},age=#{age} where id=#{id}" ) int update (User user) ; @Select ("select * from user" ) List<User> getAll () ; }
测试代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 import com.domain.User;import com.mapping.UserMapper;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 org.testng.annotations.BeforeSuite;import org.testng.annotations.Test;import java.io.IOException;import java.io.InputStream;import java.util.List;public class TestCURD { @BeforeSuite private SqlSession before () throws IOException { String resource = "config.xml" ; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); return sqlSessionFactory.openSession(); } @Test public void testInsert () throws IOException { SqlSession session = before(); User user = new User(); user.setName("dysmorsel" ); user.setAge(18 ); user.setId(3 ); try { UserMapper mapper = session.getMapper(UserMapper.class ) ; int result = mapper.insertUser(user); session.commit(); System.out.println(result); }finally { session.close(); } } @Test public void testDelete () throws IOException { SqlSession session = before(); try { UserMapper mapper = session.getMapper(UserMapper.class ) ; int result = mapper.deleteById(3 ); session.commit(); System.out.println(result); }finally { session.close(); } } @Test public void testSelect () throws IOException { SqlSession session = before(); try { UserMapper mapper = session.getMapper(UserMapper.class ) ; User user = mapper.selectUser(3 ); System.out.println(user); }finally { session.close(); } } @Test public void testUpdate () throws IOException { SqlSession session = before(); User user = new User(); user.setName("dysmorsel" ); user.setAge(22 ); user.setId(3 ); try { UserMapper mapper = session.getMapper(UserMapper.class ) ; int result = mapper.update(user); session.commit(); System.out.println(result); }finally { session.close(); } } @Test public void testGetAll () throws IOException { SqlSession session = before(); UserMapper mapper = session.getMapper(UserMapper.class ) ; List<User> users = mapper.getAll(); System.out.println(users); session.close(); } }
以上的相关代码是全部测试通过的,使用MyBatis对表执行CRUD操作的内容基本就是这些。