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 模糊查詢的資料請關註WalkonNet其它相關文章!