Mybatis
CRUD操作
在写完第一个Mybatis程序后,我们对Mybatis也有了一定的认识,也逐渐了解了Mybatis的强大之处。接下来,我们来进行传统的增删改查操作来全面地展示Mybatis。
mapper接口层代码
package com.charles.dao; import com.charles.pojo.UserInfo; import java.util.List; public interface UserMapper { // 查询所有用户 List<UserInfo> queryAll(); // 通过id查询用户 UserInfo queryUserById(int iduserInfo); // 添加一个用户 int addUser(UserInfo userInfo); // 修改一个用户 int updateUser(UserInfo userInfo); // 删除一个用户 int deleteUser(int iduserInfo); }
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.charles.dao.UserMapper"> <select id="queryAll" resultType="com.charles.pojo.UserInfo"> select * from userinfo; </select> <select id="queryUserById" resultType="com.charles.pojo.UserInfo"> select * from userinfo where iduserInfo=#{iduserInfo}; </select> <insert id="addUser" parameterType="com.charles.pojo.UserInfo"> insert into userinfo values(#{iduserInfo},#{username},#{password},#{perms}); </insert> <update id="updateUser" parameterType="com.charles.pojo.UserInfo"> update userinfo set username=#{username},password=#{password},perms=#{perms} where iduserInfo=#{iduserInfo}; </update> <delete id="deleteUser" parameterType="int"> delete from userinfo where iduserInfo=#{iduserInfo}; </delete> </mapper>
测试类:
package com.charles.dao; import com.charles.pojo.UserInfo; import com.charles.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class UserInfoDaoTest { @Test public void queryAll(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); List<UserInfo> userInfos = mapper.queryAll(); for (UserInfo user : userInfos){ System.out.println(user); } // 使用后需要及时关闭,跟JDBC差不多. session.close(); } // 通过Id查询用户 @Test public void queryUserById(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); UserInfo userInfo = mapper.queryUserById(2); System.out.println(userInfo); session.close(); } // 添加一个用户 @Test public void addUser(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); int cc = mapper.addUser(new UserInfo(4, "CC", "123", "user:null")); if (cc > 0) System.out.println("添加成功!!"); else System.out.println("添加失败~~"); // 一定要提交,否则数据无效 session.commit(); session.close(); } // 修改一个用户 @Test public void updateUser(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); int charles = mapper.updateUser(new UserInfo(1, "Charles", "1234", "user:gold")); if (charles>0) System.out.println("修改成功"); else System.out.println("修改失败"); session.commit(); session.close(); } // 删除一个用户 @Test public void deleteUser(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); int i = mapper.deleteUser(4); if (i>0) System.out.println("删除成功"); else System.out.println("用户不存在"); session.commit(); session.close(); } }
结果展示:
1. 通过Id查询
2. 添加用户
3. 修改用户
4. 删除用户,删除后再次运行
以上便是CRUD的操作了。