打赏

mybatis_SQL映射(1)

文章摘录自:http://blog.csdn.net/y172158950/article/details/17258377

1. select的映射

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <select id="selectPerson" parameterType="int" resultType="hashmap">  
  2.        select id, name, sex, updateTime from person where id =#{id};  
  3. </select>  
a) #{id}:创建预编译语句参数,占位符
b) parameterType:参数的类型,int类型
c)resultType:返回的结果集,封装为hashMap
[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public Map<String, Object> selectPersonById(int id) {  
  2.     SqlSession session = DbFactory.getInstance().openSession();  //单例SqlSessionFactory工厂  
  3.     Map<String, Object> m = (Map<String, Object>) session.selectOne("com.yjq.entity.Person.selectPerson", id);  
  4.     session.commit();  
  5.     session.close();  
  6.     return m;  
  7. }  
[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. //output  
  2. {id=4, sex=f, NAME=zql, updateTime=2013-12-10 14:18:11.0, SEX=f, name=zql, ID=4, UPDATETIME=2013-12-10 14:18:11.0}  
d) 结果集为啥有大小写的key2份?神奇的mybatis啊。

2. insert,update,delete的映射

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <insert id="insertPerson" parameterType="com.yjq.entity.Person" useGeneratedKeys="true" keyProperty="id"> <!-- 将自增主键生成的值回写到对象  -->  
  2.        insert into person(name, sex, updateTime) values(#{name}, #{sex}, #{updateTime});  
  3. </insert>  
  4. <delete id="deletePerson" parameterType="int">  
  5.        delete from person where id=#{id};  
  6. </delete>  
  7. <update id="updatePerson" parameterType="com.yjq.entity.Person">  
  8.        update person set name=#{name}, sex=#{sex}, updateTime=#{updateTime} where id=#{id};  
  9. </update>  
a) parameterType="com.yjq.entity.Person":对象参数,#{name}表示Person对象的name属性。
b) useGeneratedKeys="true" keyProperty="id":只存在于insert命令,写这2个参数会将主键自增长的值回写到传入的参数对象。
[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public Person insertPerson(Person p) {  
  2.     SqlSession session = DbFactory.getInstance().openSession();  
  3.     session.insert("com.yjq.entity.Person.insertPerson", p);  
  4.     session.commit();  
  5.     session.close();  
  6.     return p;  
  7. }  
[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. PersonDao dao = new PersonDao();  
  2. Person p = new Person();  
  3. p.setName("zql");  
  4. p.setSex("f");  
  5. p.setUpdateTime(new Date());  
  6. System.out.println("------" + p.getId());  
  7. dao.insertPerson(p);  
  8. System.out.println(p.getId());  
[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. //output(insert前id未赋值,insert后id=7)  
  2. ------0  
  3. 7  
c) 返回值:insert,update,delete方法均返回int参数,表示操作了多少条数据
[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. int result = session.update("com.yjq.entity.Person.updatePerson", p);  
d) 假设数据库不支持自增id,mybatis可以生成随机主键
[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <insert id="insertPerson2" parameterType="com.yjq.entity.Person">  
  2.     <selectKey keyProperty="id" resultType="int" order="BEFORE">  <!-- 生成随机id数 -->  
  3.         select CONVERT(RAND()*1000000,SIGNED) a from dual;  
  4.     </selectKey>  
  5.     insert into person(id, name, sex, updateTime) values(#{id}, #{name}, #{sex}, #{updateTime});  
  6. </insert>  
posted @ 2016-07-27 11:46  海米傻傻  阅读(210)  评论(0编辑  收藏  举报