`
ka520888
  • 浏览: 5635 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

IBatis的一些配置问题

阅读更多

Ibatis
注意在项目中配置文件如果是分模块写的,在完实体的XML文件后,记得把该文件放到ibatis总配置中
在写IBATIS SQL语句的时候,把类作为参数的时候,在SQL语句中用到类的参数属性必须与实体类中的一致,
不然会说找不到该属性…

用like 模糊查询时用  '$变量名$%' 这样写!


在ibatis中用联合查询的时候,查询的两张表互引用的列
名字要一样
A表的sid 主键
那么B表在引用的时候也得用sid作列名

在写联合查询的时候.在resultMap中写了的属性
在写SELECT语句的时候.全部都要查询出来,不然可能会暴
某某列找不到的错误......我郁闷了黑久..终于搞定.操


<action.xml中配置>
<insert id="insertProduct" parameterMap="pmap_product">
                insert into product
                (pid,pno,pname,pinfo,sfid,ssid,stid,pnormal,pprice,pimage,ppid,pkuchun,pcommend,phot,pstate,pnumber,zstate,ptime,freeshipping)
                values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
//返回当前插入数据的pid   和列名要对应
                <selectKey keyProperty="pid" resultClass="Integer">
                        select @@identity as pid
                </selectKey>
        </insert>

实现类中返回
public int insertProduct(Product product) throws Exception {
                return (Integer) super.getSqlMapClientTemplate().insert(
                                "insertProduct", product);
        }


动态语句不支持别名

<parameterMap class="_product" id="pmap_product">
用这个标签来标识类的属性的时候,注意插入语句上的列的顺序
要和标签里的属性位置匹配,不然会发生数据插入错误的问题
用#列名# 这种方法就不会出现上面的问题


//resultMap的应用,把想要查询的列全部放在RESULTMAP中
然后在SELECT中写入相关连的语句即可 接口方法返回MAP对象  column列名一定要与数据库的列名匹配
在SQL语句的写的列名必须与数据库列名匹配
如果在数据库中没有的列名.是自己AS 取的别名.那么在RESULTMAP中的列名要与SQL语句中写的列名匹配
<resultMap id="rmap_dishreview" class="java.util.HashMap">

                <result column="reviewid" property="reviewid"
                        jdbcType="INTEGER" />
                <result column="reviewcontent" property="reviewcontent"
                        jdbcType="VARCHAR" />
                <result column="reviewtime" property="reviewtime"
                        jdbcType="VARCHAR" />
                <result column="userid" property="userid" jdbcType="INTEGER" />
                <result column="dishid" property="dishid" jdbcType="INTEGER" />
                <result column="nickname" property="nickname" jdbcType="VARCHAR"/>       
        </resultMap>

        <select id="selectDishReviewById" parameterClass="java.util.Map"
                resultMap="rmap_dishreview">
                select * from dishreview as d,user as u where d.userid=u.userid and d.userid=#userid#
                and reviewid=#reviewid#
        </select>

 

 

 

请大家不要犯我相同的错误!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics