1. 如何构建一个mybatis的环境
a) 导入mybatis-3.2.2.jar
b) 导入数据库驱动包
c) mabatis-config配置文件
d) mapper配置文件
e) javaBean实体类
f) 接口
简单的CURD
构建一个mybatis的环境具体步骤:
<?xml version="1.0"encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTDConfig 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- mybatis的数据类型和处理器等等...都可以实现接口重写,然后配置 -->
<configuration>
<!—使用properties给dataSource的属性动态赋值 -->
<properties resource="org/mybatis/example/config.properties"/>
<!—别忘了typeAliases别名是你的好伙伴-->
<typeAliases>
<typeAlias type="org.mybatis.example.Blog" alias="Blog" />
</typeAliases>
<environments default="de">
<environment id="de">
<transactionManager type="JDBC" />
<dataSource type="POOLED" >
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--
映射文件
-->
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
配置Mapper文件:
<?xml version="1.0"encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTDMapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.example.BlogMapper">
<!— 简单的CURD -->
<select id="selectBlog1"parameterType="int" resultType="Blog">
select id,name from Blogwhere id = #{id}
</select>
<select id="selectBlog2" resultType="Blog">
select * from blog
</select>
<insert id="insertAuthor"parameterType="Blog" >
<selectKey></selectKey>
insert into Blog (name)values (#{name})
</insert>
<update id="updateAuthor"parameterType="Blog">
update Blog set name =#{name} where id = #{id}
</update>
<delete id="deletetAuthor"parameterType="Blog">
delete from Blog where id =#{id}
</delete>
<!-- 使用 resultMap -- >
<resultMap id="aa" type="Blog">
<id property="id"column="id"/>
<result property="name"column="name"/>
</resultMap>
<select id="selectBlog3"resultMap="aa">
select* from blog
</select>
<select id="selectBlog3"resultType="Blog">
select* from blog
</select>
</mapper>
Blog博客实体类:
package org.mybatis.example;
public classBlog {
private int id;
private String name;
private Author author;
public Author getAuthor() {
return author;
}
public void setAuthor(Authorauthor) {
this.author = author;
}
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 Blog(int id, String name) {
this.id = id;
this.name = name;
}
public Blog() {
}
}
使用接口:
public interfaceBlogMapper {
<!-- 使用注解 -- >
@Select("SELECT * FROM blog WHERE id = #{id}")
BlogselectBlog(intid);
<!-- 依赖Mapper配置文件 -- >
BlogselectBlog3(intid);
}
Test测试类:
package com.fzh.test;
public class Test {
publicstatic void main(String[] args) throws IOException {
Stringresource = "org/mybatis/example/mybatis-config.xml";
InputStreaminputStream = Resources.getResourceAsStream(resource);
SqlSessionsession = null;
try{
//InputStreaminputStream =Test.class.getResourceAsStream("/org/mybatis/example/mybatis-config.xml");
SqlSessionFactorysqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
session= sqlSessionFactory.openSession();
//通过命名空间org.mybatis.example.BlogMapper和id-selectBlog1映射查询
// Blog blog = (Blog)session.selectOne("selectBlog1", 2);
// System.out.println("blog.name:"+blog.getName());
/*Blog blog = new Blog();
blog.setName("abcdefg");
intcount = session.insert("insertAuthor",blog);
System.out.println(count);
session.commit();*/
/*session.update("org.mybatis.example.BlogMapper.updateAuthor",new Blog(7,"c"));
session.commit();*/
/*List<Blog>list = session.selectList("selectBlog2");
for(Blogblog:list) {
System.out.println(blog.getName());
}*/
/*Blogblog = new Blog();
blog.setId(7);
session.update("org.mybatis.example.BlogMapper.deletetAuthor",blog);
session.commit();*/
//使用3.2新特征接口映射
//@使用注解
/*SqlSessionsession = sqlSessionFactory.openSession();
try{
BlogMapper mapper =session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(1);
System.out.println("blog.name:"+blog.getName());
}finally {
session.close();
}*/
//接口使用xml配置sql语句
/*BlogMapper mapper =session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog3(2);
System.out.println("blog.name:"+blog.getName());*/
//没有参数的情况下,传入参数也不会报错
//List<Blog>list = session.selectList("selectBlog2",2);
List<Blog>list = session.selectList("selectBlog2");
for(Blogblog:list) {
System.out.println(blog.getName());
}
}catch(Exceptionex) {
if(session!=null)
session.close();
}
}
}
这些情况下,MyBatis 会在幕后自动创建一个 ResultMap,基于属性名来映射列到 JavaBean 的属性上。如果列名没有精确匹配,你可以在列名上使用 select 字句的别名(一个基本的 SQL 特性)来匹配标签。比如:
<select id="selectUsers" parameterType="int" resultType="User">
select
user_id as"id",
user_name as"userName",
hashed_password as"hashedPassword"
fromsome_table
whereid = #{id}
</select>
ResultMap 最优秀的地方你已经了解了很多了,但是你还没有真正的看到一个。这些简单的示例不需要比你看到的更多东西。只是出于示例的原因, 让我们来看看最后一个示例中外部的 resultMap 是什么样子的,这也是解决列名不匹配的另外一种方式。
<resultMap id="userResultMap" type="User">
<id property="id" column="user_id" />
<result property="username" column="username"/>
<result property="password" column="password"/>
</resultMap>