环境搭建

数据库schema

1)datasource.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- SqlSeesionTemplate是线程安全的,可以被多个Dao共享,可以使用Singleton -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 第一个参数是 sqlSessionFactory -->
<constructor-arg index="0" ref="sqlSessionFactory"/>
<!-- 第二个参数是 ExecutorType -->
<constructor-arg index="1" value="BATCH"/>
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 指定MyBatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 导入Mapper -->
<property name="mapperLocations" value="classpath:mappers/*.xml" />
</bean> <!-- datasource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean> </beans>

2.applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="dao" />
<context:component-scan base-package="bean" /> <bean id="userDao" class="dao.UserDao">
<!--<property name="sqlSessionFactory" ref="sqlSessionFactory" />-->
<property name="sqlSessionTemplate" ref="sqlSession" />
</bean> <bean id="articleDao" class="dao.ArticleDao">
<property name="sqlSessionTemplate" ref="sqlSession" />
</bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean> </beans>

3. log4j.properties

将ibatis log4j运行级别调到DEBUG可以在控制台打印出ibatis运行的sql语句,方便调试: 

### 设置Logger输出级别和输出目的地 ###
log4j.rootLogger=debug,stdout,logfile ### 把日志信息输出到控制台 ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout ### 把日志信息输出到文件:jbit.log ###
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=jbit.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n ###显示SQL语句部分
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

4.mybatis-config.xml

<?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>
<!-- MyBatis 配置 -->
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="false"/>
<setting name="autoMappingBehavior" value="PARTIAL"/>
<setting name="defaultExecutorType" value="BATCH"/>
<setting name="defaultStatementTimeout" value="25"/>
<setting name="safeRowBoundsEnabled" value="false"/>
<setting name="mapUnderscoreToCamelCase" value="false"/>
<setting name="localCacheScope" value="SESSION"/>
<setting name="jdbcTypeForNull" value="OTHER"/>
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
</settings> <!-- 类别名设置 -->
<typeAliases>
<typeAlias alias="User" type="bean.User"/>
<typeAlias alias="Article" type="bean.Article"/>
<typeAlias alias="Post" type="bean.Post"/>
</typeAliases>
</configuration>

查询代码示例

BaseMapper.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"> <mapper namespace="dao.base">
<resultMap id="userResultMap" type="User">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
<result property="address" column="address" />
</resultMap> <resultMap id="postResultMap" type="Post">
<id property="id" column="id" />
<result property="content" column="content" />
</resultMap> <!-- 复杂类型的ResultMap构造 -->
<resultMap id="articleResultMap" type="Article">
<id property="id" column="article_id" />
<result property="title" column="article_title" />
<result property="content" column="article_content" />
<!-- association表示单个外键对象使用 -->
<!-- property为Article里的成员 -->
<!-- columnprefix表示在userResultMap里的column全都加上prefix,以跟select语句里column label的匹配 -->
<association property="user" resultMap="userResultMap" columnPrefix="user_"/>
<!-- collection表示多个的外键对象List,例如一对多关系 -->
<!-- 此处表示一个Article对应多个Post -->
<collection property="postList" resultMap="postResultMap" columnPrefix="post_" />
</resultMap>
</mapper>

UserMapper.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"> <!-- 对应类的SQL语句 -->
<mapper namespace="dao.userdao">
<!-- 简单查询示例 -->
<!-- Select的flushCache默认为false -->
<!-- useCache默认为true,statementType默认为PREPARED -->
<!-- MyBatis结果集的映射规则就是使用column label(没有则直接使用column) -->
<!-- 去对应resultType里的property名字并赋值(除了使用resultMap手动控制映射) -->
<select id="selectUserByID" parameterType="int" resultType="User"
flushCache="false" useCache="true" timeout="10000" statementType="PREPARED">
SELECT
*
FROM user
WHERE id = #{id}
</select> <!-- 简单插入示例 -->
<!-- 此处值得注意的是 userGenerateKeys,keyProperty,keyColumn -->
<!-- 这三个值表示使用jdbc自动生成主键并赋给数据库里的column和实体的property -->
<!-- flushCache默认为true,statmentType默认为PREPARED -->
<insert id="insertUser" parameterType="User" timeout="10000"
flushCache="true" statementType="PREPARED" useGeneratedKeys="true"
keyProperty="id" keyColumn="id">
INSERT INTO user (name, age, address)
VALUES (#{name}, #{age}, #{address})
</insert> <!-- 简单更新示例 -->
<update id="updateUser" parameterType="User">
UPDATE user SET
name = #{name},
age = #{age},
address = #{address}
</update> <!-- 简单删除 -->
<delete id="deleteUser" parameterType="int">
DELETE FROM user
WHERE id = #{id}
</delete> <!-- Cache 配置 -->
<!-- 根据官方文档的介绍
By default, just local sessión caching is enabled that is used solely to cache data for the duration of a sessión.
To enable a global second level of caching you simply need to add one line to your SQL Mapping file
需要加上这个cache标签才能使用全局的cache,否则只能使用session范围内的一级缓存,实际上在spring中根本无法使用一级缓存 -->
<cache
eviction="FIFO"
flushInterval="60000"
size="512"
readOnly="true" />
</mapper>

ArticleMapper.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"> <mapper namespace="dao.articledao">
<!-- select里的column label必须与resultMap里的column(或者加上prefix的column)匹配 -->
<select id="selectArticleById" parameterType="int" resultMap="dao.base.articleResultMap">
SELECT
a.id AS article_id,
a.title AS article_title,
a.content AS article_content,
a.user_id AS user_id,
u.name AS user_name,
u.age AS user_age,
u.address AS user_address,
p.id AS post_id,
p.content AS post_content
FROM article AS a
JOIN user AS u
ON a.user_id = u.id
JOIN post AS p
ON a.id = p.article_id
WHERE a.id = #{id}
</select>
</mapper>

Dao

package dao;

import bean.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-8-3
* Time: 下午9:21
* To change this template use File | Settings | File Templates.
*/
@Repository
public class UserDao extends SqlSessionDaoSupport {
public User selectUserById(int id) {
return getSqlSession().selectOne("dao.userdao.selectUserByID", id);
}
} package dao; import bean.Article;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-8-5
* Time: 下午12:37
* To change this template use File | Settings | File Templates.
*/
@Repository
public class ArticleDao extends SqlSessionDaoSupport {
public Article selectArticleById(int id) {
return getSqlSession().selectOne("dao.articledao.selectArticleById", id);
}
}

bean

package bean;

public class User {

    private Integer id;

    private String name;

    private Integer age;

    private String address;

    public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return id + "|" + name + "|" + age + "|" + address;
}
}
package bean;

public class Post {
private String id;
private String content; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} @Override
public String toString() {
return content;
}
}
package bean;

import java.util.List;

public class Article {

    private Integer id;

    // user属于单个外键复杂对象
private User user; private String title; private String content; // postList属于一对多关系复杂对象
private List<Post> postList; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title == null ? null : title.trim();
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content == null ? null : content.trim();
} public List<Post> getPostList() {
return postList;
} public void setPostList(List<Post> postList) {
this.postList = postList;
} @Override
public String toString() {
return title + "|" + content + "|" + user + "|" + postList;
}
}

MyBatis 查询示例的更多相关文章

  1. 第一个Mybatis程序示例 Mybatis简介(一)

    在JDBC小结中(可以参阅本人JDBC系列文章),介绍到了ORM,其中Mybatis就是一个不错的ORM框架 MyBatis由iBatis演化而来 iBATIS一词来源于“internet”和“aba ...

  2. mybatis查询返回null解决方案

    mybatis查询返回null解决方案: 问题:查询出的列与javabean中的字段名不一致. 解决方案: 1.将javabean中的字段改为和查询出的列名一致: 2.将sql加入as改变列名,和ja ...

  3. apache ignite系列(九):使用ddl和dml脚本初始化ignite并使用mybatis查询缓存

    博客又断了一段时间,本篇将记录一下基于ignite对jdbc支持的特性在实际使用过程中的使用. 使用ddl和dml脚本初始化ignite 由于spring-boot中支持通过spring.dataso ...

  4. Elasticsearch .Net Client NEST 多条件查询示例

    Elasticsearch .Net Client NEST 多条件查询示例 /// <summary> /// 多条件搜索例子 /// </summary> public c ...

  5. mybatis 查询 xml list参数

    mybatis 查询 xml list参数: <select id="getByIds" resultType="string" parameterTyp ...

  6. NHibernate查询示例合集

    基本查询   复杂查询示例 /// <summary> /// 获取自定义表单数据中属于部门的部分 /// </summary> /// <param name=&quo ...

  7. MyBatis 查询映射自定义枚举

    背景                  MyBatis查询若想映射枚举类型,则需要从 EnumTypeHandler 或者 EnumOrdinalTypeHandler 中选一个来使用         ...

  8. mybatis查询异常-Error querying database. Cause: java.lang.ClassCastException: org.apache.ibatis.executor.ExecutionPlaceholder cannot be cast to java.util.List

    背景,mybatis查询的时候直接取的sqlsession,没有包装成SqlSessionTemplate,没有走spring提供的代理. 然后我写的获取sqlsession的代码没有考虑到并发的情况 ...

  9. spring boot - 整合jpa多对对关系保存和查询示例

    pojo: package com.example.zs.springDataJpa; import org.hibernate.annotations.Proxy; import javax.per ...

随机推荐

  1. Linux入门篇(一)——文件

    这一系列的Linux入门都是本人在<鸟哥的Linux私房菜>的基础上总结的基本内容,主要是记录下自己的学习过程,也方便大家简要的了解 Linux Distribution是Ubuntu而不 ...

  2. Fiddler中如何过滤会话、查询会话、保存回话、对比会话

    1.Fiddler中如何过滤会话 在抓包时,会捕捉到很多的会话,但是我们只关心特定的接口会话,这个时候我们可以使用过滤功能,来帮助我们从一大堆会话中筛选去我们关心的会话 (1)勾选过滤器选项Filte ...

  3. 析构函数(C#)

    析构函数又称终结器,用于析构类的实例. 定义 析构函数(destructor) 与构造函数相反,当对象结束其生命周期时(例如对象所在的函数已调用完毕),系统自动执行析构函数.析构函数往往用来做&quo ...

  4. Java数组的创建和初始化

    我们说到数组,可能有的人就会比较害怕了,其实,数组只是把对象序列(很多个对象)或者基本类型序列(很多个基本类型)放在一起而已.数组是通过方括号下标操作符[]来定义和使用的.如果要定义,创建一个数组,只 ...

  5. jQuery 核心函数 (十一)

    函数 描述 jQuery() 接受一个字符串,其中包含了用于匹配元素集合的 CSS 选择器. jQuery.noConflict() 运行这个函数将变量 $ 的控制权让渡给第一个实现它的那个库.

  6. 不会PS如何自制简单线条、任意填充色的小图标

    最近在做H5的开发中,需要用到一些简单的小图标,百度出来的图片,总是或多或少差了一些颜色.于是准备自己制作图片,PS是不会的,学习以及软件安装太费时,于是就准备用常见的软件来试着做一做. 在尝试了 w ...

  7. Spark算子篇 --Spark算子之aggregateByKey详解

    一.基本介绍 rdd.aggregateByKey(3, seqFunc, combFunc) 其中第一个函数是初始值 3代表每次分完组之后的每个组的初始值. seqFunc代表combine的聚合逻 ...

  8. js 实现复制剪切

    原生js实现复制 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  9. JAVA 将图片转换为Base64编码

    这里使用的jar包是commons-codec-1.10.jar; 示例代码 import java.io.FileInputStream; import java.io.FileOutputStre ...

  10. C# 构造函数总结

    构造函数 构造函数分为:实例构造函数,静态构造函数,私有构造函数. 实例构造函数 1.构造函数的名字与类名相同. 2.使用 new 表达式创建类的对象或者结构(例如int)时,会调用其构造函数.并且通 ...