The sql for operating the database is defined in the Mapper.xml mapping file, each sql is a statement, and the mapping file is the core of mybatis.
Input parameter mapping
parameterType (input type)
Pass simple types:
Such as:<select id="queryUserById" parameterType="Integer" resultType="User"> select * from t_user where id = #{id} </select>As shown in the above, parameterType is the input type, and resultType is the output type. Remember to use the placeholder #{} (anti-sql injection function and '`' floating character by default) or ${} (the opposite) in the sql statement for sql splicing.
Pass the pojo object
Mybatis uses the ognl expression to parse the value of the object field. The value in the #{} or ${} brackets is the pojo attribute name.
Supplement: What is ognl expression? OGNL expression is the abbreviation of Object-Graph Navigation Language, which is a powerful expression language. Through simple and consistent expression syntax, you can access any properties of objects and call methods of objects. , traverse the structure diagram of the entire object, and implement field type conversion.
Use of ognl expressions?
First look at a code:
In User.xml
<update id="updateUser" parameterType="com.firtDay.sqlMapconFig.User"> UPDATE t_user set _name = #{_name},_money = #{_money} WHERE id = #{id} </update>
User.java
public Integer id; private String _name; private int _money;
test class
//update data @Test public void testDemo5(){ SqlSession openSession = sqlSessionFactory.openSession(); User user = new User(); user.setId(1); user.set_name("魏杰"); user.set_money(1000); int update = openSession.update("updateUser", user); openSession.commit(); openSession.close(); }database
UPDATE t_user set _name = #{_name},_money = #{_money} WHERE id = #{id}
### Error updating database. Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'money' in 'class com.firtDay.sqlMapconFig.User'
means the parameter value was found.
Pass the pojo wrapper object
In development, you can use pojo to pass query conditions.
The query conditions may be comprehensive query conditions, including not only user query conditions but also other query conditions (for example, when querying user information, the user's purchased product information is also used as query conditions). In this case, the packaging object can be used to pass input parameters.
Wrapper Object: A property in the Pojo class is another pojo.
Requirement: Fuzzy query user information based on user name, and put the query condition in the user attribute of QueryVo.
Mapper interface
public interface mapper { User queryUserById(Integer id); User queryUserByIdQueryVo(QueryVo vo); Integer queryUserCount(); User queryUserByName(User user); List<User> queryUserByIds(QueryVo vo); List<User> queryUserByIds(List<Integer> ids); List<User> queryUserByIds(Integer[] ids); List<OrderDemo> queryOrders();//One to one List<UserDemo> queryOrdersDemo();//One-to-many }
Write QueryVo
public class QueryVo implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private User user; private List<Integer> ids; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public List<Integer> getIds() { return ids; } public void setIds(List<Integer> ids) { this.ids = ids; } }
Pass parameter type settings in xml
<select id="queryUserByIds" resultType="User" parameterType="QueryVo">
test class
@Test public void testDemo2(){ SqlSession openSession = sqlSessionFactory.openSession(); mapper m = openSession.getMapper(mapper.class); User user = new User(); user.setId(3); // Knowledge point one QueryVo vo = new QueryVo(); vo.setUser(user); user = m.queryUserByIdQueryVo(vo); System.out.println(user); System.out.println("----------------"); user = m.queryUserById(user.getId()); System.out.println(user); System.out.println("---------------------------"); Integer queryUserCount = m.queryUserCount(); System.out.println(queryUserCount); openSession.close(); }