Mybatis CURD及模糊查询功能的实现
命名空间namespace:
配置文件中namespace中的名称为对应Mapper接口或者Dao接口的完整包名,必须一致!
1.查询(select)
select:
接口中的方法名与映射文件中的SQL语句ID 一一对应
- id
- parameterType
- resultType
案例:根据id查询用户
1.写接口(在UserMapper中添加对应的方法)
public interface UserMapper { //根据ID查询用户 User getuserByID(int id); }
2.UserMapper.xml配置文件的编写Sql语句
<select id="getuserByID" parameterType="int" resultType="com.xiong.pojo.User"> select * from mybatis.user where id = #{id}; </select>
3.测试
@Test public void getUserID(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.getuserByID(4); System.out.println(user); sqlSession.close(); }
注意:根据密码和名字直接查询用户
使用万能的Map方法
1.在接口方法中,直接传递Map;
User getuserByID2(Map<String,Object> map);
2.在编写sql语句的时候,需要传递参数的类型,参数类型是map
<select id="getuserByID2" resultType="com.xiong.pojo.User" parameterType="map"> select * from mybatis.user where id = #{userID} and name =#{username}; </select>
此处的userID,username可以不是数据库里面的字段,用map方法添加就可。
3.在使用方法的时候,Map的 key 为 sql中取的值即可,没有顺序要求!
@Test public void getUserID2(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); Map map= new HashMap<String, Object>(); map.put("userID",2); map.put("username","gou"); User user = mapper.getuserByID2(map); System.out.println(user); sqlSession.commit(); sqlSession.close(); }
总结:如果参数过多,我们可以考虑直接使用Map实现,如果参数比较少,直接传递参数即可
2.插入(insert)
insert
1.在UserMapper中添加对应的方法
int addUser(User user);
2.在UserMapper.xml中添加insert语句
<insert id="addUser" parameterType="com.xiong.pojo.User"> insert into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd}); </insert>
3.测试
@Test public void adduser(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int i = mapper.addUser(new User(4, "狗哥", "45412331")); if(i>0){ System.out.println("插入成功"); } //记得提交事物 sqlSession.commit(); sqlSession.close(); }
注意点:增、删、改操作需要提交事务!
3.更新(update)
1.编写接口方法
int updateUser(User user);
2.编写接口对应的UserMapper.xml文件中的SQL
<update id="updateUser" parameterType="com.xiong.pojo.User"> update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}; </update>
3.测试(记住一定要提交事物)
@Test public void updateuser(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); System.out.println(sqlSession); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int i = mapper.updateUser(new User(5, "hh", "55464545")); if(i>0){ System.out.println("更新成功"); } sqlSession.commit(); sqlSession.close(); }
4.删除(delete)
1.编写接口方法
int delete(int id);
2.编写对应配置文件的SQL
<delete id="delete" parameterType="int"> delete from mybatis.user where id=#{id}; </delete>
3.测试(记得要提交事物)
@Test public void deleteuser(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); System.out.println(sqlSession); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int i = mapper.delete(4); if(i>0){ System.out.println("更新成功"); } sqlSession.commit(); sqlSession.close(); }
总结:所有的增删改操作都需要提交事务!
5.模糊查询(like)
直接在sql语句中拼接通配符,会引起sql注入
<!-- 模糊查询--> <select id="getUserListByLike" resultType="com.xiong.pojo.User"> select * from mybatis.user where name like "%"#{value}"%"; </select>
测试:
@Test public void getUserListByLike(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> user = (List<User>) mapper.getUserListByLike("李"); for (User user1 : user) { System.out.println(user1); } sqlSession.commit(); sqlSession.close(); }
以上就是Mybatis CURD及模糊查询功能的实现的详细内容,更多关于Mybatis 模糊查询的资料请关注我们其它相关文章!
赞 (0)