传入参数必须与配置文件中标明的参数一致
如
<select id="findByBookId" parameterType="java.lang.Integer" resultType="com.ichrono.rysmart.chronoline.bean.model.SportsBookFile">
select * from <include refid="tableName"/> t
where t.book_id=#{bookId}
</select>
传入String、Long等会报错
<select id="findByBookId" parameterType="java.lang.Long" resultType="com.ichrono.rysmart.chronoline.bean.model.SportsBookFile">
select * from <include refid="tableName"/> t
where t.book_id=#{bookId}
</select>
传入Integer 也会报错,不能自动转型
对于映射的model 类,如果数据库中的字段类型能够转型到model类的字段类型,那么就不会报错,否则则会报错
如数据库字段为 Int类型,则model类对应的字段可以为String 可以为Long 等。
如果数据库中字段为varchar 且存取的数据可以转化成int 或long等,则也不会报错,
否则会报错。
mybatis 插入和更新只返回行数,不能返回主键
只能通过以下方式:
<insert id="insert" parameterType="cn.softsea.model.DicCity" >
<selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER" >
SELECT LAST_INSERT_ID()
</selectKey>
insert into Dic_City (City_Code, Provinces_Code,
State_Code, City_Name, PY_Code,
PY_Code_Short, Ext1, Ext2,
Ext3, Ext4, Ext5)
values (#{cityCode,jdbcType=VARCHAR}, #{provincesCode,jdbcType=VARCHAR},
#{stateCode,jdbcType=VARCHAR}, #{cityName,jdbcType=VARCHAR}, #{pyCode,jdbcType=VARCHAR},
#{pyCodeShort,jdbcType=VARCHAR}, #{ext1,jdbcType=VARCHAR}, #{ext2,jdbcType=VARCHAR},
#{ext3,jdbcType=VARCHAR}, #{ext4,jdbcType=VARCHAR}, #{ext5,jdbcType=VARCHAR})
</insert>
调用Mapper返回主键:
//生成新对象用于插入
DicCity city = new DicCity();
city.setCityCode("330100");
city.setCityName("杭州市");
//获取mapper对象
DicCityMapper cityMapper = (DicCityMapper) SpringContextUtil.getBean("dicCityMapper");
int row = cityMapper.insert(city); //insrt不再返回主键,只返回响应行数,这点和ibatis不同了
System.out.println("响应的行数:"+row);
//取得自增的标识列 ID的值
System.out.println("新插入的数据的ID:"+city.getId());
另:再解释一下selectKey 的 order="BEFORE"时,表示 selectKey 中的语句先执行,接着再执行insert语句,这用于oracle的sequence方式获取主键。
mysql 中用after id自动生成后 获得id ,返回回去。
数据库中字段 映射到map字段,取出时类型必须一致,否则报错
mysql数据库 日期可以传String
mysql 中表字段 限定了小数位数 ,即使存储的是整数,则查询的model里相应字段 仍然带有小数
比如 数据库字段 Decimal 长度 5 小数点 2
存储 70 则model里映射的是70.00
如
<select id="findByBookId" parameterType="java.lang.Integer" resultType="com.ichrono.rysmart.chronoline.bean.model.SportsBookFile">
select * from <include refid="tableName"/> t
where t.book_id=#{bookId}
</select>
传入String、Long等会报错
<select id="findByBookId" parameterType="java.lang.Long" resultType="com.ichrono.rysmart.chronoline.bean.model.SportsBookFile">
select * from <include refid="tableName"/> t
where t.book_id=#{bookId}
</select>
传入Integer 也会报错,不能自动转型
对于映射的model 类,如果数据库中的字段类型能够转型到model类的字段类型,那么就不会报错,否则则会报错
如数据库字段为 Int类型,则model类对应的字段可以为String 可以为Long 等。
如果数据库中字段为varchar 且存取的数据可以转化成int 或long等,则也不会报错,
否则会报错。
mybatis 插入和更新只返回行数,不能返回主键
只能通过以下方式:
<insert id="insert" parameterType="cn.softsea.model.DicCity" >
<selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER" >
SELECT LAST_INSERT_ID()
</selectKey>
insert into Dic_City (City_Code, Provinces_Code,
State_Code, City_Name, PY_Code,
PY_Code_Short, Ext1, Ext2,
Ext3, Ext4, Ext5)
values (#{cityCode,jdbcType=VARCHAR}, #{provincesCode,jdbcType=VARCHAR},
#{stateCode,jdbcType=VARCHAR}, #{cityName,jdbcType=VARCHAR}, #{pyCode,jdbcType=VARCHAR},
#{pyCodeShort,jdbcType=VARCHAR}, #{ext1,jdbcType=VARCHAR}, #{ext2,jdbcType=VARCHAR},
#{ext3,jdbcType=VARCHAR}, #{ext4,jdbcType=VARCHAR}, #{ext5,jdbcType=VARCHAR})
</insert>
调用Mapper返回主键:
//生成新对象用于插入
DicCity city = new DicCity();
city.setCityCode("330100");
city.setCityName("杭州市");
//获取mapper对象
DicCityMapper cityMapper = (DicCityMapper) SpringContextUtil.getBean("dicCityMapper");
int row = cityMapper.insert(city); //insrt不再返回主键,只返回响应行数,这点和ibatis不同了
System.out.println("响应的行数:"+row);
//取得自增的标识列 ID的值
System.out.println("新插入的数据的ID:"+city.getId());
另:再解释一下selectKey 的 order="BEFORE"时,表示 selectKey 中的语句先执行,接着再执行insert语句,这用于oracle的sequence方式获取主键。
mysql 中用after id自动生成后 获得id ,返回回去。
数据库中字段 映射到map字段,取出时类型必须一致,否则报错
mysql数据库 日期可以传String
mysql 中表字段 限定了小数位数 ,即使存储的是整数,则查询的model里相应字段 仍然带有小数
比如 数据库字段 Decimal 长度 5 小数点 2
存储 70 则model里映射的是70.00