从SqlSessionFactory中获取SqlSession
SqlSession调用Mapper
public interface BlogMapper {
@Select("SELECT * FROM blog WHERE id = #{id}")
Blog selectBlog(int id);
}
BlogMapper mapper = session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(101);
resultType直接映射到自定义类,resultMap是哈希表,手动映射
<select id="selectUsers" resultType="User">
select id, username, password
from users
where id = #{id}
</select>
<select id="selectUsers" resultMap="userResultMap">
select user_id, user_name, hashed_password
from some_table
where id = #{id}
</select>
<resultMap id="userResultMap" type="User">
<id property="id" column="user_id" />
<result property="username" column="user_name"/>
<result property="password" column="hashed_password"/>
</resultMap>
动态SQL的if, choose, when, otherwise, trim, where, set, foreach, bind语句
java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
BlogMapper m
自动生成文件有4个包括:
Student.java: table对应的java object
StudentMapper.xml用于mybatis生成java的指导文件
StudentMapper.java: 对表格增删减除的操作
StudentExample.java: 用于产生对每个column的大于等于小于条件判断
updateByExample, updateByEampleSelective区别
Example的意思是where条件,所以updateByExample(User, UserExample)是按where条件更新Object的全部字段,若为空,直接更新。
updateByEampleSelective(User, UserExample)是按where条件更新Object的某些字段(不为null),所以null的话,myBatis就不会更新。所以如果选用Selective的API,就不能将该字段更新为null,因为null是一个保留字来决定是否更新该字段。
updateByPrimaryKey(User)按主键更新,主键设在User中,这里就不用指定where条件。updateByExampleSelective按主键更新非null字段。
MyBatis简介
MyBatis Generator
updateByExample介绍
在c语言上编程样本
ReplyDelete读取一个Xml文件的java样本