create增加,read查找,update更新,delete删除
1.常规的增删改查
UserMapper.java
public interface UserMapper { //查询全部用户 List<User> findAll(); //根据ID查找 User getUserId(int id); //添加用户 int addUser(User user); //更新用户 int updateUser(User user); //删除用户 int deleteUser(int id); }
userMapper.xml
<mapper namespace="mapper.UserMapper"> <!--配置查询所有--> <!--id对应的是dao层的方法名--> <select id="findAll" resultType="domain.User"> select * from user; </select> <!--根据id的查询--> <select id="getUserId" resultType="domain.User" parameterType="int"> select * from mybatis_study.user where id= #{id}; </select> <!--添加操作--> <insert id="addUser" parameterType="domain.User"> insert into user (id,username,sex,address) values (#{id},#{username},#{sex},#{address}); </insert> <!--更新操作--> <update id="updateUser" parameterType="domain.User"> update user set username=#{username},sex=#{sex},address=#{address} where id = #{id}; </update> <!--删除操作--> <delete id="deleteUser" parameterType="int"> delete from user where id = #{id} </delete> </mapper>
测试类
//查询所有 @Test public void TestList(){ SqlSession session = MybatisUtil.getSqlSession(); UserMapper userMapper = session.getMapper(UserMapper.class); List<User> list = userMapper.findAll(); for (User user : list) { System.out.println(user); } //释放资源 session.close(); } //根据ID查询 @Test public void getUserId(){ SqlSession session = MybatisUtil.getSqlSession(); UserMapper mapper = session.getMapper(UserMapper.class); User userId = mapper.getUserId(41); System.out.println(userId); session.close(); } //添加操作,增删改都需要提交事务 @Test public void addUser(){ SqlSession session = MybatisUtil.getSqlSession(); UserMapper mapper = session.getMapper(UserMapper.class); mapper.addUser(new User(3, "小哈", "男", "新乡市")); //提交事务 session.commit(); session.close(); } //更新操作 @Test public void updateUser(){ SqlSession session = MybatisUtil.getSqlSession(); UserMapper mapper = session.getMapper(UserMapper.class); mapper.updateUser(new User(3,"小何","女","河南省")); //提交事务 session.commit(); session.close(); } //删除操作 @Test public void deleteUser(){ SqlSession session = MybatisUtil.getSqlSession(); UserMapper mapper = session.getMapper(UserMapper.class); mapper.deleteUser(3); session.commit(); session.close(); }
当表中字段过多,或者多少数字段进行操作时,可以使用map,在开发当中经常用
UserMapper.java
//用map来添加操作 int addUserMap(Map<String,Object> map);
UserMapper.xml
<!--对象中的属性可以直接取出来,传递map的key--> <insert id="addUserMap" parameterType="Map"> insert into user (id,username) values (#{uid},#{userName}) </insert>
Test
//使用map来操作 @Test public void addUserMap(){ SqlSession session = MybatisUtil.getSqlSession(); UserMapper mapper = session.getMapper(UserMapper.class); Map<String,Object> map = new HashMap<String,Object>(); //key值对应的sql语句中传入的值 map.put("uid",3); map.put("userName","小张"); mapper.addUserMap(map); session.commit(); session.close(); }
模糊查询
//模糊查询 List<User> getUserLike(String username);
<!--模糊查询--> <select id="getUserLike" resultType="domain.User"> select * from user where username like #{username} </select>
//模糊查询 @Test public void getUserLike(){ SqlSession session = MybatisUtil.getSqlSession(); UserMapper mapper = session.getMapper(UserMapper.class); List<User> userLike = mapper.getUserLike("%张%"); for (User user : userLike) { System.out.println(user); } session.commit(); session.close(); }