mybatis实现增删改查
在第一次实现对数据库的查询后,练习了对数据库的增删改查;修改UserMapper接口文件和UserMapper.xml文件。还需修改测试文件进行测试
UserMapper.java
package dao; import pojo.User; import java.util.List; public interface UserMapper { //查询全部用户 List<User> getUserList(); //根据ID查询用户 User getUserById(int id); //插入一个用户 int addUser(User user); //修改用户 int updateUser(User user); //删除用户 int deleteUser(int id); }
UserMapper.xml
<?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"> <!--namespace=绑定一个对应的Dao/Mapper接口--> <mapper namespace="dao.UserMapper"> <!--id为接口方法,resultType为返回值类型,parameterType参数类型--> <select id="getUserList" resultType="pojo.User"> select * from mybatis.user </select> <select id="getUserById" parameterType="int" resultType="pojo.User"> select * from mybatis.user where id = #{id} </select> <!--对象中的属性可以直接取出来--> <insert id="addUser" parameterType="pojo.User"> insert into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd}) </insert> <update id="updateUser" parameterType="pojo.User"> update mybatis.user set name =#{name},pwd=#{pwd} where id = #{id} </update> <delete id="deleteUser" parameterType="int"> delete from mybatis.user where id=#{id} </delete> </mapper>
测试文件:UserDaoTest.java
package dao; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import pojo.User; import utils.MybatisUtils; import java.util.List; public class UserDaoTest { @Test public void test() { //获得SqlSession对象 SqlSession sqlSession=MybatisUtils.getSqlSession(); //执行Sql UserMapper userMapper=sqlSession.getMapper(UserMapper.class); List<User> userList=userMapper.getUserList(); for(User user:userList) { System.out.println(user.getId()); } //关闭SqlSession sqlSession.close(); } @Test public void getUserById() { //获得SqlSession对象 SqlSession sqlSession=MybatisUtils.getSqlSession(); //获得接口 UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user=mapper.getUserById(1); System.out.println(user.getName()); //关闭SqlSession sqlSession.close(); } //增删改需要提交事务 @Test public void addUser() { //获得SqlSession对象 SqlSession sqlSession=MybatisUtils.getSqlSession(); //获得接口 UserMapper mapper = sqlSession.getMapper(UserMapper.class); int flag=mapper.addUser(new User(3,"苗可卓3","3")); if(flag>0) { System.out.println("插入成功"); } //提交事务 sqlSession.commit(); //关闭SqlSession sqlSession.close(); } @Test public void updateUser() { //获得SqlSession对象 SqlSession sqlSession=MybatisUtils.getSqlSession(); //获得接口 UserMapper mapper = sqlSession.getMapper(UserMapper.class); int flag=mapper.updateUser(new User(1,"苗迪","miaodi")); if(flag>0) { System.out.println("修改成功"); } //提交事务 sqlSession.commit(); //关闭SqlSession sqlSession.close(); } @Test public void deleteUser() { //获得SqlSession对象 SqlSession sqlSession=MybatisUtils.getSqlSession(); //获得接口 UserMapper mapper = sqlSession.getMapper(UserMapper.class); int flag=mapper.deleteUser(1); if(flag>0) { System.out.println("删除成功"); } //提交事务 sqlSession.commit(); //关闭SqlSession sqlSession.close(); } }
注意事项,在Sql的CRUD操作中,增删改需要提交事务。