码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis详解(二)

时间:2019-08-30 23:16:10      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:int   str   结合   result   nts   直接   图片   名称空间   names   

使用接口结合xml文件

创建一个接口,该接口要和映射文件匹配

public interface UserDao {
  //根据ID查询 User getUserId(
int id); }

在映射文件中

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:表示名称空间。这里的namespace一定要和接口所在的包以及接口的名字一样 -->
<mapper namespace="com.zhiyou100.zjc.dao.UserDao">
    <!-- 根据id查询用户。id:标识该标签。
             parameterType:参数类型。可以写 也可以省略
             resultType:返回结果的类型。        
            #{id}:类似于EL表达式。 解析id的值
     -->
  <!--getUserId对应着上面接口的方法名-->
<select id="getUserId" parameterType="int" resultType="com.zhiyou100.zjc.bean.User"> select * from users where id=#{id} </select> </mapper>

测试

class Test {
    static SqlSession session=null;
    UserDao userdao =  session.getMapper(UserDao.class); //相当于你创建了一个dao对象
    @BeforeAll
    static void setUpBeforeClass() throws Exception {
        Reader reader = Resources.getResourceAsReader("conf.xml");
        SqlSessionFactory sessionFactory  =new SqlSessionFactoryBuilder().build(reader);
        session  =sessionFactory.openSession();
    }    
    @AfterAll
    static void tearDownAfterClass() throws Exception {
        session.commit();
    }    
    @org.junit.jupiter.api.Test
    void getUserIdTest() {
    User user=    userdao.getUserId(2);
    System.out.println(user);  
    }
}

解决字段与属性不匹配

1、解决方案为为查询的字段起别名 要求别名与属性名一致。(不建议使用)

2、 使用resultMap标签来定义实体类与字段之间的对应关系。

    <select id="getOrderId" resultMap="orderId">
        select * from orders where order_id=#{id}
    </select>
    <resultMap type="com.zhiyou100.zjc.bean.Order" id="orderId">
        <id column="order_id" property="id"/>
        <result column="order_no" property="no"/>
        <result column="order_price" property="price"/> 
    </resultMap>

链表查询

1、 通过链表查询来得到。

package com.zhiyou100.zjc.bean;

import java.util.List;

public class Classmate {
    private int id;
    private String name;
    private int teacherid;
    private Teacher teacher;//关联老师的对象public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getTeacherid() {
        return teacherid;
    }
    public void setTeacherid(int teacherid) {
        this.teacherid = teacherid;
    }
    
}

映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:表示名称空间。现在的目的是区分id的. -->
<mapper namespace="com.zhiyou100.zjc.dao.ClassmateDao">
    <select id="getClassmateInfo" resultMap="classId">
        select * from class c,teacher t,student s  where t.t_id=c.teacher_id and c.c_id=s.class_id and c_id=#{id}
    </select>
    <resultMap type="com.zhiyou100.zjc.bean.Classmate" id="classId">
        <id column="c_id" property="id"/>
        <result column="c_name" property="name"/>
        <result column="teacher_id" property="teacherid"/> 
      <!-- teacher为老师老师的对象名 --> <association property="teacher" javaType="com.zhiyou100.zjc.bean.Teacher"> <id column="t_id" property="id"/> <result column="t_name" property="name"/> </association> </resultMap> </mapper>

 

2、两次查询

技术图片

一对多查询

package com.zhiyou100.zjc.bean;

import java.util.List;

public class Classmate {
    private int id;
    private String name;
    private int teacherid;
    private Teacher teacher;//一对多
    private List<Student> students;
    
    
    public List<Student> getStudents() {
        return students;
    }
    public void setStudents(List<Student> students) {
        this.students = students;
    }
    public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getTeacherid() {
        return teacherid;
    }
    public void setTeacherid(int teacherid) {
        this.teacherid = teacherid;
    }    
    
}

 

映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:表示名称空间。现在的目的是区分id的. -->
<mapper namespace="com.zhiyou100.zjc.dao.ClassmateDao">
    <select id="getClassmateInfo" resultMap="classId">
        select * from class c,teacher t,student s  where t.t_id=c.teacher_id and c.c_id=s.class_id and c_id=#{id}
    </select>
    <resultMap type="com.zhiyou100.zjc.bean.Classmate" id="classId">
        <id column="c_id" property="id"/>
        <result column="c_name" property="name"/>
        <result column="teacher_id" property="teacherid"/> 
        <association property="teacher" javaType="com.zhiyou100.zjc.bean.Teacher">
            <id column="t_id" property="id"/>
            <result column="t_name" property="name"/>        
        </association>
    <!--student为List<Student>集合的对象名--> <collection property="students" ofType="com.zhiyou100.zjc.bean.Student"> <id column="s_id" property="id"/> <result column="s_name" property="name"/> <result column="class_id" property="classid"/> </collection> </resultMap> </mapper>

 

Mybatis中$和#的区别

#相当于对数据 加上 双引号,$相当于直接显示数据

1、#将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。如:order by #user_id#,

  如果传入的值是111,那么解析成sql时的值为order by “111”, 如果传入的值是id,则解析成的sql为order by “id”. 

2、将传入的数据直接显示生成在sql中。如:orderby将传入的数据直接显示生成在sql中。如:orderbyuser_id$,

   如果传入的值是111,那么解析成sql时的值为order by user_id, 如果传入的值是id,则解析成的sql为order by id. 

3、#方式能够很大程度防止sql注入。  

4、方式无法防止Sql注入。

5、方式一般用于传入数据库对象,例如传入表名.  

6、一般能用#的就别用$.

MyBatis排序时使用order by 动态参数时需要注意,用$而不是#

添加对象时如何把生产的id返回

技术图片

Mybatis详解(二)

标签:int   str   结合   result   nts   直接   图片   名称空间   names   

原文地址:https://www.cnblogs.com/zjc364259451/p/11437203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!