标签:mybatis basic faq mapper parameter
Map<String, Object> parms = new HashMap<String, Object>(); parms.put("table", "foo"); // 表名 parms.put("criteria", 37); // 查询过滤条件 List<Object> rows = mapper.generalSelect(parms);
<select id="generalSelect" parameterType="map"> select * from ${table} where col1 = #{criteria} </select>
select * from foo where col1 = ?
有两种使用LIKE的方法。(推荐使用)第一种方法是,在Java代码中添加SQL通配符。
示例一:String wildcardName = "%Smi%"; List<Name> names = mapper.selectLike(wildcardName);
<select id="selectLike"> select * from foo where bar like #{value} </select>
String wildcardName = "Smi"; List<Name> names = mapper.selectLike(wildcardName);
<select id="selectLike"> select * from foo where bar like ‘%‘ || ‘${value}‘ || ‘%‘ </select>
<insert id="insertName"> insert into names (name) values (#{value}) </insert>
List<String> names = new ArrayList<String>(); names.add("Fred"); names.add("Barney"); names.add("Betty"); names.add("Wilma"); // 注意这里 ExecutorType.BATCH SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); try { NameMapper mapper = sqlSession.getMapper(NameMapper.class); for (String name : names) { mapper.insertName(name); } sqlSession.commit(); } finally { sqlSession.close(); }
<insert id="insertName" useGeneratedKeys="true" keyProperty="id"> insert into names (name) values (#{name}) </insert>
Name name = new Name(); name.setName("Fred"); int rows = mapper.insertName(name); // 完成后,id已经被设置到对象中 System.out.println("rows inserted = " + rows); System.out.println("generated key value = " + name.getId());
import org.apache.ibatis.annotations.Param; public interface UserMapper { User selectUser(@Param("username") String username, @Param("hashedPassword") String hashedPassword); }
<select id=”selectUser” resultType=”User”> select id, username, hashedPassword from some_table where username = #{username} and hashedPassword = #{hashedPassword} </select>
标签:mybatis basic faq mapper parameter
原文地址:http://blog.csdn.net/renfufei/article/details/39649707