首页 > 其他 > 详细

MyBaits开发入门一

时间:2017-02-26 01:01:33      阅读:203      评论:0      收藏:0      [点我收藏+]

1. MyBaits基本概念:略

2. 案例:需求:建立数据库查询,一对一

3.开发步骤:

  • (1).建立数据库+表
  • (2).加包
  • (3).定义实体类
  • (4).编写db.properties,将数据库信息写入其中;编写MyBatis的配置文件conf.xml文件,引入db.properties;引入别名
  • (5).定义sql映射文件***Mapper.xml文件
  • (6).将***Mapper.xml文件注册到conf.xml文件中
  • (7).编写MyBaitsUtils工具
  • (8).测试

4.代码

  (1).加包

  技术分享

  (2). 建立数据库和表

CREATE TABLE teacher(
    t_id INT PRIMARY KEY AUTO_INCREMENT, 
    t_name VARCHAR(20)
);
CREATE TABLE class(
    c_id INT PRIMARY KEY AUTO_INCREMENT, 
    c_name VARCHAR(20), 
    teacher_id INT
);
ALTER TABLE class ADD CONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teacher(t_id);    

INSERT INTO teacher(t_name) VALUES(‘LS1‘);
INSERT INTO teacher(t_name) VALUES(‘LS2‘);

INSERT INTO class(c_name, teacher_id) VALUES(‘bj_a‘, 1);
INSERT INTO class(c_name, teacher_id) VALUES(‘bj_b‘, 2);

  (3). 定义实体类

技术分享
package com.atguigu.mybatis.domain;

/**
 * @author hp
 *
 */
public class Classes {

    private int id;
    private String name;
    
    private 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 Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    public Classes(int id, String name, Teacher teacher) {
        super();
        this.id = id;
        this.name = name;
        this.teacher = teacher;
    }

    public Classes() {
        
    }

    @Override
    public String toString() {
        return "Classes [id=" + id + ", name=" + name + ", teacher=" + teacher + "]";
    }
    
    
}
View Code
技术分享
package com.atguigu.mybatis.domain;

/**
 * @author hp
 *
 */
public class Teacher {

    private int id;
    private String name;
    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 Teacher(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    
    public Teacher() {
    
    }
    @Override
    public String toString() {
        return "Teacher [id=" + id + ", name=" + name + "]";
    }
    
    
    
}
View Code

   (4). 编写db.properties,将数据库信息写入其中;

username=root
password=lfdy
jdbcUrl=jdbc:mysql://localhost:3306/mybatis1
driverClass=com.mysql.jdbc.Driver

  编写MyBatis的配置文件conf.xml文件,引入db.properties;

 1   <!--  加载db.properties-->
 2     <properties resource="db.properties"></properties>
3 <!-- 配置连接数据库信息 --> 4 <environments default="development"> 5 <environment id="development"> 6 <transactionManager type="JDBC" /> 7 <dataSource type="POOLED"> 8 <property name="driver" value="${driverClass}"/> 9 <property name="url" value="${url}"/> 10 <property name="username" value="${username}"/> 11 <property name="password" value="${password}"/> 12 </dataSource> 13 </environment> 14 </environments>

  引入别名

<!-- 加入别名 -->
    <typeAliases>
        <package name="com.atguigu.mybatis.domain"/>
    </typeAliases>

  (5).定义sql映射文件***Mapper.xml文件

<?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"> 

<!-- 编写对应的CRUD -->
<mapper namespace="com.atguigu.mybatis.mapper.ClassMapper">
    
    <!-- 方式一: 嵌套结果,使用嵌套结果来处理重复的联合结果的子集 -->
    <select id="getClass" parameterType="int" resultMap="ClassResultMap">
        select * from class c,teacher t where c.teacher_id=t.t_id and c.c_id=#{id}
    </select>
    
    <resultMap type="Classes" id="ClassResultMap">
        <id property="id" column="c_id"/>
        <result property="name" column="c_name"/>
        <association property="teacher" javaType="Teacher">
            <id property="id" column="t_id"/>
            <result property="name" column="t_name"/>
        </association>
    </resultMap>

  

  

  <!-- 方式二:嵌套查询,使用另一个SQL语句来返回预期的复杂类型 -->
  <select id="getClass2" parameterType="int" resultMap="ClassResultMap2">
    select * from class where c_id = #{id}
  </select>

  <select id="getTeacher2" parameterType="int" resultType="Teacher">
    select t_id id,t_name name from teacher where t_id = #{id}
  </select>

  <resultMap type="Classes" id="ClassResultMap2">
  <id property="id" column="c_id"/>
  <result property="name" column="c_name"/>
      <association property="teacher" column="teacher_id" select="getTeacher2">
      </association>
  </resultMap>

</mapper>

  (6). 将***Mapper.xml文件注册到conf.xml文件中

<!-- 注册sql映射的mapper文件 -->
    <mappers>
        <mapper resource="com/atguigu/mybatis/mapper/ClassMapper.xml" />
    </mappers>

  (7). 编写MyBaitsUtils工具

package com.atguigu.mybatis.test;

import java.io.InputStream;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtils {
    
    public static SqlSessionFactory getFactory(){
        String resource = "conf.xml";
        
        InputStream is = MyBatisUtils.class.getClassLoader().getResourceAsStream(resource);
        
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        
    
        return factory;
    }

}

  (8).测试

package com.atguigu.mybatis.test;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.atguigu.mybatis.domain.Classes;

/**
 * @author hp
 *
 */
public class MyBatisTest {
    
    public static void main(String[] args) {
        
        SqlSessionFactory sessionFactory = MyBatisUtils.getFactory();
        
        SqlSession session = sessionFactory.openSession();
        
        String statement = "com.atguigu.mybatis.mapper.ClassMapper.getClass";
        
        Classes c = session.selectOne(statement, 1);
        System.out.println(c);
        
        session.close();
    
        
    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

      

MyBaits开发入门一

原文:http://www.cnblogs.com/lfdingye/p/6443375.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!