MyBatis处理多个参数问题
参数问题
目前将多个参数封装到一个javabean对象,然后使用该对象传递
a.传入多个参数时,不用再mapper.xml编写parameterType
b.命名参数
src\org\myy\mapper\studentMapper.xml
a.
<insert id="addStudentmore"> insert into student1(stuno,stuname,stuage,graname) values(#{param1},#{param2},#{param3},#{param4}) </insert>
b.命名参数
<insert id="addStudentmore"> insert into student1(stuno,stuname,stuage,graname) values(#{stuNo},#{stuName},#{stuAge},#{graName}) </insert>
c.综合使用
<insert id="addStudentmore2"> insert into student1(stuno,stuname,stuage,graname) values(#{stuNo},#{student.stuName},#{student.stuAge},#{student.graName}) </insert>
src\org\myy\mapper\StudentMapper.java
a.
int addStudentmore(Integer stuNo,String stuName, Integer stuAge, String graName);
b.
int addStudentmore(@Param("stuNo") Integer stuNo,@Param("stuName") String stuName, @Param("stuAge")Integer stuAge, @Param("graName")String graName);
c.
int addStudentmore2(@Param("stuNo") int stuno,@Param("student")Student student);
src\org\myy\test\Test.java
ab.
//Connection - SqlSession操作Mybatis //conf.xml->reader Reader reader = Resources.getResourceAsReader("conf.xml"); //reader->sqlSession //可以通过build的第二参数 指定数据库环境 SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader,"devOracle"); SqlSession session = sessionFactory.openSession(true);//设置自动提交 StudentMapper studentMapper=session.getMapper(StudentMapper.class); studentMapper.addStudentmore(555,"myy",100,"myy"); session.close();
c.
//Connection - SqlSession操作Mybatis //conf.xml->reader Reader reader = Resources.getResourceAsReader("conf.xml"); //reader->sqlSession //可以通过build的第二参数 指定数据库环境 SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader,"devOracle"); SqlSession session = sessionFactory.openSession(true);//设置自动提交 StudentMapper studentMapper=session.getMapper(StudentMapper.class); Student student=new Student(null,"myy",100,"myy"); studentMapper.addStudentmore2(333,student); session.close();
be simple, be happy.