MyBatis 查询示例
环境搭建
数据库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 查询示例的更多相关文章
- 第一个Mybatis程序示例 Mybatis简介(一)
在JDBC小结中(可以参阅本人JDBC系列文章),介绍到了ORM,其中Mybatis就是一个不错的ORM框架 MyBatis由iBatis演化而来 iBATIS一词来源于“internet”和“aba ...
- mybatis查询返回null解决方案
mybatis查询返回null解决方案: 问题:查询出的列与javabean中的字段名不一致. 解决方案: 1.将javabean中的字段改为和查询出的列名一致: 2.将sql加入as改变列名,和ja ...
- apache ignite系列(九):使用ddl和dml脚本初始化ignite并使用mybatis查询缓存
博客又断了一段时间,本篇将记录一下基于ignite对jdbc支持的特性在实际使用过程中的使用. 使用ddl和dml脚本初始化ignite 由于spring-boot中支持通过spring.dataso ...
- Elasticsearch .Net Client NEST 多条件查询示例
Elasticsearch .Net Client NEST 多条件查询示例 /// <summary> /// 多条件搜索例子 /// </summary> public c ...
- mybatis 查询 xml list参数
mybatis 查询 xml list参数: <select id="getByIds" resultType="string" parameterTyp ...
- NHibernate查询示例合集
基本查询 复杂查询示例 /// <summary> /// 获取自定义表单数据中属于部门的部分 /// </summary> /// <param name=&quo ...
- MyBatis 查询映射自定义枚举
背景 MyBatis查询若想映射枚举类型,则需要从 EnumTypeHandler 或者 EnumOrdinalTypeHandler 中选一个来使用 ...
- 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的代码没有考虑到并发的情况 ...
- spring boot - 整合jpa多对对关系保存和查询示例
pojo: package com.example.zs.springDataJpa; import org.hibernate.annotations.Proxy; import javax.per ...
随机推荐
- 自定义类型转换器Convert
//自己指定一个类型转换器(将String转成Date) ConvertUtils.register(new Converter() { @Override public Object convert ...
- Mongodb高级查询【二】
上一篇文章,写了mongodb常规操作,继续写入,本章主要讲高级查询,文本,聚集,大数据查询. Mongodb的查询语法是很多的,是NOSQL队伍中比较丰富的一个.当然有很多查询跟关系型查询无法相比. ...
- [Spark性能调优] 第二章:彻底解密Spark的HashShuffle
本課主題 Shuffle 是分布式系统的天敌 Spark HashShuffle介绍 Spark Consolidated HashShuffle介绍 Shuffle 是如何成为 Spark 性能杀手 ...
- java socket 和.net socket 通讯 demo
结束符协议"##" import java.io.BufferedReader; import java.io.IOException; import java.io.InputS ...
- hook 虚表
PVOID* GetVtpl(PVOID lpThis, int nIndex){ return *(PVOID**)lpThis + nIndex;} PVOID HookVtpl(PVOID* ...
- duilib基本流程
duilib的基本流程如上图,通过解析一个xml文件,将文件中的内容渲染为窗口界面,这个解析过程由WindowImplBase类来完成. 基本框架如下: 1. 首先在公共头文件中加入如下内容: #in ...
- java web 之 listen 与 filter
一.Listener监听器 Javaweb开发中的监听器,是用于监听web常见对象 HttpServletRequest HttpSession ServletContext 监听它们的创建与销毁.属 ...
- Java关于BufferedWriter.newline()换行的注意事项
最近项目中需要导出文件,其实导出文件是一个挺简单的事情.但是却遇到了很奇怪的问题. 首先导出到文件需要用到 BufferedWriter.而换行则是通过 bw.newline() 方法,问题将出在 n ...
- Spring MVC 详解之废话少说
<陈翔六点半之废话少说>.... Spring WEB MVC 的请求流程: Spring WEB MVC架构: 开始创建.配置gradle项目 1.在gralde项目中,选择SDK 和框 ...
- vim中的批量替换
VI中的批量替换 1) 文件内全部替换: :%s#abc#123#g (如文件内有#,可用/替换,:%s/abc/123/g) --注:把abc替换成123 (或者: %s/str ...