源码下载
首先下载mybatis-parent的源码:
然后下载mybatis的源码:
mybatis源码POM中的以下包需要注释,否则会报错 :
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### Cause: java.lang.IllegalStateException: Cannot enable lazy loading because Javassist is not available. Add Javassist to your classpath.
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.2.20</version>
<!-- <scope>compile</scope>-->
<!-- <optional>true</optional>-->
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.27.0-GA</version>
<!-- <scope>compile</scope>-->
<!-- <optional>true</optional>-->
</dependency>
将mybatis-parent与mybatis导入idea同一个project下。
mybatis需要设置父工程为mybatis-parent,需要注意 parent-version
标签要一致:
测试模块搭建
新建一个测试模块,这里叫mybatis-gabriel
项目基础架构
pom :
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0-SNAPSHOT</version>
</dependency>
</dependencies>
相关代码
主要测试入口代码
public class TestMain {
public static void main(String[] args) {
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SqlSessionFactory sqlSessionFactory = null;
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = null;
try {
sqlSession = sqlSessionFactory.openSession();
RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
Role role = roleMapper.getRole(1L);
System.out.println(role.getId() + ":" + role.getRoleName() + ":" + role.getNote());
sqlSession.commit();
} catch (Exception e) {
// TODO Auto-generated catch block
sqlSession.rollback();
e.printStackTrace();
} finally {
sqlSession.close();
}
}
}
po :
/*
* @author gethin
* 角色的实体类
*/
public class Role {
private long id;
private String roleName;
private String note;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
MyStringHandler :
@MappedTypes({String.class})
@MappedJdbcTypes(JdbcType.VARCHAR)
public class MyStringHandler implements TypeHandler<String> {
Logger log= Logger.getLogger(MyStringHandler.class.getName());
@Override
public String getResult(ResultSet rs, String colName) throws SQLException {
log.info("使用我的TypeHandler,ResultSet列名获取字符串");
return rs.getString(colName);
}
@Override
public String getResult(ResultSet rs, int index) throws SQLException {
log.info("使用我的TypeHandler,ResultSet下标获取字符串");
return rs.getString(index);
}
@Override
public String getResult(CallableStatement cs, int index) throws SQLException {
log.info("使用我的TypeHandler,CallableStatement下标获取字符串");
return cs.getString(index);
}
@Override
public void setParameter(PreparedStatement ps, int index, String value, JdbcType arg3) throws SQLException {
log.info("使用我的TypeHandler");
ps.setString(index, value);
}
}
mapper :
public interface RoleMapper {
public Role getRole(Long id);
Role selectTest(String roleName, int rowNum, Map map);
public Role findRole(String roleName);
public int deleteRole(Long id);
public int insertRole(Role role);
}
RoleMapper.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="org.mybatis.debug.mapper.RoleMapper">
<resultMap type="org.mybatis.debug.po.Role" id="roleMap">
<id column="id" property="id" javaType="long" jdbcType="BIGINT" />
<result column="role_name" property="roleName" javaType="string"
jdbcType="VARCHAR" />
<result column="note" property="note"
typeHandler="org.mybatis.debug.handle.MyStringHandler" />
</resultMap>
<!--复杂sql解析调试-->
<select id="selectTest" resultMap="roleMap">
select id,role_name as roleName,note from role
<where>
<if test="roleName==#roleName">
and #{roleName} = #{roleName} and #{rowNum} = #{rowNum}
</if>
<if test="map.list != null">
<foreach collection="map.list" item="item" open="(" close=")" separator="," >
#{item}
</foreach>
</if>
</where>
<!--prefix=“set" 给拼接的sql语句加前缀,前缀为set,
suffixOverrides=","自动将最后一个逗号删除-->
<trim prefix="AND" suffixOverrides=",">
<choose>
<when test="roleName!=null">
roleName like #{roleName},
</when>
<when test="rowNum!=null">
rowNum = #{rowNum},
</when>
<otherwise>
1 = 1
</otherwise>
</choose>
</trim>
</select>
<select id="getRole" parameterType="long" resultMap="roleMap">
select
id,role_name as roleName,note from role where id=#{id}
</select>
<select id="findRole" parameterType="long" resultMap="roleMap">
select
id,role_name,note from role where role_name like CONCAT('%',#{roleName
javaType=string,
jdbcType=VARCHAR,typeHandler=com.gethin.handler.MyStringHandler},'%')
</select>
<insert id="insertRole" parameterType="org.mybatis.debug.po.Role">
insert into
role(role_name,note) value(#{roleName},#{note})
</insert>
<delete id="deleteRole" parameterType="long">
delete from role where
id=#{id}
</delete>
</mapper>
sql :
-- ----------------------------
-- Table structure for role
-- ----------------------------
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (
`id` int(11) DEFAULT NULL,
`role_name` varchar(255) DEFAULT NULL,
`note` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of role
-- ----------------------------
INSERT INTO `role` VALUES ('1', '管理员', '管理员');
-- ----------------------------
-- Table structure for role
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) DEFAULT NULL,
`role_id` int(11) DEFAULT NULL,
`user_name` varchar(255) DEFAULT NULL,
`user_note` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of role
-- ----------------------------
INSERT INTO `user` VALUES ('1', '1','张三', '管理员张三');
oracle :
create table ROLE
(
ID NUMBER not null
constraint ROLE_PK
primary key,
ROLE_NAME VARCHAR2(50),
NOTE VARCHAR2(100)
);
/
INSERT INTO ROLE VALUES ('1', '超级管理员', '超级管理员');
INSERT INTO ROLE VALUES ('2', '普通管理员', '普通管理员');
INSERT INTO ROLE VALUES ('3', '超级会员', '超级会员');
INSERT INTO ROLE VALUES ('4', '普通会员', '普通会员');
create table "TUSER"
(
ID NUMBER not null,
ROLE_ID NUMBER,
USER_NAME VARCHAR2(100),
USER_NOTE VARCHAR2(100)
);
/
create unique index USER_ID_UINDEX
on "TUSER" (ID);
/
alter table "TUSER"
add constraint USER_PK
primary key (ID);
/
INSERT INTO TUSER VALUES ('1', '1','张三', '超级管理员张三');
INSERT INTO TUSER VALUES ('2', '1','李四', '超级管理员李四');
错误解决
通过指定mybatis-config.xml
启动时可能会报错:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: java.net.ConnectException: Connection timed out: connect
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:37)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:99)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)
at org.mybatis.debug.TestMain.main(TestMain.java:30)
Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: java.net.ConnectException: Connection timed out: connect
at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:283)
at org.apache.ibatis.parsing.XPathParser.<init>(XPathParser.java:134)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.<init>(XMLConfigBuilder.java:85)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:96)
... 2 more
这是因为指定mybatis配置验证xml规则的dtd头连接失败
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
将头部修改为:
<!--<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"src\main\java\org\apache\ibatis\builder\xml\mybatis-3-config.dtd">
或者本地路径的全路径,如果在非mybatis源码包下,需要写全路径,比如自己定义的mybatis-gabriel项目。只要是mybatis的xml配置都得修改。
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"D:\StudyData\Mybatis\SourceCode\mybatis-master\src\main\java\org\apache\ibatis\builder\xml\mybatis-3-mapper.dtd">
根据自己实际项目地址修改,ctrl+鼠标左键可打开本地‘http://mybatis.org/dtd/mybatis-3-config.dtd’,将路径指定为本地路径即可。
如果在项目发布时,记得把这个路径改为服务器环境下的dtd文件地址,切记!
学习博客
调试博客
Mybatis重要类解析
四大组件Executor、StatementHandler、ParameterHandler、ResultSetHandler 详解
知乎源码解析