Mapper 的动态代理作用
目前使用sqlsession进行增删改查的缺点:
[1]没有办法实现多参数的传递
[2]书写的时候没有接口,后期的维护就比较的低
解决的方案:
Mapper的代理方式实现增删改查
接口代码
public interface FlowerMapper {
//查询所有操作
List<Flower> selectAll();
//修改操作
int insert(Flower flower);
}
//查询
Flower selectOne(int id , String name);
//Flower selectOne2( @Param(""uu) int id , @Param(""yy) String name);//起别名
Flower selectOne3(Flower flower);
Flower selectOne4(Flower f1 , Flower f2);
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);