MyBatisMyBatis的resultMap

对应StudentDAO.xml中不奇怪配法的结果:

MyBatis 1MyBatis 2

<?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">
<mapper namespace="cn.zhang.dao">

    <!-- 结果映射,指定了数据库和实体类中的对应值 -->
    <resultMap type="Student" id="findstudent">
        <result property="name" column="stuname" />
    </resultMap>

    <!-- 查询所有 -->
    <select id="findAll" resultMap="findstudent">
        select * from student
    </select>

</mapper>

 

6.StudentDAO.xml (对应的照耀文件)

1.我们学习MyBatis时,恐怕会碰到实体类属性跟数据库字段分裂的动静

8..MyTest.java (测试类)

View Code

是还是不是相当粗略,那里只是记录一下。。

1.Student.java (实体类)

如:数据库    ——  实体类

MyBatis 3MyBatis 4

MyBatis 5MyBatis 6

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=debug, stdout

MyBatis 7MyBatis 8

用resultMap的配法:

源码介绍与对待:

MyBatis 9

View Code

package cn.zhang.test;

import java.io.IOException;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import cn.zhang.dao.StudentDao;
import cn.zhang.dao.impl.StudentDaoImpl;
import cn.zhang.entity.Student;

public class MyTest {

    StudentDao dao;
    @Before
    public void initData() throws IOException{
        dao=new StudentDaoImpl();
    }

    /**
     * 查询所有学生
     * @throws IOException
     */
    @Test
    public void findAll() throws IOException{
        List<Student> list = dao.findAll();
        for (Student student : list) {
            System.out.println("编号: "+student.getStuno()+"姓名:"+student.getName());
        }

    }


}

7.log4j.properties

package cn.zhang.util;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * 获得session的工具类
 * 
 */
public class MybatisUtil {

    private static String config = "mybatis-config.xml";
    static Reader reader;
    static {
        try {
            reader = Resources.getResourceAsReader(config);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static SqlSessionFactory factory = new SqlSessionFactoryBuilder()
            .build(reader);

    // 提供一个可以获取到session的方法
    public static SqlSession getSession() throws IOException {

        SqlSession session = factory.openSession();
        return session;
    }
}

最健康的配法:

View Code

4.StudentDao.java (定义方法的接口)

MyBatis 10MyBatis 11

这么就能够成功的获得值了,那正是resultMap的效果。

即: 数据库中的stuname字段对应的实际情况体类里的name属性

MyBatis 12

2.mybatis-config.xml (MyBatis的布置文件)

对应StudentDAO.xml中resultMap配法的结果:

    stuname  —->  name

MyBatis 13MyBatis 14

3.MybatisUtil.java (得到session的工具类)

View Code

MyBatis 15MyBatis 16

package cn.zhang.dao;

import java.io.IOException;
import java.util.List;

import cn.zhang.entity.Student;

public interface StudentDao {


    /**
     * 查询所有记录
     * @return
     * @throws IOException
     */
    public List<Student> findAll() throws IOException;


}

View Code

MyBatis 17

5.StudentDaoImpl.java (达成接口方法的兑现类)

View Code

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 配置别名 -->
    <typeAliases>
        <!--方式一: 按类型名定制别名 -->
        <typeAlias type="cn.zhang.entity.Student" alias="Student" />
        <!--方式二: 拿当前指定包下的简单类名作为别名 -->
        <!-- <package name="cn.zhang.entity"/> -->
    </typeAliases>
    <environments default="mysql">
        <environment id="mysql">
            <!-- 使用jdbc的事务 -->
            <transactionManager type="JDBC" />
            <!-- 使用自带的连接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/y2161" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="cn/zhang/dao/StudentDAO.xml" />
    </mappers>
</configuration>
package cn.zhang.dao.impl;

import java.io.IOException;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import cn.zhang.dao.StudentDao;
import cn.zhang.entity.Student;
import cn.zhang.util.MybatisUtil;

public class StudentDaoImpl implements StudentDao {
    SqlSession session;

    public StudentDaoImpl() throws IOException {
        session = MybatisUtil.getSession();
    }

    /**
     * 查询所有
     */
    public java.util.List<Student> findAll() throws IOException {
        List<Student> list = session.selectList("findAll");
        session.close();
        return list;
    }

}

 

package cn.zhang.entity;

import java.util.Date;

/**
 * 学生实体类
 * 
 */
public class Student {

    private Integer stuno;
    private String name;
    private Integer stuage;
    private Date studate;


    @Override
    public String toString() {
        return "Student [stuno=" + stuno + ", name=" + name + ", stuage="
                + stuage + ", studate=" + studate + "]";
    }

    public Integer getStuno() {
        return stuno;
    }

    public void setStuno(Integer stuno) {
        this.stuno = stuno;
    }



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getStuage() {
        return stuage;
    }

    public void setStuage(Integer stuage) {
        this.stuage = stuage;
    }

    public Date getStudate() {
        return studate;
    }

    public void setStudate(Date studate) {
        this.studate = studate;
    }

}

View Code

<?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">
<mapper namespace="cn.zhang.dao">

    <!-- 查询所有 -->
    <select id="findAll" resultType="Student">
        select * from student
    </select>

</mapper>

那会儿,大家得以接纳我们的resultMap来消除这一题材。。。

若果此刻,大家要用常规的查询艺术时是无法正确查询到stuname的值的,它会显得为null

网站地图xml地图