MyBatis中Mapper代理方式
生活随笔
收集整理的這篇文章主要介紹了
MyBatis中Mapper代理方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Mapper 的動態代理作用
目前使用sqlsession進行增刪改查的缺點:
[1]沒有辦法實現多參數的傳遞
[2]書寫的時候沒有接口,后期的維護就比較的低
解決的方案:
Mapper的代理方式實現增刪改查
接口代碼
XML中文件
<!--namespace:必須是接口所在的全路徑--> <mapper namespace="com.bjsxt.mapper.FlowerMapper"><!--id的名稱和接口中方法必須保持一致--><select id="selectAll" resultType="flower">SELECT * from flower</select><insert id="insert">insert into flower values (DEFAULT ,#{name},#{price},#{production})</insert><select id="selectOne" resultType="flower">select * from flower where id = #{param1} and name = #{param2}</select><select id="selectOne2" resultType="flower">select * from flower where id = #{uu} and name = #{yy}</select><select id="selectOne3" resultType="flower">select * from flower where id = #{id} and name = #{name}</select><select id="selectOne4" resultType="flower">select * from flower where id = #{param1.id} and name = #{param2.name}</select> </mapper>測試代碼
FlowerMapper mapper = sqlSession.getMapper(FlowerMapper.class); //查詢操作 List<Flower> list = mapper.selectAll(); //添加操作 Flower f=new Flower(); f.setName("sxt"); f.setPrice(19); f.setProduction("bj"); int insert = mapper.insert(f); System.out.println(insert);總結
以上是生活随笔為你收集整理的MyBatis中Mapper代理方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Excel数据库求和技巧数据库怎么求和
- 下一篇: MyBatis中动态SQL