增删改查实现
public class UserDaoTest { @Test public void test() { SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> userList = userMapper.getUserList(); for (User user : userList) { System.out.println(user); } sqlSession.close(); } @Test public void test01(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.addUser(new User(12,"张三",12)); sqlSession.commit(); sqlSession.close(); } @Test public void test02(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.updateUser(new User(12,"李四",13)); sqlSession.commit(); sqlSession.close(); } @Test public void test03(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.deleteUser(12); sqlSession.commit(); sqlSession.close(); } @Test public void test04(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User userById = mapper.getUserById(10); System.out.println(userById); sqlSession.close(); } }
<select id="getUserList" resultType="com.guo.pojo.User"> select * from user </select> <insert id="addUser" parameterType="com.guo.pojo.User" > insert into user(id,name,pwd) values(#{id},#{name},#{pwd}) </insert> <update id="updateUser" parameterType="com.guo.pojo.User"> update user set name=#{name},pwd=#{pwd} where id=#{id} </update> <delete id="deleteUser" parameterType="int"> delete from user where id=#{id} </delete> <select id="getUserById" parameterType="int" resultType="com.guo.pojo.User"> select * from user where id=#{id} </select>