1.根据ID查询用户
接口类:
public interface UserMapper { // 根据ID查询用户 User getUserById(int id); }
UserMapper.xml:
parameterType为传输的参数数据类型
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="top.imustctf.dao.UserMapper"> <select id="getUserById" resultType="top.imustctf.pojo.User" parameterType="int"> select * from mybatis.user where id = #{id} </select> </mapper>
测试类:
@Test public void test() { SqlSession sqlSession = MybatisUtils.getSqlSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.getUserById(1); System.out.println(user); // User{id=1, name='dahe', password='123456'} } catch (Exception e) { throw new RuntimeException(e); } finally { sqlSession.close(); } }
2.添加用户
接口类:
public interface UserMapper { // 添加用户 int addUser(User user); }
UserMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="top.imustctf.dao.UserMapper"> <insert id="addUser" parameterType="top.imustctf.pojo.User"> insert into mybatis.user (id,name,password) values (#{id},#{name},#{password}) </insert> </mapper>
测试类:
public class UserMapperTest { @Test public void test() { SqlSession sqlSession = MybatisUtils.getSqlSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = new User(4, "meimei", "admin"); int i = userMapper.addUser(user); System.out.println(i); // 1 } catch (Exception e) { throw new RuntimeException(e); } finally { sqlSession.close(); } } }
3.更新用户
接口类:
public interface UserMapper { // 更新用户 int updateUser(User user); }
UserMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="top.imustctf.dao.UserMapper"> <update id="updateUser" parameterType="top.imustctf.pojo.User"> update mybatis.user set name=#{name} , password=#{password} where id = #{id} </update> </mapper>
测试类:
public class UserMapperTest { @Test public void test() { SqlSession sqlSession = MybatisUtils.getSqlSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = new User(5,"titi", "password"); int i = userMapper.updateUser(user); System.out.println(i); // 1 } catch (Exception e) { throw new RuntimeException(e); } finally { sqlSession.close(); } } }
4.根据ID删除用户
接口类:
public interface UserMapper { // 删除用户 int delUserById(int id); }
UserMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="top.imustctf.dao.UserMapper"> <delete id="delUserById"> delete from mybatis.user where id = #{id} </delete> </mapper>
测试类:
@Test public void test() { SqlSession sqlSession = MybatisUtils.getSqlSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); int i = userMapper.delUserById(1); System.out.println(i); // 1 } catch (Exception e) { throw new RuntimeException(e); } finally { sqlSession.close(); } }
5.模糊查询
接口类:
public interface UserMapper { // 模糊查询 List<User> getUserLike(String value); }
UserMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="top.imustctf.dao.UserMapper"> <select id="getUserLike" resultType="top.imustctf.pojo.User"> select * from mybatis.user where name like "%"#{value}"%" </select> </mapper>
测试类:
public class UserMapperTest { @Test public void test() { SqlSession sqlSession = MybatisUtils.getSqlSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> userList = userMapper.getUserLike("i"); for (User user : userList) { System.out.println(user); } } catch (Exception e) { throw new RuntimeException(e); } finally { sqlSession.close(); } } }